Hi all,
Here i am using checkbox list control and a button.In button click i
wrote below code to insert values into a table in database.I write this
code in loop because of I am using check box list control....
when i run my webform user can select single check box or multiple
check boxes then when button click i want to insert those checked
values into my table which has only one column
But when inserting from second record its showing below error....
The
variable name '@counselor_name' has already been declared. Variable
names must be unique within a query batch or stored procedure.
Here below is my code....
string st = "";
foreach (ListItem li in Chkcounselor.Items)
{
if (li.Selected)
{
st = li.Text.ToString();
SqlParameter counselorname = new SqlParameter("@counselor_name", SqlDbType.VarChar, 50);
counselorname.Value = st;
cmd.Parameters.Add(counselorname);
cmd.ExecuteNonQuery();
}
}
Please help me out....
thank you.
Problem records insertion through asp.net
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.

Blocked AccountRank #403 · 6.2K reputationPosted Nov 24, 2008, 10:40 PM EST
Hi,
Also create the new SqlCommand object for each list item like that.
SqlCommand cmd = new SqlCommand(query, connection)
Before the line cmd.Parameters.Add(counselorname);
I think it will resolve your problem.satheesh babu86 reputationPosted Nov 24, 2008, 10:48 PM EST
Its working......