【发布时间】:2014-06-16 07:53:54
【问题描述】:
我是新手,在运行我的表单时遇到以下错误。 '字符串或二进制数据将被截断。语句已终止。'
这是表单的代码
<form runat="server">
<table style="width:500px">
<tr>
<td>Customer Name*: </td>
<td><asp:TextBox ID="CustomerName" runat="server" MaxLength="50"></asp:TextBox></td>
<td><asp:RequiredFieldValidator id="rfvValidator" runat="server" Enabled="true" ControlToValidate="CustomerName" ErrorMessage="Customer Name is Required." ></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>Address 1: </td>
<td><asp:TextBox ID="Address1" runat="server" MaxLength="100"></asp:TextBox></td>
</tr>
<tr>
<td>Address 2:</td>
<td><asp:TextBox ID="Address2" runat="server" MaxLength="100"></asp:TextBox></td>
</tr>
<tr>
<td>Address 3:</td>
<td><asp:TextBox ID="Address3" runat="server" MaxLength="100"></asp:TextBox></td>
</tr>
<tr>
<td>Town: </td>
<td><asp:TextBox ID="Town" runat="server" MaxLength="100"></asp:TextBox></td>
</tr>
<tr>
<td>Country:</td>
<td><asp:TextBox ID="Country" runat="server" MaxLength="50"></asp:TextBox></td>
</tr>
<tr>
<td>Postcode:</td>
<td><asp:TextBox ID="PostCode" runat="server" MaxLength="8"></asp:TextBox></td>
</tr>
<tr>
<td>Telephone:</td>
<td><asp:TextBox ID="Telephone" runat="server" MaxLength="50"></asp:TextBox></td>
</tr>
<tr>
<td>Fax:</td>
<td><asp:TextBox ID="Fax" runat="server" MaxLength="50"></asp:TextBox></td>
</tr>
</table>
<asp:Button id="btnSave" Text="Save" runat="server" />
<input type=button name="cancel" value="Cancel" onClick="parent.jQuery.fancybox.close()">
<asp:Label ID="Label1" runat="server"></asp:Label>
</form>
这是我的提交按钮代码
Imports System.Data
Imports System.Data.SqlClient
Public Class EditCustomer
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Dim con As SqlConnection
Dim cmd As SqlCommand
Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
con = New SqlConnection("Data Source=IPAddress;Initial Catalog=medical01;Persist Security Info=True;User ID=******;Password=******")
con.Open()
cmd = New SqlCommand("INSERT INTO Customer_tbl (customername,address1,address2,address3,town,country,postcode,telephone,fax,isDeleted) VALUES('" & CustomerName.Text & "','" & Address1.Text & "','" & Address2.Text & "','" & Address3.Text & "','" & Town.Text & "', " & PostCode.Text & ", " & Telephone.Text & "," & Fax.Text & ", 1)", con)
cmd.ExecuteNonQuery()
Label1.Text = "Customer Added."
con.Close()
End Sub
End Class
这是我的数据库:
我收到此错误:
String or binary data would be truncated.
语句已终止。
说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.Data.SqlClient.SqlException:字符串或二进制数据将被截断。 声明已终止。
【问题讨论】:
-
可以将customerid设置为自增
-
不这样认为,如果我从代码中获取 SQL 并添加值并将其直接运行到 SQL 中,则插入工作并分配正确的 ID。