1 Replies - 7068 Views - Last Post: 14 November 2009 - 04:41 PM Rate Topic: -----

#1 nkasei28  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 93
  • Joined: 10-May 09

Add all items from listbox into a database

Post icon  Posted 14 November 2009 - 01:55 PM

Hi there, please i want to add all items from my listbox into a database. the first item has to go into the first row of the table followed by the second item into the second row followed by the thir in that order. I have already created the connection with the database. I now want to write an insert query to insert the items into the table with the sql command in a loop but i don't know how to get each item from the listbox and add it to the insert statement. I want to assume that non of the items in the listbox has been selected. Here is my Code. I haven't done much yet. Please i need your help. Thanks for your help in advance.

  Dim Conn As SqlConnection
		Dim cmd As SqlCommand
		Dim ConString As String

		ConString = "Data Source=AKWASIOWUSU-PC;Initial Catalog=MyAttendance;Integrated Security=True"
		Conn = New SqlConnection(ConString)

		Try
			Conn.Open()

			Dim i As Integer = Me.lsbPresent.Items.Count
			Dim j As Integer

			For j = 0 To i
				cmd=New SqlCommand(
			Next
		Catch ex As Exception

		End Try 


Is This A Good Question/Topic? 0
  • +

Replies To: Add all items from listbox into a database

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Add all items from listbox into a database

Posted 14 November 2009 - 04:41 PM

What you have is a good start. Keep in mind that the indexes in a listbox start at 0 so you will have to subtract 1 from the count.

You could just as easily use
For J as integer = 0 to lsbPresent.items.count -1
  cmd = New SQLCommand("Insert Into myTable (field1) Values (" &	lsbpresent.item(j) & ")")
cmd.executeNonQuery
Next


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1