Results 1 to 2 of 2

Thread: Problems on DataGrid Update

Threaded View

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Resolved Problems on DataGrid Update

    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.

    Code:
    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();
    }
    Last edited by wey97; Nov 10th, 2004 at 09:52 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width