-1

i just want to set a variable based on if qno field row is empty or not i.e any entry has been inserted earlier or not

c# code:

cmd2 = new SqlCommand("Select COUNT(*) FROM " + tname + "WHERE qno = @qno", con99);
    cmd2.Parameters.AddWithValue("@qno", qno);
    if ((int)cmd2.ExecuteScalar() == 0) //V Studio shows error here
          qno_present = 0;
    else
        qno_present = 1;

Error:

an exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code Additional information: Incorrect syntax near '='.

Jai hind
  • 85
  • 1
  • 11
  • 2
    You are missing a space after your table name. But really this screams of a poor design when you have to pass in the table name. And potentially a sql injection vulnerability. – Sean Lange Nov 22 '16 at 21:01
  • 1
    MySQL or SQL-Server? They're not the same thing. – Barmar Nov 22 '16 at 21:02

1 Answers1

3
cmd2 = new SqlCommand("Select COUNT(*) FROM " + tname + "WHERE qno = @qno", con99);

You need space before the WHERE clause

cmd2 = new SqlCommand("Select COUNT(*) FROM " + tname + " WHERE qno = @qno", con99);
mybirthname
  • 17,949
  • 3
  • 31
  • 55