Home > Blockchain >  syntax error near '=' -- delete statement
syntax error near '=' -- delete statement

Time:05-09

im trying to add something so that things can be deleted from a table, though it says there is a syntax error near '=' and i cant seem to spot it. i know this isnt the most ideal way to be doing this, but ive been told to do it this way and im on a deadline;; heres what ive put:

Con.Open();
                    string query = "DELETE FROM tablepassengers WHERE passportno.="   tbpassno.Text   ';';
                    SqlCommand cmd = new SqlCommand(query, Con);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("deleted");
                    Con.Close();
                    populate();

CodePudding user response:

As you said the . is meant to be there and that the column name is passportno., this is where your problem is. It's not something that is expected, or recommended, but it is something that can be handled.

When using Sql you really should be using Parameters when constructing Sql statements in code. It is strongly suggested, not only is it good practice it will protect your applications from targetted attacks, to use Parameters -- Please read Why do we always prefer using parameters in SQL statements?

Change your code to look like this:

string query = "DELETE FROM tablepassengers WHERE [passportno.]=@passportNo;";
using (SqlCommand cmd = new SqlCommand(query, Con))
{
    cmd.Parameters.Add(new SqlParameter("passportNo", SqlDbType.VarChar, tbpassno.Text));
    cmd.ExecuteNonQuery();
}
MessageBox.Show("deleted");
Con.Close();

CodePudding user response:

        try
        {
            string query = "DELETE FROM tablepassengers WHERE passportno="   tbpassno.Text;
            SqlCommand cmd = new SqlCommand(query, Con);
            Con.Open();
            cmd.ExecuteNonQuery();
            MessageBox.Show("deleted");

        }
        catch (SqlException ex)
        {
            MessageBox.Show("Error\n"   ex.Message);
        }
        finally
        {
            Con.Close();
        }
  • Related