I got an error:ExecuteNonQuery: CommandText property has not been initialized [Resolved]

Posted by maninaanee-8287 under ASP.NET on 12/9/2011 | Points: 10 | Views : 12795 | Status : [Member] | Replies : 6
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;
}

}




Responses

Posted by: maninaanee-8287 on: 12/9/2011 [Member] Starter | Points: 25

Up
0
Down

Resolved
Hi varun,
I got an error. Error in updationIncorrect syntax near the keyword 'where'.

Thanks&Regards,
padma

maninaanee-8287, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Varung on: 12/9/2011 [Member] Starter | Points: 25

Up
0
Down
replace
strsql.Append(chkUpdate);
at
strsql.Append(strUpdate);

G.Varun

maninaanee-8287, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Varung on: 12/9/2011 [Member] Starter | Points: 25

Up
0
Down

string strUpdate = "Update Details set Name='" + strName + "', Location='" + strLoc + "' where ID='" + strID + "'";

replace this code
if ID is int in your database remove single quotes for strID else keep it as it is

G.Varun

maninaanee-8287, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Varung on: 12/9/2011 [Member] Starter | Points: 25

Up
0
Down
error cleared?????

G.Varun

maninaanee-8287, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: maninaanee-8287 on: 12/9/2011 [Member] Starter | Points: 25

Up
0
Down
o.k my problem is solved.
Thank You so much

maninaanee-8287, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Varung on: 12/9/2011 [Member] Starter | Points: 25

Up
0
Down
always welcome.....

G.Varun

maninaanee-8287, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response