Click here to Skip to main content
15,910,121 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i am trying to add value in checklistbox from database on combobox_SelectedIndexChanged event.

for that i used these code

VB
Dim a, b As Integer
       Dim h As String
       h = Convert.ToString(comboBox3.Text)
       commObj = New OleDbCommand("SELECT Test FROM SubTest WHERE MainTest = '" & h & "' ", cnn)
       myAdapter = New OleDbDataAdapter(commObj)
       myAdapter.Fill(myDataSet1, "cDetails")
       For Each ds As DataRow In myDataSet1.Tables("cDetails").Rows




           CheckedListBox1.Items.Add(ds("SubTest").ToString())
       Next


but main issue is that when i got output in checklistbox that time at list starting we get 8 blank item( blank space ) and later database selected item start

my question is why these 8 space are present there.
i am trying so many possibility but cant get solution :-(
try to help for these problem.
Posted
Updated 31-Jan-12 20:12pm
v2
Comments
RDBurmon 1-Feb-12 2:01am    
Put the proper values for 'MainTest'

SELECT Test FROM SubTest WHERE MainTest =''

Now my question is when you run this query Have you recevied those 8 empty values as well in the database itself
harishtaware 1-Feb-12 2:06am    
no that space not present in database. it show at only in checkboxlist.
and i am selecting proper values for maintest. i got required output but why these space come there at starting :-(
Simon_Whale 1-Feb-12 4:21am    
You should also change your SQL Statement to include the use of parameterised queries http://www.4guysfromrolla.com/webtech/092601-1.2.shtml

1 solution

you need to do like this in your vb code..
first of fill your datatable myDataSet1 with your select query data

C#
CheckedListBox1.DataValueField = "Test";
CheckedListBox1.DataTextField = "Test";
CheckedListBox1.DataSource = myDataSet1;
CheckedListBox1.DataBind();


no need to do like any loop for attaching items to checkbox list
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900