We help IT Professionals succeed at work.
Get Started

Using Listbox Index in SQL INSERT

JackW9653
JackW9653 asked
on
615 Views
Last Modified: 2012-05-09
Hello Experts,

I have a SQL Server 2005 datbase table with the following structure:
NavCode   nvarchar(10)
FormOrder  int
FormName   nvarchar(50)

I want to insert items from a listbox in a WinForms app (VB.Net) into the table using the ordinal position of the item as the FormOrder. I can get the correct position by using the selectedindex + 1but since I'm inserting several rows at a time, I'm not selecting anything.The NavCode field is the same for all inserted records. Here's the code:

If cn.State = ConnectionState.Closed Then
            cn.Open()
        End If

        Dim strNavCode As String = txtNavCode.Text
        Dim strForm As String = ""
        Dim intOrder As Integer =""
       
        Dim strSelect As String = ""


        Dim sqlDA As New SqlClient.SqlDataAdapter(strSelect, cn)

        Try
            If lbWorkflow.Items.Count > 0 Then
                For Each strForm In lbWorkflow.Items
                    strSelect = "INSERT INTO tblWorkflows (NavCode, FormOrder, FormName)" & _
                        " VALUES ('" & strNavCode & "','" & intOrder & "','" & strForm & "')"
                    sqlDA.SelectCommand.CommandText = strSelect
                    sqlDA.SelectCommand.ExecuteNonQuery()
                Next
            End If
        Catch err As SqlException
            MsgBox(err.Message)
        Finally
            cn.Close()
        End Try

How do I set the ordinal position of each item as FormOrder during the Insert query?
Thanks,

JackW9653
Comment
Watch Question
Chief Technology Officer
CERTIFIED EXPERT
Most Valuable Expert 2011
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE