字符串或二进制数据将被截断。该语句已终止。?

时间:2015-03-26 05:06:01

标签: .net vb.net sql-server-2005

这是我的代码。当我执行时,我得到一个错误,如下面给出

If Val(dr("USE_Amount").ToString) >= Val(txtprice.Text) Then
                    dr.Close()
                    msg = "Ref No : " + ref_no + ". Your mobile no : " + txt_mob.Text + " Talk time : " + txttalk.Text + " Validity : " + txtval.Text
                    cmd = New SqlCommand("update TBL_Banking set USE_Amount=USE_Amount-" + txtprice.Text + " where USE_creditcard='" + txt_credit.Text + "' and USE_pin='" + txt_pwd.Text + "' and USE_uname='" + HiddenField1.Value + "' and USE_Bank='" + ddl_bank.SelectedItem.Text + "'", con)
            cmd.ExecuteNonQuery()
            cmd = New SqlCommand("insert into TBL_Recharge values('" + Now.ToString + "','" + txt_mob.Text + "','" + dvendor.SelectedItem.Text + "','" + txt_credit.Text + "','" + ddl_bank.SelectedItem.Text + "'," + txtprice.Text + ",'" + HiddenField1.Value + "','" + msg + "')", con)
            cmd.ExecuteNonQuery()
            lbl_msg.Text = "Recharge sucessfully"

            MsgBox("Recharge Message Successfully send to mobile no : " + txt_mob.Text)
            txt_mob.Text = ""
            txt_credit.Text = ""
            txt_pwd.Text = ""
            txt_pwd.Text = ""
        Else
            dr.Close()
            lbl_msg.Text = "Unable to Recharge..Invalid Balance"
        End If

错误消息为string or binary data would be truncated.

2 个答案:

答案 0 :(得分:1)

基本上,其中一个插入值的大小超过了其各自列的大小。此外,您应该使用参数化查询,否则: - 1)您的代码将容易受到SQL注入攻击,并且 2)文本框中的某些字符会给你例外。例如,尝试写作" abc' d"在你的txt_mob中。

答案 1 :(得分:0)

当您尝试将一些值插入数据长度超过实际列长度或容量的数据库列时,会出现错误String or binary data would be truncated。如果我们将一个包含51个字符的字符串插入到具有最大长度的列中50,发生同样的错误。

检查数据库中的相同方案。