SQL Command with Parameters, Loop?

Posted on 2006-05-01
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

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


    Author Comment

    LVL 10

    Expert Comment


    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    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 ( Literal, only instead of starting and ending with w…
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now