简体   繁体   English

删除并更新所选行

[英]delete and update the selected row

executenonquery is my problem, this code works on other button in different datagridview executenonquery是我的问题,此代码适用于不同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 更新代码仍然执行sa代码,但没有更新它,这是我的Update按钮代码

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); cmd.Parameters.AddWithValue(“ @ CustomerID2”,txtCustomerID2.Text);

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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