I have a list box where the user can add choices depending on what items they want active in the database but I cannot figure out how loop through this listbox to update the items.

Any ideas?

Visual Studio 2010

Dim lstitem As String
            For i = 1 To ListBox1.Items.Count()
                lstitem = ListBox1.SelectedItem
                sql = "UPDATE laitemtype SET IsActive = 1 where itemtypedescription=@lst"
                Dim cmd6 As New SqlCommand(sql, Con)
                cmd6.Parameters.Add("@lst", SqlDbType.VarChar, 144)
                cmd6.Parameters("@lst").Value = lstitem
                cmd6.ExecuteNonQuery()
            Next i

Recommended Answers

All 3 Replies

Try it this way:

For i As Integer = 0 To ListBox1.Items.Count - 1
	Dim lstitem As String = ListBox1.SelectedItem.ToString()
	Dim sqlQuery As String = "UPDATE laitemtype SET IsActive = @active WHERE itemtypedescription=@lst"
	Dim cmd6 As New SqlCommand(sqlQuery, Con)
	'I hope oyu have created a new instance of SqlConnection AND Opened it!
	cmd6.Parameters.Add("@active", SqlDbType.Int).Value = 1
	cmd6.Parameters.Add("@lst", SqlDbType.VarChar, 144).Value = lstitem
	cmd6.ExecuteNonQuery()
Next

Works like a charm thanks!

moone009 now please mark the thread as Solved ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.