wey97
Feb 17th, 2004, 12:14 PM
I have a DataGrid with an Edit/Update/Cancel field but after I click Edit and then click Update I get the following error:
Incorrect syntax near the keyword 'Where'.
On the line -> cmd.ExecuteNonQuery();
Notice I tried adding tics to the Boolean field Admin and the userID field with no luck.
public void DataGrid1_Update(object obj, DataGridCommandEventArgs e)
{
SqlConnection con = new SqlConnection("server=(LOCAL);database=Reading;trusted_connection=true;");
string cmdText = "Update [Users] Set " +
"[UserName]=@UserName, " +
"[FullName]=@FullName, " +
"[Email]=@Email, " +
"[Admin]=@Admin, " +
"Where [UserID]=@UserID";
// tried this also
// string cmdText = "Update [Users] Set " +
// "[UserName]=@UserName, " +
// "[FullName]=@FullName, " +
// "[Email]=@Email, " +
// "[Admin]='@Admin', " +
// "Where [UserID]='@UserID'";
SqlCommand cmd = new SqlCommand(cmdText, con);
string userID = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
cmd.Parameters.Add(new SqlParameter("@UserID", userID));
string userName = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
cmd.Parameters.Add(new SqlParameter("@UserName" , userName));
string fullName = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
cmd.Parameters.Add(new SqlParameter("@FullName" , fullName));
string email = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
cmd.Parameters.Add(new SqlParameter("@Email" , email));
string admin = ((CheckBox)e.Item.Cells[4].Controls[1]).Checked ? "1" : "0";
cmd.Parameters.Add(new SqlParameter("@Admin" , admin));
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
DataGrid1.EditItemIndex = -1;
BindData();
}
Incorrect syntax near the keyword 'Where'.
On the line -> cmd.ExecuteNonQuery();
Notice I tried adding tics to the Boolean field Admin and the userID field with no luck.
public void DataGrid1_Update(object obj, DataGridCommandEventArgs e)
{
SqlConnection con = new SqlConnection("server=(LOCAL);database=Reading;trusted_connection=true;");
string cmdText = "Update [Users] Set " +
"[UserName]=@UserName, " +
"[FullName]=@FullName, " +
"[Email]=@Email, " +
"[Admin]=@Admin, " +
"Where [UserID]=@UserID";
// tried this also
// string cmdText = "Update [Users] Set " +
// "[UserName]=@UserName, " +
// "[FullName]=@FullName, " +
// "[Email]=@Email, " +
// "[Admin]='@Admin', " +
// "Where [UserID]='@UserID'";
SqlCommand cmd = new SqlCommand(cmdText, con);
string userID = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
cmd.Parameters.Add(new SqlParameter("@UserID", userID));
string userName = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
cmd.Parameters.Add(new SqlParameter("@UserName" , userName));
string fullName = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
cmd.Parameters.Add(new SqlParameter("@FullName" , fullName));
string email = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
cmd.Parameters.Add(new SqlParameter("@Email" , email));
string admin = ((CheckBox)e.Item.Cells[4].Controls[1]).Checked ? "1" : "0";
cmd.Parameters.Add(new SqlParameter("@Admin" , admin));
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
DataGrid1.EditItemIndex = -1;
BindData();
}