-7

How do I fix the error? the error appears on OrderNumber + AddOrderNumber and on line OrderNumber = sqlCmd1.ExecuteScalar().ToString();

Thanks here is the code:

  int AddOrderNumber = 1;
        int OrderNumber;


        TextBox1.Text = OrderNumber + AddOrderNumber;
        TextBox2.Text = DateTime.Now.ToString("MM-dd-yyyy");
        TextBox3.Text = (string)Session["name"];


        using (SqlConnection con6 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString))
        {

            //Grab Customer #
            using (SqlCommand sqlCmd1 = new SqlCommand { CommandText = "SELECT TOP 1 [OrderNumber] FROM [datarep].[dbo].[OrderHeader] Order By [OrderNumber] DESC", Connection = con6 })
            {
                con6.Open();
                object result = sqlCmd1.ExecuteScalar();

                if (result != null)
                {
                    OrderNumber = sqlCmd1.ExecuteScalar().ToString();

                }
                else
                {

                }
                con6.Close();
            }
        }
  • 4
    Just read what your compiler tells you. The error tells you exactly all you need to know in this case. – S_F Nov 26 '14 at 16:32
  • is the OrderNumber stored as a number or a string in the database? and if you are planning to do math on it, why use `.ToString()` at all? – Claies Nov 26 '14 at 16:33
  • its stored as an int – Jerry large Nov 26 '14 at 16:34
  • just remove the `.ToString()` from the `ExecuteScalar()`. there is no reason to convert the value from the database at all. – Claies Nov 26 '14 at 16:37

1 Answers1

0
OrderNumber = sqlCmd1.ExecuteScalar().ToString();

OrderNumber is of type int and you're trying to pass in a string to that variable. Do a Convert.ToInt32 on sqlCmd1.ExecuteScalar().ToString().

OrderNumber = Convert.ToInt32(sqlCmd1.ExecuteScalar().ToString());
Drew Kennedy
  • 4,118
  • 4
  • 24
  • 34