February 25, 2007 at 12:36 pm
I'm trying to learn how to use command parameters to do my SQL commands with and seemed to have hit an impasse.
I created the following code:
cmd.CommandText = "Insert into PINS (PIN, ControlNumber, BatchID, Consumed) values (@PIN, @Control, @BatchID, @Consumed)"
cmd.Parameters.Add("@PIN", SqlDbType.VarChar).Value = pin.number
cmd.Parameters.Add("@Control", SqlDbType.VarChar).Value = pin.control
cmd.Parameters.Add("@BatchID", SqlDbType.Int).Value = BatchID
cmd.Parameters.Add("@Consumed", SqlDbType.Bit).Value = False
cmd.ExecuteNonQuery()
When the ExecuteNonQuery command runs, I get the following error:
The variable name '@ID' has already been declared. Variable names must be unique within a query batch or stored procedure.
Must declare the scalar variable "@DateStamp".
My problem is that this variable name is not used anywhere else in my code, so it's not duplicate use of the name. What am I doing wrong?
February 25, 2007 at 6:50 pm
It was because I had the cmd.parameter.add statements inside a For loop. I am moving the contents of an array to a DB, so I put it in a loop to step through the array. The problem is that you can't redefine the parameter again until you clear them, so all I had to do is add this command to my loop:
cmd.parameter.clear
There may be a better way, but this works.
February 26, 2007 at 1:09 am
- check the command if parameters exist (hen you've been there already), just alter their values. Avoid the clear.
cmd.Parameters(
"@PIN").Value = pin.number
Johan
Learn to play, play to learn !
Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere :w00t:
- How to post Performance Problems
- How to post data and code to get the best help
- How to prevent a sore throat after hours of presenting ppt
press F1 for solution, press shift+F1 for urgent solution 😀
Need a bit of Powershell? How about this
Who am I ? Sometimes this is me but most of the time this is me
Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply