从表单过帐中删除按钮

时间:2011-01-26 09:20:16

标签: c# asp.net database sqldatareader sql-delete

我有一张AVUKAT表 Columns--> HESAP, MUSTERI, AVUKAT

这是我的删除按钮代码

protected void Delete_Click(object sender, EventArgs e)
        {
            string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString;

            SqlConnection myConnection = new SqlConnection(strConnectionString);
            myConnection.Open();


            string hesap = Label1.Text;
            string musteriadi = DropDownList1.SelectedItem.Value;
            string avukat = DropDownList2.SelectedItem.Value;

            SqlCommand cmd = new SqlCommand("DELETE FROM AVUKAT WHERE (@HESAP, @MUSTERI, @AVUKAT)", myConnection);

            cmd.Parameters.AddWithValue("@HESAP", hesap);
            cmd.Parameters.AddWithValue("@MUSTERI", musteriadi);
            cmd.Parameters.AddWithValue("@AVUKAT", avukat);
            cmd.Connection = myConnection;

            SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            Grid_Goster.DataSource = dr;
            Grid_Goster.Visible = true;
            myConnection.Close();
        }

但它会产生这样的错误。

Server Error in '/' Application.
An expression of non-boolean type specified in a context where a condition is expected, near ','.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: An expression of non-boolean type specified in a context where a condition is expected, near ','.
Source Error:
Line 83:             cmd.Connection = myConnection;
Line 84: 
Line 85:             SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
Line 86:             Grid_Goster.DataSource = dr;
Line 87:             Grid_Goster.Visible = true;

我找不到错误.. 在哪里?

3 个答案:

答案 0 :(得分:1)

您的sql命令目前是DELETE FROM AVUKAT WHERE (@HESAP, @MUSTERI, @AVUKAT)

这需要根据实际表格更改为有效的删除命令。根据您上面所写的内容,它需要是:

DELETE FROM AVUKAT 
WHERE HESAP = @HESAP AND 
      MUSTERI = @MUSTERI AND 
      AVUKAT = @AVUKAT

答案 1 :(得分:0)

您的SQL错误,您没有给出布尔条件来指定应删除哪些行。

如果您想根据所有这些参数进行删除:

DELETE FROM AVUKAT 
WHERE HESAP = @HESAP
AND MUSTERI = @MUSTERI 
AND AVUKAT = @AVUKAT

或者,如果您想根据这些参数中的任何一个进行删除:

DELETE FROM AVUKAT 
WHERE HESAP = @HESAP
OR MUSTERI = @MUSTERI 
OR AVUKAT = @AVUKAT

答案 2 :(得分:-2)

您需要在SQL语句中提供布尔表达式。例如:

SqlCommand cmd = new SqlCommand("DELETE FROM AVUKAT WHERE @HESAP = 'foobar' AND @MUSTERI > 0 AND @AVUKAT <> 42")