title: Insert list box items to sql server
body: <p>I am trying to add items from a list box to a foreign key table in sql server.</p>
  
  <p>Any items in the list box should be added to the database.</p>
  
  <p>Current I am stuck on the loop because it is not collecting the item in the list box.</p>
  
  <p>The items in the list box will not be selected.</p>
  
  <p>Any tips would be great!</p>
  
  <pre><code>Dim CommandText As String = "INSERT INTO code (id, Code, specialty) VALUES (@id, @ClientCode, @specialty);"
          Dim cmd As SqlCommand = New SqlCommand(CommandText, sqlconninsertDB)
  
  For Each item As ListItem In lstspecialty.Items
                  If Items.Count = lstspecialty.Items.Count Then
                      Try
                          With cmd.Parameters
                              .Add(New SqlParameter("@id", ID))
                              .Add(New SqlParameter("@ClientCode", Code))
                              .Add(New SqlParameter("@specialty", item.Value))
                          End With
  
                      cmd.ExecuteNonQuery()
                      Label.Text = "Data Inserted"
                  Catch ex As Exception
                       ErrorLabel.Text = "Error Message"
                  End Try
              End If
          Next
  </code></pre>