The variable name \'@LockState\' has already been declared. Variable names must be unique within a query batch or stored procedure.
Wh
My answer is basically an extension to other two existing answers (from @gzaxx and @Damith), which both are correct.
You do use the same SqlCommand
instance in each loop iteration, so you do not have to (and should not) perform any initialization operations in the loop.
What others already wrote is that adding parameters' definitions in the loop is the cause of your issue, as in result you are trying to define each of the parameters as many times, as the loop iterates.
What I think you should also consider, is also moving other code out of the loop, which basically means removing this from inside the loop:
string upd = "update card set LockState=@lockstate,
card_descr=@card_descr where [cardNumber] = N'{0}'";
rwd.command.CommandText = up
rwd.command.Parameters.Add(new SqlParameter("@LockState", SqlDbType.NVarChar)).Value =1;
rwd.command.Parameters.Add(new SqlParameter("@card_descr", SqlDbType.NVarChar)).Value = txt_desc2.Text;
rwd.connection.Open();
and placing its equivalent before the loop:
string upd = "update card set LockState=@lockstate,
card_descr=@card_descr where [cardNumber] = N'{0}'";
rwd.command.CommandText = up
SqlParameter lockStateParam = rwd.command.Parameters.Add("@LockState",SqlDbType.NVarChar);
SqlParameter cardDescrParam = rwd.command.Parameters.Add("@card_descr",SqlDbType.NVarChar);
rwd.connection.Open();
You'd also need to replace the way you set parameter values, placing this in the loop, instead of the removed code:
lockStateParam.Value = 1;
cardDescrParam.Value = txt_desc2.Text;
As we moved opening the connection out of the loop, we also need to move the connection closing operation outside of the loop, so this instructions goes out:
rwd.connection.Close();
The entire operation gives us this code block:
string upd = "update card set LockState=@lockstate,
card_descr=@card_descr where [cardNumber] = N'{0}'";
rwd.command.CommandText = up
SqlParameter lockStateParam = rwd.command.Parameters.Add("@LockState",SqlDbType.NVarChar);
SqlParameter cardDescrParam = rwd.command.Parameters.Add("@card_descr",SqlDbType.NVarChar);
rwd.connection.Open();
for (long counter = from; counter <= to; counter++)
{
lockStateParam.Value = 1;
cardDescrParam.Value = txt_desc2.Text;
rwd.command.ExecuteScalar();
}
rwd.connection.Close();
When looking further at this, I see, that each iteration actually uses the very same values for the parameters, so - if it's not a result of simplifying the code for StackOverflow - it means that you basically execute the very same update over and over again.
I'd assume that it actually is a simplified code, as (just as @Damith noted) you seem to be filtering the updated rows by cardNumber
column. What seems a bit strange in that, is that you are apparently using string formatting, to replace the {0}
placeholder with a text. If it's so, then you do have to set the CommandText
attribute inside the loop, but I'd rather consider doing what @Damith suggested, so adding a third command parameter for that purpose.
On a side-note: If the answers provided work for you (and basically all given should), consider accepting one of those (probably the first one that appeared here).