locked
ExecuteScalar() gives error, what is wrong? RRS feed

  • Question

  • User590250825 posted

    When I try to run this code I get the following error: An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

    This is my code:

    protected void ButtonAdd_Click(object sender, EventArgs e)
            {
                SqlConnection con = new SqlConnection(strcon);
                string Id = DropDownListB.DataValueField;
                SqlCommand cmd = new SqlCommand("SELECT Price FROM BooksInfo WHERE ISBN No=@Id", con);
                cmd.Parameters.AddWithValue("@Id", Id);
                con.Open();
                string test = (String)cmd.ExecuteScalar();
                LabelQuantity.Text = test;
                con.Close();
            }

    Sunday, February 16, 2014 5:53 PM

Answers

  • User281315223 posted

    Could the issue here actually be related to your SQL statement?

    You currently are using column "ISBN No", however you might want to try this by using an underscore or square brackets as seen below to see if that helps :

    SqlCommand cmd = new SqlCommand("SELECT Price FROM BooksInfo WHERE ISBN_No = @Id", con);

    or :

    SqlCommand cmd = new SqlCommand("SELECT Price FROM BooksInfo WHERE [ISBN No] = @Id", con);
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 16, 2014 6:21 PM
  • User590250825 posted

    Just before this answer I changed it to just "ISBN" Cus I was thinking the same thing, and it worked. Thanks for the help

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 16, 2014 6:27 PM

All replies

  • User281315223 posted

    Do you have any additional information about your exception? Such as an InnerMessage for the exception itself?

    You might try slightly revising your current code as follows to see if it makes any noticable difference :

    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
         using(SqlConnection con = new SqlConnection(strcon))
         {
              // Open your connection
              con.Open();
    
              // Build your command
              SqlCommand cmd = new SqlCommand("SELECT Price FROM BooksInfo WHERE ISBN No=@Id", con);
    
              // Add your parameter
              cmd.Parameters.AddWithValue("@Id", DropDownListB.DataValueField);
     
              // Attempt to cast your scalar value (an object) as a string
              string test = cmd.ExecuteScalar().ToString();
    
              // Store the result in your Textbox
              LabelQuantity.Text = test;
         }
    }

    You might try storing your scalar within a generic variable and using the debugger to see what the values is (to see if that can help determine exactly what is going wrong) :

    var result = cmd.ExecuteScalar();

    Sunday, February 16, 2014 6:10 PM
  • User590250825 posted

    Hi, thanks for the help, unfortunately same problem consists.

    The only extra info I get is:

    {"An expression of non-boolean type specified in a context where a condition is expected, near 'No'."}

    (The error accurs at ExecuteScalar();)

    Sunday, February 16, 2014 6:13 PM
  • User281315223 posted

    Could the issue here actually be related to your SQL statement?

    You currently are using column "ISBN No", however you might want to try this by using an underscore or square brackets as seen below to see if that helps :

    SqlCommand cmd = new SqlCommand("SELECT Price FROM BooksInfo WHERE ISBN_No = @Id", con);

    or :

    SqlCommand cmd = new SqlCommand("SELECT Price FROM BooksInfo WHERE [ISBN No] = @Id", con);
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 16, 2014 6:21 PM
  • User590250825 posted

    Just before this answer I changed it to just "ISBN" Cus I was thinking the same thing, and it worked. Thanks for the help

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 16, 2014 6:27 PM
  • User281315223 posted

    No problem.

    Glad that you got it all taken care of (I figured since the error message explicitly mentioned the 'No' that there was an issue with the naming of the column).

    Monday, February 17, 2014 7:40 AM