protected void btnUpdate_Click(object sender, EventArgs e)
{
// create stringbuilder to store multiple DML statements
StringBuilder strsql = new StringBuilder(string.Empty);
// create sqlconnection and command
SqlConnection cn = new SqlConnection(strcn);
SqlCommand cmd = new SqlCommand();
// loop through gridview rows to find checkbox
// and check whether it is cheked or not
for (int i = 0; i < grdview1.Rows.Count; i++)
{
CheckBox chkUpdate = (CheckBox)grdview1.Rows[i].Cells[0].FindControl("chkbox1");
if (chkUpdate != null)
{
if (chkUpdate.Checked)
{
//Get the values of textboxes using FindControl
string strID = grdview1.Rows[i].Cells[1].Text;
string strName = ((TextBox)grdview1.Rows[i].FindControl("txtbox1")).Text;
string strLoc = ((TextBox)grdview1.Rows[i].FindControl("txtloc")).Text;
string strUpdate = "Update Details set Name='" + strName + "'," + " Location='" + strLoc + "'," + " where ID='" + strID + "'," + ";";
strsql.Append(chkUpdate);
}
}
}
try
{
cmd.CommandType=CommandType.Text;
cmd.CommandText=strsql.ToString();
cmd.Connection=cn;
cn.Open();
cmd.ExecuteNonQuery();----------------------------------> Here I got an error
}
catch(SqlException ex)
{
string errorMsg="Error in updation";
errorMsg+=ex.Message;
throw new Exception (errorMsg);
}
finally
{
cn.Close();
}
}
-------------------------------------
protected void checked_changed(object sender, EventArgs e)
{
CheckBox chkTest = (CheckBox)sender;
GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer;
TextBox txtname = (TextBox)grdRow.FindControl
("txtbox1");
TextBox txtlocation = (TextBox)grdRow.FindControl
("txtloc");
if (chkTest.Checked)
{
txtname.ReadOnly = false;
txtlocation.ReadOnly = false;
txtname.ForeColor = System.Drawing.Color.Black;
txtlocation.ForeColor = System.Drawing.Color.Black;
}
else
{
txtname.ReadOnly = true;
txtlocation.ReadOnly = true;
txtname.ForeColor = System.Drawing.Color.Blue;
txtlocation.ForeColor = System.Drawing.Color.Blue;
}
}