Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 2nd, 2005, 12:40 PM
mh mh is offline
Authorized User
 
Join Date: Mar 2005
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default checklist selection

Hi,

I'm having problems with my checkboxlist. Currently, my checkboxlist is read from a database of fieldnames once the page is loaded. If the user clicks on the Save button, it checks to see what was selected and the variable 'field' is set to 'y' or 'n' if it's not selected. I do this for the whole checkboxlist. The contents in 'field' are then set to the session("test") which is loaded on the next page. However, it says 'n' for all of them. Is there any reason why? Can someone please tell me what is going on? Thanks.

Here is my code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
         Session("recordcount") = ""
        SqlDataAdapter1.Fill(DsVenSel1)
        Session("recordcount") = DsVenSel1.Tables("syscolumns").Rows.Count.ToString
        cmdSave.Text = Session("recordcount")
        ChkLstVen.DataSource = DsVenSel1
        ChkLstVen.DataTextField = "Name"
        ChkLstVen.DataBind()
End Sub

    Private Sub cmdSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Dim i
        Dim field As String
        field = ""
        For i = 0 To ((Session("recordcount")) - 1)
            If ChkLstVen.Items(i).Selected Then
                If i = 0 Then
                    field = "Y"
                ElseIf i > 0 Then
                    field = field & "Y"
                End If
            ElseIf Not (ChkLstVen.Items(i).Selected) Then
                If i = 0 Then
                    field = "N"
                ElseIf i > 0 Then
                    field = field & "N"
                End If
            End If
        Next
  Session("test") = field
        Response.Redirect("DataSelFin.aspx")
End Sub

 
Old May 2nd, 2005, 03:54 PM
Authorized User
 
Join Date: Jul 2004
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Mayur52o
Default

why don't you try the for loop like this


for each chk as checkbox in chklstven.items
   if chk.checked then
     'Code
   elseif not (chk.checked) then
     'Code
   end if
next
 
Old May 2nd, 2005, 04:09 PM
mh mh is offline
Authorized User
 
Join Date: Mar 2005
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I get the following error when I change my code to what was suggested:

Server Error in '/CPS' Application.
--------------------------------------------------------------------------------

Specified cast is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error:


Line 86: field = ""
Line 87: 'For i = 0 To ((Session("recordcount")) - 1)
Line 88: For Each chk As CheckBox In ChkLstVen.Items
Line 89: If chk.Checked Then
Line 90:


Source File: c:\inetpub\wwwroot\CPS\DataSelVen.aspx.vb Line: 88

Stack Trace:


[InvalidCastException: Specified cast is not valid.]
   CPS.DataSelVen.cmdSave_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\CPS\DataSelVen.aspx.vb:88
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
   System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String eventArgument) +57
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument) +18
   System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +33
   System.Web.UI.Page.ProcessRequestMain() +1292


Did I do something wrong?

 
Old May 2nd, 2005, 04:26 PM
Authorized User
 
Join Date: Jul 2004
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Mayur52o
Default

in the declaration what is chkLstVen declared as?
 
Old May 2nd, 2005, 04:29 PM
Authorized User
 
Join Date: Jul 2004
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Mayur52o
Default

is should be declared something like this

Protected WithEvents chkLstVen As System.Web.UI.WebControls.CheckBoxList
 
Old May 2nd, 2005, 04:30 PM
mh mh is offline
Authorized User
 
Join Date: Mar 2005
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It's declared as:

Protected WithEvents ChkLstVen As System.Web.UI.WebControls.CheckBoxList

 
Old May 2nd, 2005, 04:33 PM
Authorized User
 
Join Date: Jul 2004
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Mayur52o
Default

try this see waht it does

  For Each chk As CheckBox In ChkLstVen.Items
            Response.Write(chk.Checked)
        Next
 
Old May 2nd, 2005, 04:38 PM
mh mh is offline
Authorized User
 
Join Date: Mar 2005
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It gives me the same error.

 
Old May 2nd, 2005, 08:41 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

mh,

The problem is that you are binding the list of checkboxes every time the page loads so all the checkbox values are getting reset. You need to load the checkbox list only on the first page load (If Not IsPostback) or on some other one-time event.

-Peter
 
Old May 3rd, 2005, 12:19 PM
mh mh is offline
Authorized User
 
