1 Replies - 1344 Views - Last Post: 19 December 2013 - 03:31 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?

Posted 19 December 2013 - 03:24 PM

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

checkboxlist1 : id , rowa

checkboxlist2:id , rowb

id : int
rowa : bit
rowb : bit
When I click one checkbox, the value is saved to my database as TRUE ,But somehow, when I load the page, it does not show the checked state (checked/unchecked). Here is my code . Can you help me out? Thanks.

. Here is my code:

Button1_click

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", SqlDbType.Bit).Value = 1
myCommand.Parameters.AddWithValue("@rowb", SqlDbType.Bit).Value = 1
myCommand.ExecuteNonQuery() myConnection.Close()
End Using


Page_load

Dim connectionString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
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 currentCheckBox As ListItem = checkboxlist2.Items.FindByText(objReader("rowb"))
If currentCheckBox IsNot Nothing Then
currentCheckBox.Selected = True
End If
End While
End Using

Is This A Good Question/Topic? 0
  • +

Replies To: How to load the state of checkboxlist in database?

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3074
  • View blog
  • Posts: 10,268
  • Joined: 12-December 12

Re: How to load the state of checkboxlist in database?

Posted 19 December 2013 - 03:31 PM

I think you need to use the CheckBoxList's DataBound event instead of PageLoad. During PageLoad the CheckBoxList is empty.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1