Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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.
 
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 18-Apr-13 17:59pm
Comments
Member 9989624 at 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
good
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)
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
  Permalink  
v3
Comments
Member 9989624 at 19-Apr-13 0:16am
   
could you please help me with the code:(
Idle_Force at 19-Apr-13 0:21am
   
Open connection,
Loop
send data to database
End Loop
Close connection!
Member 9989624 at 19-Apr-13 0:25am
   
Sorry mate..:) Got it.. Working amazingly:) you are my man.. thanks a lott
Idle_Force at 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
0 OriginalGriff 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 19 Apr 2013
Copyright © CodeProject, 1999-2014
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