Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
Was hoping I could get some help with this code. I'm trying to fill the table with the data found in two Listboxs. But I'm an getting an error: SQLException was unhandled. The variable name '@TrackTitle' has already been declared. Variable names must be unique within a query batch or stored procedure. Is there anyway to make this work? I've tried all sorts of different configurations but no luck.

VB
objConnection.Open()

           .CommandText = "INSERT INTO Tracks " & _
                    "(TrackTitle, TrackSeqNum) " & _
                    "VALUES(@TrackTitle, @TrackSeqNum )"

           For i = 0 To ListBox3.Items.Count - 1
               .Parameters.AddWithValue("@TrackTitle", ListBox3.Items(i))
               .Parameters.AddWithValue("@TrackSeqNum", ListBox1.Items(i))

               .ExecuteNonQuery()
           Next
           
           objConnection.Close()
Posted

1) Only for the first time (i=0) you can add parameter! For the next loop you must change it!
or...
2) Build query in run-time in this way:
VB
sSQL = "INSERT INTO Tracks (TrackTitle, TrackSeqNum) " & vbCrLf & _
"VALUES( '" & ListBox3.Items(i) & "', '" & ListBox1.Items(i)"')"

Then you need to create command object with CommandText = sSQL and call ExecuteNonQuery.
 
Share this answer
 
Comments
Thraxman 20-Apr-11 15:30pm    
losmac, That seemed to do the trick! There's a few quirks when SQL runs across a file that has some funky punctuation in it, but at least i see stuff going into the database! Big Plus! Now I've got something to work with! Thanks!!!!!!!!!

Dim sSQL As String
For i = 0 To ListBox3.Items.Count - 1
sSQL = "INSERT INTO Tracks (TrackTitle, TrackSeqNum) " & vbCrLf & _
"VALUES( '" & (ListBox3.Items(i)) & "', '" & (ListBox1.Items(i)) & "')"
.CommandText = sSQL
objConnection.Open()
.ExecuteNonQuery()
objConnection.Close()
Next
You can use parameterized query like this. Note that parameter are added before for loop and have the value updated in the loop.
VB
objConnection.Open()
           .CommandText = "INSERT INTO Tracks " & _
                    "(TrackTitle, TrackSeqNum) " & _
                    "VALUES(@TrackTitle, @TrackSeqNum )"

           .Parameters.AddWithValue("@TrackTitle", "")
           .Parameters.AddWithValue("@TrackSeqNum", "")
           For i = 0 To ListBox3.Items.Count - 1
               .Parameters("@TrackTitle").Value = ListBox3.Items(i)
               .Parameters("@TrackSeqNum").Value = ListBox1.Items(i)
               .ExecuteNonQuery()
           Next
           objConnection.Close()
 
Share this answer
 
v2
Comments
Thraxman 20-Apr-11 21:05pm    
Costica U, Yeah...I see what you did. Cool! It works great too! Thanks!!!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900