none
Run C# statement Based Off of SQL Server Field Value RRS feed

  • Question

  • I have a SQL Server Table that contains a value of either 0 or -1.  And I have a C# statement set to read like so.  BUT it didn't execute and I was curious if it's becauause I have my C# code set to be "1" and it needs to be "-1"  Can someone with more knowledge let me know?

    //Connection String & other variables established above
    string query = "Select * from databaseAlpha WHERE Needed = @MT ORDER BY LastName ASC";
    
    SqlCommand cmd = new SqlCommand(queryString, connection)
    connection.Open();
    if (DateTime.Now.Hour >= 15)
    {
    cmd.Parameters.AddWithValue("@MT", 1)
    }
    else
    {
    cmd.Parameters.AddWithValue("@MT", 0);
    }
    //Do More Here
    

    Monday, November 18, 2013 9:10 PM

Answers

  • Are you asking

    "If the database contains only zero and minus one what will be returned if I ask for entries containing one"?

    Can I rephrase

    "If the database has 'Smith' and 'Jones' in it, what will I get if I ask for 'Brown'"?

    Am I understanding your question correctly?


    Paul Linton

    Tuesday, November 19, 2013 4:33 AM

All replies

  • Hi Joe Marx's,

    When attempting to run your code, have you run cmd.ExecuteReader() or cmd.ExecuteScalar()?

    Regards,

    Luke

    Monday, November 18, 2013 9:16 PM
  • Hi Joe Marx's,

    When attempting to run your code, have you run cmd.ExecuteReader() or cmd.ExecuteScalar()?

    Regards,

    Luke


    cmd.ExecuteReader();
    Monday, November 18, 2013 9:25 PM
  • If the table only has the values 0 and -1 in it and you search for 1 then you are not going to find any rows.  The number one is not the same as the number minus one.

    Is that what you are asking?

    By the way, when you write things like "BUT it didn't execute" that tells us very little.  It was not surprising to me that it didn't execute because there was no Execute statement!  But later I see that you mention an ExecuteReader statement.  Maybe it "Didn't execute" because a previous line threw an exception, maybe an "if" statement that you haven't shown stopped execution, maybe your connection string is wrong, maybe the table doesn't exist in the database that you connection string points to, maybe there are no rows in the table, maybe the code doesn't compile, maybe you get an error because your SQL is wrong (databaseAlpha is an odd name for a table), etc, etc, etc.  Without giving details about what "didn't execute" means precisely with ALL relevant code, all returned error details, all exception details, all results expected, all actual results, contents of the table being accessed, etc it is only guessing on our part.

    At the absolute minimum you need to tell us what "Didn't execute" means.  My guess is that it does actually execute.  My guess is that you are not getting the results you are expecting - just because an SQL statement returns zero rows does not mean that it didn't execute.  But this is all just guessing.


    Paul Linton

    Monday, November 18, 2013 10:31 PM
  • If the table only has the values 0 and -1 in it and you search for 1 then you are not going to find any rows.  The number one is not the same as the number minus one.

    Is that what you are asking?

    By the way, when you write things like "BUT it didn't execute" that tells us very little.  It was not surprising to me that it didn't execute because there was no Execute statement!  But later I see that you mention an ExecuteReader statement.  Maybe it "Didn't execute" because a previous line threw an exception, maybe an "if" statement that you haven't shown stopped execution, maybe your connection string is wrong, maybe the table doesn't exist in the database that you connection string points to, maybe there are no rows in the table, maybe the code doesn't compile, maybe you get an error because your SQL is wrong (databaseAlpha is an odd name for a table), etc, etc, etc.  Without giving details about what "didn't execute" means precisely with ALL relevant code, all returned error details, all exception details, all results expected, all actual results, contents of the table being accessed, etc it is only guessing on our part.

    At the absolute minimum you need to tell us what "Didn't execute" means.  My guess is that it does actually execute.  My guess is that you are not getting the results you are expecting - just because an SQL statement returns zero rows does not mean that it didn't execute.  But this is all just guessing.


    Paul Linton


    Wow, I didn't realize all the possible scenarios of what could have caused it to not execute.  I apologize for not being descriptive enough.  I just wasn't sure if since the database holds either a 0 or -1 value if that is what the value of @MT should be also, either a @MT = 0 or @MT = -1.
    Tuesday, November 19, 2013 4:28 AM
  • Are you asking

    "If the database contains only zero and minus one what will be returned if I ask for entries containing one"?

    Can I rephrase

    "If the database has 'Smith' and 'Jones' in it, what will I get if I ask for 'Brown'"?

    Am I understanding your question correctly?


    Paul Linton

    Tuesday, November 19, 2013 4:33 AM
  • Hi Joe,

    Thanks for your posting.

    From your description, I know that this issue is related to SQL Server Table, I think that Data Platform is more suitable for it. For better help, I move it to Data Platform Development forum.

    Hope these help.


    Lilia Gong <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, November 21, 2013 5:28 AM