Search code examples
c#asp.netdatabasesqldatareadersql-delete

Delete Button from Form Posting


I have a table AVUKAT Columns--> HESAP, MUSTERI, AVUKAT

This is my delete button code

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();
        }

But it generates an error like this.

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;

I can't find the error.. Where is?


Solution

  • Your sql command currently is DELETE FROM AVUKAT WHERE (@HESAP, @MUSTERI, @AVUKAT)

    This needs to be changed to be a valid Delete command based on the actual table. Based on what you wrote above, it would need to be:

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