none
UPDATE COMMAND WITH WHERE CLAUSE , RRS feed

  • Question

  • Hi

    I am wriitng a standalone application which updates a table named customer
    I have used a datagridview to display the table
    I have written the foll code but can anyone help me write code for the update statement to update the selected city ....
    ie when the user changes the city, i need to write code for the update stmt to change city
    ============================================================================================
    private void Updatebutton_Click(object sender, EventArgs e)
            {
                try
                {
                   
                  string tip = dataGridView1.SelectedCells[0].Value.ToString();
                  SqlCommand upcom = new SqlCommand("update customer set city = @city1 ;",sqlDataAdapter1.SelectCommand.Connection);

                   

                    upcom.Parameters.Add(new SqlParameter("@city1", SqlDbType.VarChar));
                    upcom.Parameters["@city1"].SourceVersion = DataRowVersion.Current;
                  upcom.Parameters["@city1"].SourceColumn = Northwinddataset1.Tables["customer"].Columns["city"].ToString();

         
                   upcom.Parameters["@city1"].Value = tip;
                  
                    sqlDataAdapter1.UpdateCommand = upcom;
                   sqlDataAdapter1.FillSchema(Northwinddataset1.customer, SchemaType.Source);
                   sqlDataAdapter1.Fill(Northwinddataset1.customer);
                 
                    sqlDataAdapter1.Update(Northwinddataset1,"customer");
                }
    Tuesday, July 15, 2008 9:15 PM

Answers

  • Your SQL Update statement should have a WHERE clause that uniquely identifies the row:

    UPDATE Customer SET City = @City WHERE CustomerID = @CustomerID

    Also, I'm not sure if your DataGridView is using a DataSet source for sqlDataAdapter1 but calling the Update method will apply all changes made to the DataGridView if this is the case and not just your selected row. If you just want to update a single row then you will probably want to use ExecuteNonQuery for the Command object instead.
    Wednesday, July 16, 2008 12:28 PM