无法隐式转换类型'字符串'到' int'使用添加

时间:2014-11-26 16:31:09

标签: c# asp.net

如何修复错误? OrderNumber + AddOrderNumber和OrderNumber = sqlCmd1.ExecuteScalar()上出现错误.ToString();

谢谢,这是代码:

  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();
            }
        }

1 个答案:

答案 0 :(得分:0)

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

OrderNumber属于int,您尝试将string传递给该变量。在Convert.ToInt32上执行sqlCmd1.ExecuteScalar().ToString()

OrderNumber = Convert.ToInt32(sqlCmd1.ExecuteScalar().ToString());