C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » VB.NET Syntax »

choosing multiple values from checkbox list and put in database and back from the database to check


Posted Date: 14 Nov 2008    Resource Type: Code Snippets    Category: VB.NET Syntax
Author: Abraham KuriakoseMember Level: Gold    
Rating: 1 out of 5Points: 10



This code shows how to choose multiple value from checkbox


mports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Dim li As New ListItem ' create a new list item
Dim i, j As Integer
Dim arr As Array
Dim str As String
Dim dtr As SqlDataReader
Dim con As New SqlConnection("")
Dim cmd As New SqlCommand
'1st method
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

For Each li In CheckBoxList1.Items

If li.Selected = True Then
lbltext.Text = lbltext.Text & "," & li.Text
End If
Next li
' query =lbltext.text
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
lbltext.Text = "" 'put a label
End Sub
'2nd method
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
For i = 0 To CheckBoxList1.Items.Count - 1
If CheckBoxList1.Items(i).Selected = True Then
lbltext.Text = lbltext.Text & "," & CheckBoxList1.Items(i).Text
End If
Next
con.Open()
cmd = New SqlCommand("insert into check1 values('" & lbltext.Text & "')", con)
cmd.ExecuteNonQuery()
con.Close()
CheckBoxList1.SelectedIndex = -1
End Sub
'Back from the database
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
con.Open()
cmd = New SqlCommand("select * from check1", con)
dtr = cmd.ExecuteReader
While (dtr.Read)
str = dtr(0)
End While
con.Close()
arr = Split(str, ",")
For i = 0 To UBound(arr)
ListBox1.Items.Add(arr(i))
For j = 0 To CheckBoxList1.Items.Count - 1
If CheckBoxList1.Items(j).Text = arr(i) Then
CheckBoxList1.Items(j).Selected = True
End If
Next
Next

End Sub
End Class




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Multiple selection in check box  .  Checkbox  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Open File Dialog Control
Previous Resource: Animated closing for windows form in VB.Net
Return to Discussion Resource Index
Post New Resource
Category: VB.NET Syntax


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use