0

I do this in my sql managment studio

SELECT * FROM SMSMessage WHERE respondCode IS NULL

and I got results

I want to do that query from C#

I tried this:

string query = "SELECT * FROM SMSMessage WHERE respondCode IS @respondCode";
SqlConnection con = new SqlConnection(Utilities.getConnectionString());
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@respondCode", DBNull.Value);

I got this exxception :

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: Incorrect syntax near '@respondCode'.

why please?

I also tried

string query = "SELECT * FROM SMSMessage WHERE respondCode IS NULL";

and I got empty results.

Uupdate

string query = @"SELECT *
    FROM SMSMessage
    WHERE (respondCode = @respondCode)
       OR (@respondCode IS NULL AND respondCode IS NULL)";
            SqlConnection con = new SqlConnection(Utilities.getConnectionString());
            SqlCommand cmd = new SqlCommand(query, con);
            cmd.Parameters.AddWithValue("@respondCode", DBNull.Value);
            con.Open();
            using (con)
            {
                DataTable results = new DataTable();
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                sda.Fill(results);
7
  • 2
    "I also tried string query = "SELECT * FROM SMSMessage WHERE respondCode IS NULL";" and got empty results. Sounds like you are connecting to a different instance/database than the one you are connecting to in SSMS then. Commented Jun 27, 2015 at 11:47
  • no no the same one, i just have one instance, Commented Jun 27, 2015 at 11:48
  • 3
    Then, how did you concluded that you are getting empty result set? Commented Jun 27, 2015 at 11:49
  • Could also be table in different schema. Commented Jun 27, 2015 at 11:51
  • Also, might be the debugger behavior. Did you try to execute 2 lines past sda.Fill(results); and see if you have positive row count? Commented Jun 27, 2015 at 11:55

2 Answers 2

3

Since the NULL values cannot be compared, the NULL = NULL evaluates to unknown, your query needs to be:

SELECT * FROM SMSMessage 
WHERE (respondCode = @respondCode) OR
      (respondCode IS NULL AND @respondCode IS NULL)
Sign up to request clarification or add additional context in comments.

6 Comments

help pleaes, i am getting empty results, though there are results in the datagase
Are you sure you are pointing to the same database? Check your connection string.
yes the same database, i am using datatabel to fill the data and the number of rows = 0
I will try to change the query to check if i get results and update you
i accepted your answer, thanks , i updated my questions, with the code i am using, could you check please why i am geetting empty result? i am using the same database really, i just have one sql instance
|
0

One way of writing this query to behave as you desire would be

SELECT *
FROM   SMSMessage
WHERE  EXISTS (SELECT respondCode
               INTERSECT
               SELECT @respondCode) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.