Click here to Skip to main content
13,054,018 members (65,685 online)
Rate this:
Please Sign up or sign in to vote.
See more:

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.

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
                con.ConnectionString = "Provider=SQLOLEDB.1;Password=$impadmins123;Persist Security Info=True;User ID=simp;Initial Catalog=Employee Information;Data Source=SRAVI6"
                cmd.Connection = con
                cmd1.Connection = con
                cmd.CommandText = "insert into tic (name) values ('" & selected & "')"
                cmd1.CommandText = "insert into tic (new) values ('" & selected1 & "')"
Posted 18-Apr-13 16:59pm
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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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)
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
Member 9989624 19-Apr-13 0:16am
could you please help me with the code:(
Idle_Force 19-Apr-13 0:21am
Open connection,
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 19 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100