470,853 Members | 2,311 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,853 developers. It's quick & easy.

Way to loop code for inserting Checkboxlist values into DB?

I am trying to do two things for a test app:
1) Populate a CheckboxList webcontrol by loading a list of subject
names (for the DataTextField) and subject codes (for the
2) Respond to user selection by inserting their boolean values based
on checked vs. unchecked boxes. I have a SQL Server table with a lot
of columns of data type bit. All of the columns are named after
different subject codes (such as HSmath, HSast, etc.)

The first step I can do fine - but the second I am have trouble with.
How would I write looping code that would insert the status of each
checkbox into a field with names after the checkbox's value? Meaning,
how would I get HSmath's checked status to be inserted into the
HSmath field in my table, and then the HSast's checked status to be
inserted into the HSast field, and so on.

I have included my code thus far (removing unnecessary markup),
including my looping code for printing out results on the screen
(screenshot attached) - obviously this is what I'd replace with my
database code. Any help is appreciated.


Dim strConn as string =

Sub Page_Load(sender AS Object,E as EventArgs)
If Not IsPostBack Then
Dim conPubs As SqlConnection
Dim cmdSelect As SqlCommand
Dim dtrSubjects As SqlDataReader
conPubs = New SqlConnection(strConn)
cmdSelect = New SqlCommand( "Select * From
tutors_subjectcodes order by Subject", conPubs )
dtrSubjects = cmdSelect.ExecuteReader()

check1.DataSource = dtrSubjects
check1.DataTextField = "Subject"
check1.DataValueField = "subjectcode"

End If
End Sub

Sub btnStep2_Click(ByVal sender As System.Object, ByVal e As
Dim msg As String
Dim li As ListItem
msg = ""
For Each li In check1.Items
If li.Selected = True Then
msg = msg & "<li> " & li.Text &
" (" & li.Value &
End If
lblSubjects.Text =
& msg & "</ul>"
End Sub
<asp:checkboxlist id="check1"
<asp:Button id="btnStep2"
onclick="btnStep2_Click" runat="server"
<asp:label id="lblSubjects"
Posted Via Usenet.com Premium Usenet Newsgroup Services
Nov 18 '05 #1
0 1349

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by CJ | last post: by
reply views Thread by Agi Chen | last post: by
3 posts views Thread by Dune | last post: by
1 post views Thread by Patrick.O.Ige | last post: by
3 posts views Thread by Stephen | last post: by
2 posts views Thread by Stimp | last post: by
2 posts views Thread by RSH | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.