0 Replies - 1141 Views - Last Post: 20 December 2013 - 06:25 PM Rate Topic: -----

#1 tothanhtuan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-December 13

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

Posted 20 December 2013 - 06:25 PM

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

Is This A Good Question/Topic? 0
  • +

Page 1 of 1