0

i want to save each row which checked using sqldatasource, but i got error The variable name '@Approved_By' has already been declared. Variable names must be unique within a query batch or stored procedure.

am i wrong in looping?

int i = 0;
foreach (GridViewRow row in GridView1.Rows)
{
    if (row.RowType == DataControlRowType.DataRow)
    {
        CheckBox chkRow = (row.Cells[2].FindControl("CheckBox1") as CheckBox);
        bool chk = chkRow.Checked;

        if (chk = chkRow.Checked)
        {
            SqlDataSource3.UpdateParameters.Add("Approved_By", Session["username"].ToString());
            SqlDataSource3.UpdateParameters.Add("Kode_Personel", GridView1.Rows[row.RowIndex].Cells[0].Text);
            SqlDataSource3.Update();
        }
    }
    i++;
}
4
  • Think the variable @Approved_By declared more than one time, please check it Commented Nov 16, 2017 at 4:16
  • yes i need to update everi checked row with variable @Approved_By. so im using foreach? if just one checked its okay, but if 2 or more checked get error message.. am i wrong in foreach looping? Commented Nov 16, 2017 at 4:21
  • What about executing SqlDataSource3.UpdateParameters.Clear() before adding UpdateParameters in foreach loop? Is that work? Commented Nov 16, 2017 at 4:22
  • thanks alot Tetsuya, its work Commented Nov 16, 2017 at 4:27

2 Answers 2

2

The variable name has already been declared message is pretty obvious: you're adding same parameter names in foreach loop multiple times for every iteration.

Add SqlDataSource3.UpdateParameters.Clear(); method either before adding UpdateParameters or after executing Update() method to clear parameter collection before next iteration starts:

int i = 0;
foreach (GridViewRow row in GridView1.Rows)
{
    if (row.RowType == DataControlRowType.DataRow)
    {
        CheckBox chkRow = (row.Cells[2].FindControl("CheckBox1") as CheckBox);
        bool chk = chkRow.Checked;

        if (chk = chkRow.Checked)
        {
            // add this line so that UpdateParameter collection cleared before adding new parameters
            SqlDataSource3.UpdateParameters.Clear();

            SqlDataSource3.UpdateParameters.Add("Approved_By", Session["username"].ToString());
            SqlDataSource3.UpdateParameters.Add("Kode_Personel", GridView1.Rows[row.RowIndex].Cells[0].Text);

            SqlDataSource3.Update();

            // or you can clear UpdateParameters collection here
        }

    }
    i++;
}
Sign up to request clarification or add additional context in comments.

Comments

0
protected void btn_insert_Click(object sender, EventArgs e)
        {
           
            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
            {
                GridViewRow row = GridView1.Rows[i];
                CheckBox Chbox = (CheckBox)row.FindControl("chb1");
                if (Chbox.Checked == true)
                {
                    select++;
                }
            }

            if (select == 0)
            {
                Page.RegisterStartupScript("Alert Message", "<script language='javascript'>alert('Please check one checkbox records');</script>");
                return;
            }

            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
            {
                string sid = GridView1.Rows[i].Cells[1].Text;
                string sname = GridView1.Rows[i].Cells[2].Text;
                string smarks = GridView1.Rows[i].Cells[3].Text;
                string saddress = GridView1.Rows[i].Cells[4].Text;
                GridViewRow row = GridView1.Rows[i];
                CheckBox Chbox = (CheckBox)row.FindControl("chb1");
                if (Chbox.Checked == true)
                {
                    InsertData(sid, sname, smarks, saddress);
                }
            }
            Response.Write("Record inserted successfully");
            }

        void InsertData(String sid, String sname, String smarks,string saddress)
        {
            SqlConnection con = new SqlConnection(connStr);
            try
            {
                con.Open();
                com = new SqlCommand("insert into student values('" + sid + "','" + sname + "','" + smarks + "','" + saddress + "')", con);
                com.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }  
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.