samuel henk
samuel henk

Reputation: 13

save selected checkbox in gridview row c# asp

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++;
}

Upvotes: 0

Views: 1328

Answers (2)

Code
Code

Reputation: 739

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());
            }
        }  
    }
}

Upvotes: 0

Tetsuya Yamamoto
Tetsuya Yamamoto

Reputation: 24957

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++;
}

Upvotes: 2

Related Questions