任何人都可以对我的问题有所了解吗?我有这个表单代码,它应该采用文本框的文本并将其放在数据库中。但是,我收到错误SqlException was unhandled by user code: String or binary data would be truncated. The statement has been terminated.
以下是代码:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Partial Class Usuarios
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub BotonA_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim iDUsuario As String = RFC.ToString
Dim nomUsuario As String = Name.ToString
Dim apellidoP As String = ApellidoPaterno.ToString
Dim apellidoM As String = ApellidoMaterno.ToString
Dim passUsuario As String = contrasena.ToString
Dim nombre As String = seudonimo.ToString
Dim iDtipo As Integer = "1"
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("dbconnection").ConnectionString)
con.Open()
Dim cmd As New SqlCommand("INSERT INTO usuarios (iDUsuario, nomUsuario, apellidoP, apellidoM, passUsuario, nombre, idTipo) VALUES (@iDUsuario, @nomUsuario, @apellidoP, @apellidoM, @passUsuario, @nombre, @iDtipo)", con)
cmd.Parameters.AddWithValue("@iDUsuario", iDUsuario)
cmd.Parameters.AddWithValue("@nomUsuario", nomUsuario)
cmd.Parameters.AddWithValue("@apellidoP", apellidoP)
cmd.Parameters.AddWithValue("@apellidoM", apellidoM)
cmd.Parameters.AddWithValue("@passUsuario", passUsuario)
cmd.Parameters.AddWithValue("@nombre", nombre)
cmd.Parameters.AddWithValue("@iDtipo", iDtipo)
cmd.ExecuteNonQuery()
End Sub
End Class
我读过许多像我这样的问题和问题,但我不知道如何解决这个问题。
答案 0 :(得分:1)
这意味着您的输入大于SQL中声明的列长度。调整VARCHAR(或任何CHAR类型字段)的长度以适应最大可能的输入长度。
答案 1 :(得分:0)
Dim nomUsuario As String = Name.ToString
您确定名称不是字符串吗?
Dim iDtipo As Integer = "1"
应为Dim iDtipo As Integer = 1
如果您的“iDtipo”字段是VARCHAR
cmd.Parameters.AddWithValue("@iDtipo", iDtipo.ToString)
如果是数字
cmd.Parameters.AddWithValue("@iDtipo", iDtipo)
答案 2 :(得分:0)
我做到了:这是错的:
而不是:
Dim iDUsuario As String = RFC.ToString
Dim nomUsuario As String = Name.ToString
Dim apellidoP As String = ApellidoPaterno.ToString
Dim apellidoM As String = ApellidoMaterno.ToString
Dim passUsuario As String = contrasena.ToString
Dim nombre As String = seudonimo.ToString
Dim iDtipo As Integer = "1"
是:
Dim iDUsuario As String = RFC.Text
Dim nomUsuario As String = Name.Text
Dim apellidoP As String = ApellidoPaterno.Text
Dim apellidoM As String = ApellidoMaterno.Text
Dim passUsuario As String = contrasena.Text
Dim nombre As String = seudonimo.Text
Dim iDtipo As Integer = 1
因为我不告诉编译器字符串变量会存储文本框的字符串值,所以我很傻XD