Join Date: Mar 2005
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you, Peter and Mayur52o for your help. It now works.

But, I've come across a different problem now. I'm calling a stored procedure to save what was checked from the checkboxlist into a database. I require some parameters such as Login Id. However, it keeps on saying that my login id is null when it isn't. Each time I hardcode/set something equal to login id, it saves. However, when I try to use the same id again, it doesn't work. Am I doing something wrong? Also, inside my stored procedure, it allows records to be overwritten if it contains the same id, otherwise it creates a new record if the id is new. Here is what I have:

 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not Page.IsPostBack Then
            Session("recordcount") = ""
            SqlDataAdapter1.Fill(DsVenSel1)
            Session("recordcount") = DsVenSel1.Tables("syscolumns").Rows.Count.ToString
            cmdSave.Text = Session("recordcount")
            ChkLstVen.DataSource = DsVenSel1
            ChkLstVen.DataTextField = "Name"
            ChkLstVen.DataBind()
        End If
    End Sub

    Private Sub cmdSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Dim i
        Dim field As String
        field = ""
        For i = 0 To ((Session("recordcount")) - 1)
            If ChkLstVen.Items(i).Selected Then
                If i = 0 Then
                    field = "1"
                ElseIf i > 0 Then
                    field = field & "1"
                End If
            ElseIf Not (ChkLstVen.Items(i).Selected) Then
                If i = 0 Then
                    field = "0"
                ElseIf i > 0 Then
                    field = field & "0"
                End If
            End If
        Next
        Session("test") = field

        Dim myCmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand("cps3_DataSel", SqlConnection1)
        myCmd.CommandType = CommandType.StoredProcedure

        Dim param1 As New System.Data.SqlClient.SqlParameter("@FieldType", SqlDbType.VarChar)
        param1.Direction = ParameterDirection.Input
        param1.Value = "Ven"
        myCmd.Parameters.Add(param1)
        Dim param2 As New System.Data.SqlClient.SqlParameter("@LoginId", SqlDbType.VarChar)
        param2.Direction = ParameterDirection.Input
        param2.Value = "try"
        myCmd.Parameters.Add(param2)
        Dim param3 As New System.Data.SqlClient.SqlParameter("@SpecName", SqlDbType.VarChar)
        param3.Direction = ParameterDirection.Input
        param3.Value = lblSpecName.Text
        myCmd.Parameters.Add(param3)
        Dim param4 As New System.Data.SqlClient.SqlParameter("@GrpName", SqlDbType.VarChar)
        param4.Direction = ParameterDirection.Input
        param4.Value = ""
        myCmd.Parameters.Add(param4)
        Dim param5 As New System.Data.SqlClient.SqlParameter("@Field", SqlDbType.VarChar)
        param5.Direction = ParameterDirection.Input
        param5.Value = Session("Test")
        myCmd.Parameters.Add(param5)

        SqlConnection1.Open()
        myCmd.ExecuteNonQuery()
        SqlConnection1.Close()
        Response.Redirect("DataRun.aspx")
    End Sub
End Class

And this is the error I get:

Server Error in '/CPS' Application.
--------------------------------------------------------------------------------

Cannot insert the value NULL into column 'LoginId', table 'FNTIC_CPES.dbo.CPES_VenSel'; column does not allow nulls. INSERT fails. The statement has been terminated.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'LoginId', table 'FNTIC_CPES.dbo.CPES_VenSel'; column does not allow nulls. INSERT fails. The statement has been terminated.

Source Error:


Line 130:
Line 131: SqlConnection1.Open()
Line 132: myCmd.ExecuteNonQuery()
Line 133: 'flag = myCmd.Parameters("@flag").Value
Line 134: SqlConnection1.Close()


I appreciate all the help that is provided. Thanks.







Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I create a Windows Explorer style checklist pamskate5 VB How-To 1 March 2nd, 2007 02:10 PM
Selection in DataGrid Ardvisoor ASP.NET 2.0 Professional 3 June 14th, 2006 09:23 AM
selection criteria... dbrook007 Other Programming Languages 0 February 21st, 2006 04:41 PM
listbox selection jon23d Access VBA 1 June 21st, 2005 01:35 PM
Exclusive selection RobbieGee MySQL 2 July 5th, 2004 04:35 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.