C# 从选定的datagridview中删除

C# 从选定的datagridview中删除,c#,C#,好的,我已经编辑了代码,但是现在我收到了这个错误: System.Data.SqlClient.SqlException(0x801319404):语法不正确 在……附近 您没有声明名为“com”的对象 更改com.ExecuteNonQuery()tocomm.ExecuteNonQuery()参考此。它可能会对您有所帮助 您真的“需要删除sql中的数据库”吗??你确定吗?试着注释掉第一个con.Open()行不仅如此。。。。首先他打开con,然后定义conn,打开conn,最后关闭con…

好的,我已经编辑了代码,但是现在我收到了这个错误:

System.Data.SqlClient.SqlException(0x801319404):语法不正确 在……附近

您没有声明名为“com”的对象
更改
com.ExecuteNonQuery()
to
comm.ExecuteNonQuery()

参考此。它可能会对您有所帮助


您真的“需要删除sql中的数据库”吗??你确定吗?试着注释掉第一个
con.Open()行不仅如此。。。。首先他打开con,然后定义conn,打开conn,最后关闭con…完全令人困惑。@DasranRajlui哪一行抛出异常?有很多代码混乱
con.Open()
com.ExecuteNonQuery()连接
sqlstr
!!可能他在其他地方定义了com,就像con和conn一样。您建议的修复方法可能是编译错误。。
private void gridselect(object sender, DataGridViewCellEventArgs e)
{
    chooseAnyRow = e.RowIndex;
    DataGridViewRow row = bndisplay.Rows[chooseAnyRow];
    textBox1.Text = row.Cells[5].Value.ToString();
}

private void button7_Click(object sender, EventArgs e)
{
    DataGridViewRow deleterow = bndisplay.Rows[chooseAnyRow];

    deleterow.Cells[5].Value = textBox1.Text;

    /*com = con.CreateCommand();
    com.CommandType = CommandType.Text;
    com.CommandText = "delete from candidateRegistration where icnumber = '' " + textBox1.Text;
    */

    SqlConnection con;
    SqlCommand com;
    string connstring = @"Data Source=dasranrajlui\sqlexpress;Initial Catalog=SESoriginal;Integrated Security=True";

    con = new SqlConnection(connstring);
    con.Open();
    string sqlstr = "delete from candidateRegistration where icnumber= " + textBox1.Text;

    sqlstr = sqlstr + "" + textBox1.Text + ")";

    com = new SqlCommand(sqlstr, con);

    try
    {
        com.ExecuteNonQuery();
        MessageBox.Show("deleted...");

        textBox1.Text = null;
    }

    catch (Exception EX)
    {
        MessageBox.Show(EX + "NOT deleted");
    }

    finally
    {
        con.Close();
    }

}