0

Can anyone cast some light on my problem? I have this form code and which is supposed to take the text of the textbox and put it in the database. I am, however, getting an error, SqlException was unhandled by user code: String or binary data would be truncated. The statement has been terminated.

Here is the code:

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

I've read many questions and problems like mine but I haven't any clue how I might resolve this.

Translunar
  • 3,739
  • 33
  • 55
DarkVader
  • 41
  • 1
  • 6

3 Answers3

1

It means your input is larger than the column declared length in SQL. Adjust the VARCHAR (or whatever CHAR type field you have)'s length to accommodate your maximum possible input length.

Haney
  • 32,775
  • 8
  • 59
  • 68
  • i've done it but in the aspx page, but i think you are suggesting that i do that in the .vb file also??? like: Dim iDUsuario As String = RFC.ToString (0,50) – DarkVader May 30 '13 at 16:24
  • Anywhere that it is references, you can restrict the length... You can also increase the length permitted to the DB. – Haney May 30 '13 at 18:39
0
Dim nomUsuario As String = Name.ToString

Are you sure your Name not string ?

Dim iDtipo As Integer = "1"

It should be Dim iDtipo As Integer = 1

If your "iDtipo" field is VARCHAR

cmd.Parameters.AddWithValue("@iDtipo", iDtipo.ToString)

If numeric

cmd.Parameters.AddWithValue("@iDtipo", iDtipo)
matzone
  • 5,703
  • 3
  • 17
  • 20
0

I made it: this is what was wrong:

instead of:

    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"

It is:

    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

Because i wans't telling to the compiler that the string variables will store the strings values of the textboxes, silly of me XD

DarkVader
  • 41
  • 1
  • 6
  • better named your control with initial like txtName for textbox .. cboName for combobox etc ... – matzone May 30 '13 at 16:47