转换varchar值

时间:2016-05-31 12:33:45

标签: mysql asp.net type-conversion

我打算从另一个表中向我的文本框插入一个特定值。执行此操作时,它给出了一个错误“转换失败时转换varchar值'5,000到数据类型int。在我的数据库中,creditrequest列(5000)是char,目标列是int。有什么技巧吗?

这是我的代码:

protected void Button2_Click(object sender, EventArgs e)
    {
        using (SqlConnection scn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True"))
        {
            scn.Open();
            SqlCommand cmd = new SqlCommand("INSERT INTO UserData(CreditRequest) Select CreditRequest FROM CreditRequests Where Username=@Username", scn);
            cmd.Parameters.Add("@Username", SqlDbType.NVarChar).Value = Session["New"];

            object value = cmd.ExecuteScalar();

            if (value != null)
                txtCredit.Text = value.ToString();
        }

错误在于

object value = cmd.ExecuteScalar();

1 个答案:

答案 0 :(得分:0)

INSERT INTO UserData(CreditRequest)  SELECT CAST(REPLACE(CreditRequest, ',', '') as int) FROM CreditRequests Where Username=@Username