Click here to Skip to main content
15,885,990 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Team,

I have 2 listboxs. Listbox 1 as name listbox 2 as sex

what i need is when i press a button in vb. the first value of both listbox alone should be inserted to sql. once its inserted those value should alone remove and till both list box is empty.

I had this below code. i know am doing things wrong please correct me. this code is creating duplicate and Nullvalue.

VB
For i = 0 To ListBox1.Items.Count - 1
            For j = 0 To ListBox2.Items.Count - 1
                selected = ListBox1.Items(i).ToString
                selected1 = ListBox2.Items(i).ToString
                MsgBox(selected)
                con.ConnectionString = "Provider=SQLOLEDB.1;Password=$impadmins123;Persist Security Info=True;User ID=simp;Initial Catalog=Employee Information;Data Source=SRAVI6"
                con.Open()
                cmd.Connection = con
                cmd1.Connection = con
                cmd.CommandText = "insert into tic (name) values ('" & selected & "')"
                cmd1.CommandText = "insert into tic (new) values ('" & selected1 & "')"
                cmd.ExecuteNonQuery()
                cmd1.ExecuteNonQuery()
                con.Close()
            Next
        Next
Posted
Comments
Member 9989624 18-Apr-13 23:06pm    
Or how to refer the first listbox should be first coloumn and the next listbox as second coloumn

1 solution

Instead of asking a small part and then changing what your doing the next post, post what you are up to so we can make better solutions.

VB
Dim myList As New Dictionary(Of String, String)
Dim total As Integer = ListBox1.Items.Count - 1
For Each i As Integer = 0 To total
  myList.Add(ListBox1.Items(i).ToString, ListBox2.Items(i).ToString)
Next
For Each kv As KeyValuePair(Of String, String) In myList
  ' kv.Key = the first value
  ' kv.Value = the second value paired with the first
  ' match these to your insert statements
Next
 
Share this answer
 
v3
Comments
Member 9989624 19-Apr-13 0:16am    
could you please help me with the code:(
Idle_Force 19-Apr-13 0:21am    
Open connection,
Loop
send data to database
End Loop
Close connection!
Member 9989624 19-Apr-13 0:25am    
Sorry mate..:) Got it.. Working amazingly:) you are my man.. thanks a lott
Idle_Force 19-Apr-13 0:42am    
Updated! Work on naming your controls something meaningful instead of the default names. It will help you in the end.

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