Conversion failed when converting the varchar value 'AccNum' to data type int.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gggram2000
    New Member
    • Nov 2007
    • 97

    #1

    Conversion failed when converting the varchar value 'AccNum' to data type int.

    I need some help, cant figure out the error.
    Code:
    SqlConnection dbConnection = new SqlConnection(xmldb);
    dbConnection.Open();
    string sql = "SELECT InvStatus FROM SaleReceipts Where AccNum = '" + lblAccNum.Text + "' ";
    SqlCommand command = new SqlCommand(sql, dbConnection);
    SqlDataReader reader = command.ExecuteReader();
    while (reader.Read())
    {
             InvoiceStatus = reader[0].ToString();                 
    }
    reader.Close();            
    dbConnection.Close();
    I get this error:
    Conversion failed when converting the varchar value 'AccNum' to data type int.

    I have checked in the database and AccNum is datatype int, and InvStatus datatype varchar, If there's something I'm missing or something else I should check i'd appreciate the help. Thanks
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    Change the Select statement to look like this

    Code:
    string sql = "SELECT InvStatus FROM SaleReceipts Where AccNum = '" + Convert.ToInt32(lblAccNum.Text) + "' ";
    Nathan

    Comment

    • gggram2000
      New Member
      • Nov 2007
      • 97

      #3
      Originally posted by nateraaaa
      Change the Select statement to look like this

      Code:
      string sql = "SELECT InvStatus FROM SaleReceipts Where AccNum = '" + Convert.ToInt32(lblAccNum.Text) + "' ";
      Nathan
      Thanks, but when I try that I get this error:
      "Input string was not in a correct format."

      Comment

      • nateraaaa
        Recognized Expert Contributor
        • May 2007
        • 664

        #4
        Originally posted by gggram2000
        Thanks, but when I try that I get this error:
        "Input string was not in a correct format."
        What is the value of AccNum.Text? Is the value numeric?

        If so try this
        Code:
        string sql = "SELECT InvStatus FROM SaleReceipts Where AccNum = " + Convert.ToInt32(lblAccNum.Text);

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          The problem is that you are surrounding the param with single quotes. In SQL, that represents a varchar value, a string. Change your select statement to this:
          Code:
          string sql = "SELECT InvStatus FROM SaleReceipts Where AccNum = " + lblAccNum.Text;
          Better yet, learn how to use String.Format"
          Code:
          string sql = String.Format("SELECT InvStatus FROM SaleReceipts Where AccNum = {0}", lblAccNum.Text);
          The best way would be to use a parameterized query.
          Code:
          string sql = "SELECT InvStatus FROM SaleReceipts Where AccNum = @AccNum";
          SqlCommand command = new SqlCommand(sql, dbConnection);
          command.Parameters.AddWithValue("@AccNum", lblAccNum.Text);
          This one is the most secure, and the easiest to understand just by looking at it.

          @nateraaaa,
          You wouldn't have to convert the text value to an int to put it back into a string. The original error was coming from the DB engine.

          Comment

          • gggram2000
            New Member
            • Nov 2007
            • 97

            #6
            Originally posted by insertAlias
            The problem is that you are surrounding the param with single quotes. In SQL, that represents a varchar value, a string. Change your select statement to this:
            Code:
            string sql = "SELECT InvStatus FROM SaleReceipts Where AccNum = " + lblAccNum.Text;
            Better yet, learn how to use String.Format"
            Code:
            string sql = String.Format("SELECT InvStatus FROM SaleReceipts Where AccNum = {0}", lblAccNum.Text);
            The best way would be to use a parameterized query.
            Code:
            string sql = "SELECT InvStatus FROM SaleReceipts Where AccNum = @AccNum";
            SqlCommand command = new SqlCommand(sql, dbConnection);
            command.Parameters.AddWithValue("@AccNum", lblAccNum.Text);
            This one is the most secure, and the easiest to understand just by looking at it.

            @nateraaaa,
            You wouldn't have to convert the text value to an int to put it back into a string. The original error was coming from the DB engine.
            Yeah it's much easier to understand. It worked perfect, thanks a lot for ur input! Appreciate it!

            --George

            Comment

            • Curtis Rutland
              Recognized Expert Specialist
              • Apr 2008
              • 3264

              #7
              Glad to be of service =D

              Comment

              • nateraaaa
                Recognized Expert Contributor
                • May 2007
                • 664

                #8
                If the DB expects AccNum to be an int then the value set for the @AccNum must be an int. That is why I suggested using the Convert.ToInt32 on AccNum.Text. There is also built in error handling here because if AccNum.Text is not a valid int the Convert.ToInt32 operator will throw an exception.

                Nathan

                Comment

                • Curtis Rutland
                  Recognized Expert Specialist
                  • Apr 2008
                  • 3264

                  #9
                  Good point, I was assuming he had already verified that the text was an int somewhere else.

                  Comment

                  • gggram2000
                    New Member
                    • Nov 2007
                    • 97

                    #10
                    Originally posted by insertAlias
                    Good point, I was assuming he had already verified that the text was an int somewhere else.
                    Yeah I did, when I said AccNum is dataType int in the database and was therefore viewing it as that on lblAccNum.text. Cheers!

                    Comment

                    Working...