locked
How to load the state of checkboxlist in database asp.net? RRS feed

  • Question

  •  got 2 checkboxlists , each checkbox has 6 checkboxes inside it (below is my database)

    checkboxlist1 : id , rowa

    checkboxlist2:id , rowb

         id       : int
         rowa : nvarchar(250)
         rowb : nvarchar(250)

    When I click one checkbox, the value is saved to my database as A1 , A2.... Here is my code:   

         Dim str1 As [String] = ""
            For a As Integer = 0 To CheckBoxList1.Items.Count - 1

                If CheckBoxList1.Items(a).Selected Then

                    If str1 = "" Then
                        str1 = CheckBoxList1.Items(a).Text
                    Else

                        str1 += "," + CheckBoxList1.Items(a).Text
                        str1 = ""

                    End If
                End If

            Next
            For a1 As Integer = 0 To CheckBoxList1.Items.Count - 10
                CheckBoxList1.Enabled = False
            Next

            Dim str2 As [String] = ""
            For b As Integer = 0 To CheckBoxList2.Items.Count - 1

                If CheckBoxList2.Items(b).Selected Then

                    If str2 = "" Then
                        str2 = CheckBoxList2.Items(b).Text
                    Else

                        str2 += "," + CheckBoxList2.Items(b).Text


                    End If
                End If
            Next

    > Dim connectionString As String =
    > ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
    > Dim insertSql As String = "INSERT INTO tbtest(rowa,rowb)VALUES(@rowa,@rowb)"
    > Using myConnection As New SqlConnection(connectionString)
    >     myConnection.Open()
    >     Dim myCommand As New SqlCommand(insertSql, myConnection)
    >     myCommand.Parameters.AddWithValue("@rowa", str1) 
    >     myCommand.Parameters.AddWithValue("@rowb", str2)   
    >      myCommand.ExecuteNonQuery() 
    >      myConnection.Close() 
    >      End Using

     - But somehow, when I load the page, it does not show the checked state
       (checked/unchecked). Here is my code in page load.  So my question is
       ?
        1. How to show the checked state of checkboxlist ?
        2. I try to save the state in session , but when i check a blank checkbox , it will then 
           save all the checked value to my database , how to avoid this ? i mean i want to save 
           only the value which is unchecked to database ?
        3. If it is possible can you make the checked checkboxes disable ?

      

    >  Page_load

    >      Dim connectionString As String =  
    > Using myConnection As New SqlConnection(connectionString)     
    >     Dim objCmd_team As SqlCommand = New SqlCommand("SELECT [rowa],[rowb] FROM [tbtest]", myConnection)
    >     myConnection.Open()
    >     Dim objReader As SqlDataReader = objCmd_team.ExecuteReader()

    >     While (objReader.Read())
    >         Dim currentCheckBox As ListItem = checkboxlist1.Items.FindByText(objReader("rowa"))

    >         If currentCheckBox IsNot Nothing Then
    >             currentCheckBox.Selected = True
    >         End If Dim currentCheckBox1 As ListItem = checkboxlist2.Items.FindByText(objReader("rowb"))
    >         If currentCheckBox1 IsNot Nothing Then
    >             currentCheckBox1.Selected = True
    >         End If  End While  End Using

            
     
    • Moved by Carl Cai Monday, December 23, 2013 2:57 AM (from vb) asp.net forum is more dedicated for web issue
    Friday, December 20, 2013 2:53 PM

Answers

  • Hello,

    For ASP.NET related question, you should ask in this site: http://forums.asp.net/


    Best Regards,
    Please remember to mark the replies as answers if they help

    • Marked as answer by Carl Cai Monday, December 23, 2013 2:56 AM
    Sunday, December 22, 2013 5:52 AM