简体   繁体   中英

delete and update the selected row

executenonquery is my problem, this code works on other button in different datagridview

here's my code at delete button

private void button4_Click_2(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection(@"Data Source=XXYZZ\SQLEXPRESS;Initial Catalog=rick_inventiory;Integrated Security=True");
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "Delete from tbl_Orders where CustomersID2 = '" + dataGridView5.SelectedRows[0].Cells[0].Value.ToString() + "'";
        con.Open();
        cmd.Parameters.AddWithValue("@CustomerID2", txtCustomerID2.Text);
        cmd.ExecuteNonQuery();
        con.Close();

        disp_data();

        MessageBox.Show("Deleted Successfully");


    }               

the update code still execute sa code but did not update it and heres my code for Update button

SqlConnection con = new SqlConnection(@"Data Source=XXYZZ\SQLEXPRESS;Initial Catalog=rick_inventiory;Integrated Security=True");

        con.Open();
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "Update  tbl_Products SET ProductName='" + txtProName.Text +
            "',Stocks='" + txtStocks.Text + "',Price='" + txtPrice.Text + "',Description='" +
            txtDesc.Text + "',CategoryName='" + txtCat.Text + "' where ProductID ='" + txtProID.Text + "';";
        cmd.ExecuteNonQuery();


        SqlDataAdapter da = new SqlDataAdapter("Select * from tbl_Products", con);
        DataTable dt = new DataTable();
        da.Fill(dt);

        dataGridView1.DataSource = dt;

        MessageBox.Show("Successfuly Updated");
        con.close();

In update there is a syntax problem remove inner side semi colon of update query

While in delete you want to change the line

from

cmd.Parameters.AddWithValue("@CustomerID2", txtCustomerID2.Text);

to

cmd.Parameters.AddWithValue("@CustomerID2", '" + dataGridView5.SelectedRows[0].Cells[0].Value.ToString() + "');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM