truncated error

cameronxt

Member
Joined
Mar 2, 2012
Messages
5
Programming Experience
Beginner
Hi all, im new to this forum and to VB.net programming, i have some previous experience in c++ but not a significant amount. anyhow...

I am making a program for my auto shop that keeps track of several pieces of information. When im attempting to update my sql server with the input data for a new record on my customers table i am getting this error:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: String or binary data would be truncated.
The statement has been terminated.

then under my "autos tab" during debugging it says in red:

+ PhoneText {Text = ""} System.Windows.Forms.TextBox
+ objCmd.Parameters.Item("@PhoneNumber").Value "System.Windows.Forms.TextBox, Text: " {String} Object

i am not certain what this means, i though maybe i was sending the table the wrong data type and tried several different ones to no avail. Here is my code for this particular section


VB.NET:
Private Sub btnAddCustomer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        'cnn.Open()
        Dim sSQL As String = "INSERT INTO Customers " & _
          "(FirstName, LastName, City, State, Zip, Phone#)" & _
             "VALUES (@FirstName, @LastName, @City, @State, @Zip, @PhoneNumber)"
        Dim objCmd As New SqlCommand(sSQL, cnn)

        objCmd.Parameters.Add("@FirstName", SqlDbType.VarChar)
        objCmd.Parameters.Item("@FirstName").Value = FirstNameText.Text.ToString

        objCmd.Parameters.Add("@LastName", SqlDbType.VarChar)
        objCmd.Parameters.Item("@LastName").Value = LastNameText.Text.ToString

        objCmd.Parameters.Add("@City", SqlDbType.VarChar)
        objCmd.Parameters.Item("@City").Value = CityText.Text.ToString


        objCmd.Parameters.Add("@State", SqlDbType.VarChar)
        objCmd.Parameters.Item("@State").Value = StateText.ToString

        objCmd.Parameters.Add("@Zip", SqlDbType.VarChar)
        objCmd.Parameters.Item("@Zip").Value = ZipText.Text.ToString

        objCmd.Parameters.Add("@PhoneNumber", SqlDbType.NChar)
        objCmd.Parameters.Item("@PhoneNumber").Value = PhoneText.ToString


        objCmd.ExecuteNonQuery()


    End Sub

if anyone can shed some light on this or point me towards a better way to do this it would be very much appreciated thanks
 
It means that you are trying to save a text or binary value that is longer than is allowed in the database column. You need to either change the column size in the database or else place some restrictions in your app to prevent the data being too long.
 
thats another thing i tried, i changed the limit of the column size to a higher number, i limited the input to only 10 char's since that was what the default was for the column and i have tried not inserting anything into the feild as well and still gives me the same error
 
ok so i figured out my error. after some more googling i found out that each row in a table can only hold 8060 bytes of data and i needed to limit the input of all of my fields not just the last one in order to keep it from allocating the empty space to everything else
 
Back
Top