Learn how to a build a cloud-first strategyRegister Now


SQL Command with Parameters, Loop?

Posted on 2006-05-01
Medium Priority
Last Modified: 2012-08-13

I am using Several SQL Commands, all of which have parameters:

            Command1.Parameters.Add("@param1", System.Data.SqlDbType.Varchar)
            Command1.Parameters("@param1").Value = "test"

I want to be able to run these commands more than once by using a loop....First pass thru runs fine, but then when it tries to loop I get the following error:

"The variable name '@param1' has already been declared. Variable names must be unique within a query batch or stored procedure."

How can I reset these so I can loop thru and run the same commands with the parameters reset ? Thanks for all your help !
Question by:kasowitz
LVL 34

Accepted Solution

flavo earned 2000 total points
ID: 16582726
            Command1.Parameters.Add("@param1", System.Data.SqlDbType.Varchar)
            Command1.Parameters("@param1").Value = "test"


Author Comment

ID: 16582753
LVL 10

Expert Comment

ID: 16582770

Dim C_Command As SqlCommand
For i = 0 to 10
            sql = "DELETE FROM Table1 WHERE (Code = @Code)"
            C_Command = New SqlCommand(sql, C_connection)
            C_Command.Parameters.Add("@Code", SqlDbType.VarChar, 16).Value = i.ToString


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Screencast - Getting to Know the Pipeline
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month21 days, 6 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question