Am using Vb.NET 2008 and SQL 2008. I have a problem, when the execution reaches daAdapter.Update(dtTable) then I receive this error ""String or binary data would be truncated.
The statement has been terminated"" Then the record fails to update. What could possibly be the error? Below is the complete code. Thanx in advance.
Dim SQLCon As SqlConnection
Dim SqlQuery, SqlQuery1 As String
Dim daAdapter As SqlDataAdapter
Dim SQLCom As SqlCommand
Dim SQLCom1 As SqlCommand
strUsername = GetSetting("Lands", "Connection", "DataL1")
strPassword = GetSetting("Lands", "Connection", "DataL2")
strServerName = GetSetting("Lands", "Connection", "DataL3")
strInitialCatalog = GetSetting("Lands", "Connection", "DataL4")
DataL1 = strUsername & strPassword & strServerName & strInitialCatalog
dTable = New DataSet
dtTable = New DataTable
SQLCom = New SqlCommand
SQLCom1 = New SqlCommand
SQLCon = New SqlConnection
SQLCon.ConnectionString = DataL1
Try
SqlQuery = "Insert FileRegister values ('@Prefix','@File','@Plot','@Block','@Street','@Se ction','@Location','@town','@District','@Purpose', '@Applicant','@Address','@Remarks','@Title','@Size ','@Term')"
SqlQuery1 = "select * from FileRegister"
daAdapter = New SqlDataAdapter
SQLCom.Connection = SQLCon
SQLCom1.Connection = SQLCon
SQLCom.CommandType = 1
SQLCom1.CommandType = 1
SQLCom.CommandText = SqlQuery
SQLCom1.CommandText = SqlQuery1
daAdapter.InsertCommand = SQLCom
daAdapter.SelectCommand = SQLCom1
SQLCon.Open()
daAdapter.Fill(dTable, "FileRegister")
dtTable = dTable.Tables("FileRegister")
Dim r As DataRow = dtTable.NewRow
r(0) = Me.cboPrefix.Text.Trim()
r(1) = Convert.ToDouble(Me.txtFileNo.Text.Trim)
r(2) = Me.txtPlotNo.Text.Trim
r(3) = Me.txtBlockNo.Text.Trim
r(4) = Me.txtStreetName.Text.Trim
r(5) = Me.txtSectionNo.Text.Trim
r(6) = Me.txtLocation.Text.Trim
r(7) = Me.cboTown.Text.Trim
r(8) = Me.cboDistrict.Text.Trim
r(9) = Me.cboPurpose.Text.Trim
r(10) = Me.txtApplicant.Text.Trim
r(11) = Me.txtAddress.Text.Trim
r(12) = Me.txtRemarks.Text.Trim
r(13) = Me.txtTitleNo.Text.Trim
r(14) = Me.txtSize.Text.Trim
r(15) = Me.txtTerm.Text.Trim
dtTable.Rows.Add(r)
daAdapter.InsertCommand.Parameters.Add("@Prefix", SqlDbType.Char, 3, "Prefix")
daAdapter.InsertCommand.Parameters.Add("@File", SqlDbType.VarChar, 30, "file_No")
daAdapter.InsertCommand.Parameters.Add("@Plot", SqlDbType.VarChar, 120, "plot_no")
daAdapter.InsertCommand.Parameters.Add("@Block", SqlDbType.VarChar, 10, "Block_No")
daAdapter.InsertCommand.Parameters.Add("@Street", SqlDbType.VarChar, 60, "Street_Name")
daAdapter.InsertCommand.Parameters.Add("@Section", SqlDbType.VarChar, 30, "Section_No")
daAdapter.InsertCommand.Parameters.Add("@Location" , SqlDbType.VarChar, 40, "Location")
daAdapter.InsertCommand.Parameters.Add("@Town", SqlDbType.VarChar, 150, "Town")
daAdapter.InsertCommand.Parameters.Add("@District" , SqlDbType.VarChar, 150, "District")
daAdapter.InsertCommand.Parameters.Add("@Purpose", SqlDbType.VarChar, 150, "Purpose")
daAdapter.InsertCommand.Parameters.Add("@Applicant ", SqlDbType.VarChar, 50, "Applicant")
daAdapter.InsertCommand.Parameters.Add("@Address", SqlDbType.VarChar, 160, "Address")
daAdapter.InsertCommand.Parameters.Add("@Remarks", SqlDbType.VarChar, 600, "Remarks")
daAdapter.InsertCommand.Parameters.Add("@Title", SqlDbType.VarChar, 30, "Title")
daAdapter.InsertCommand.Parameters.Add("@Size", SqlDbType.VarChar, 50, "Size")
daAdapter.InsertCommand.Parameters.Add("@Term", SqlDbType.VarChar, 15, "Term")
daAdapter.Update(dtTable)
Catch
MsgBox Err.Description
End Try
End Sub
8 2456
I think you're trying to insert something that is longer than the maximum field size in your database. Did you check the length of what you're inserting?
Steven
if one of your database fields is say a "varchar(50)", then its max length is 50, if you try to pass in more then 50 characters, you would get that error i believe
Exactly right
A text with a size of 50 chars, when you insert something into that column with say 60 chars it will truncate the last 10 chars off the string you tried to put in it.
@Plater
In fact, values from the text boxes are not updated in the database, but rather the insert statement as follows @Prefix','@File','@Plot and so on. What could possibly be the problem?
Ben -
SqlQuery = "Insert FileRegister values ('@Prefix','@File','@Plot','@Block','@Street','@Se ction','@Location','@town','@District','@Purpose', '@Applicant','@Address','@Remarks','@Title','@Size ','@Term')"
-
-
-
-
-
-
-
Am using Vb.NET 2008 and SQL 2008. I have a problem, when the execution reaches daAdapter.Update(dtTable) then I receive this error ""String or binary data would be truncated.
-
The statement has been terminated"" Then the record fails to update. What could possibly be the error? Below is the complete code. Thanx in advance.
-
-
Dim SQLCon As SqlConnection
-
Dim SqlQuery, SqlQuery1 As String
-
Dim daAdapter As SqlDataAdapter
-
Dim SQLCom As SqlCommand
-
Dim SQLCom1 As SqlCommand
-
-
strUsername = GetSetting("Lands", "Connection", "DataL1")
-
strPassword = GetSetting("Lands", "Connection", "DataL2")
-
strServerName = GetSetting("Lands", "Connection", "DataL3")
-
strInitialCatalog = GetSetting("Lands", "Connection", "DataL4")
-
DataL1 = strUsername & strPassword & strServerName & strInitialCatalog
-
dTable = New DataSet
-
dtTable = New DataTable
-
SQLCom = New SqlCommand
-
SQLCom1 = New SqlCommand
-
-
SQLCon = New SqlConnection
-
SQLCon.ConnectionString = DataL1
-
Try
-
SqlQuery = "Insert FileRegister values ('@Prefix','@File','@Plot','@Block','@Street','@Se ction','@Location','@town','@District','@Purpose', '@Applicant','@Address','@Remarks','@Title','@Size ','@Term')"
-
SqlQuery1 = "select * from FileRegister"
-
daAdapter = New SqlDataAdapter
-
SQLCom.Connection = SQLCon
-
SQLCom1.Connection = SQLCon
-
SQLCom.CommandType = 1
-
SQLCom1.CommandType = 1
-
-
SQLCom.CommandText = SqlQuery
-
SQLCom1.CommandText = SqlQuery1
-
daAdapter.InsertCommand = SQLCom
-
daAdapter.SelectCommand = SQLCom1
-
SQLCon.Open()
-
daAdapter.Fill(dTable, "FileRegister")
-
dtTable = dTable.Tables("FileRegister")
-
-
-
Dim r As DataRow = dtTable.NewRow
-
r(0) = Me.cboPrefix.Text.Trim()
-
r(1) = Convert.ToDouble(Me.txtFileNo.Text.Trim)
-
r(2) = Me.txtPlotNo.Text.Trim
-
r(3) = Me.txtBlockNo.Text.Trim
-
r(4) = Me.txtStreetName.Text.Trim
-
r(5) = Me.txtSectionNo.Text.Trim
-
r(6) = Me.txtLocation.Text.Trim
-
r(7) = Me.cboTown.Text.Trim
-
r(8) = Me.cboDistrict.Text.Trim
-
r(9) = Me.cboPurpose.Text.Trim
-
r(10) = Me.txtApplicant.Text.Trim
-
r(11) = Me.txtAddress.Text.Trim
-
r(12) = Me.txtRemarks.Text.Trim
-
r(13) = Me.txtTitleNo.Text.Trim
-
r(14) = Me.txtSize.Text.Trim
-
r(15) = Me.txtTerm.Text.Trim
-
-
dtTable.Rows.Add(r)
-
daAdapter.InsertCommand.Parameters.Add("@Prefix", SqlDbType.Char, 3, "Prefix")
-
daAdapter.InsertCommand.Parameters.Add("@File", SqlDbType.VarChar, 30, "file_No")
-
daAdapter.InsertCommand.Parameters.Add("@Plot", SqlDbType.VarChar, 120, "plot_no")
-
daAdapter.InsertCommand.Parameters.Add("@Block", SqlDbType.VarChar, 10, "Block_No")
-
daAdapter.InsertCommand.Parameters.Add("@Street", SqlDbType.VarChar, 60, "Street_Name")
-
daAdapter.InsertCommand.Parameters.Add("@Section", SqlDbType.VarChar, 30, "Section_No")
-
daAdapter.InsertCommand.Parameters.Add("@Location" , SqlDbType.VarChar, 40, "Location")
-
daAdapter.InsertCommand.Parameters.Add("@Town", SqlDbType.VarChar, 150, "Town")
-
daAdapter.InsertCommand.Parameters.Add("@District" , SqlDbType.VarChar, 150, "District")
-
daAdapter.InsertCommand.Parameters.Add("@Purpose", SqlDbType.VarChar, 150, "Purpose")
-
daAdapter.InsertCommand.Parameters.Add("@Applicant ", SqlDbType.VarChar, 50, "Applicant")
-
daAdapter.InsertCommand.Parameters.Add("@Address", SqlDbType.VarChar, 160, "Address")
-
daAdapter.InsertCommand.Parameters.Add("@Remarks", SqlDbType.VarChar, 600, "Remarks")
-
daAdapter.InsertCommand.Parameters.Add("@Title", SqlDbType.VarChar, 30, "Title")
-
daAdapter.InsertCommand.Parameters.Add("@Size", SqlDbType.VarChar, 50, "Size")
-
daAdapter.InsertCommand.Parameters.Add("@Term", SqlDbType.VarChar, 15, "Term")
-
-
-
daAdapter.Update(dtTable)
-
Catch
-
MsgBox Err.Description
-
End Try
-
-
End Sub
-
Reply
The same thing we just said?
For example, this line:
daAdapter.InsertCommand.Parameters.Add("@Location" , SqlDbType.VarChar, 40, "Location")
If the value that gets added to that parameter is longer then 40 characters (or in the database the column is actually LESS then 40 characters) you could see that error.
Field size error has now been solved.
But In fact, values from the text boxes are not updated in the database, but rather the insert statement as follows @Prefix','@File','@Plot get saved.
Ben -
SqlQuery = "Insert FileRegister values ('@Prefix','@File','@Plot','@Block','@Street','@Se ction','@Location','@town','@District','@Purpose', '@Applicant','@Address','@Remarks','@Title','@Size ','@Term')"
-
-
-
Dim SQLCon As SqlConnection
-
Dim SqlQuery, SqlQuery1 As String
-
Dim daAdapter As SqlDataAdapter
-
Dim SQLCom As SqlCommand
-
Dim SQLCom1 As SqlCommand
-
-
strUsername = GetSetting("Lands", "Connection", "DataL1")
-
strPassword = GetSetting("Lands", "Connection", "DataL2")
-
strServerName = GetSetting("Lands", "Connection", "DataL3")
-
strInitialCatalog = GetSetting("Lands", "Connection", "DataL4")
-
DataL1 = strUsername & strPassword & strServerName & strInitialCatalog
-
dTable = New DataSet
-
dtTable = New DataTable
-
SQLCom = New SqlCommand
-
SQLCom1 = New SqlCommand
-
-
SQLCon = New SqlConnection
-
SQLCon.ConnectionString = DataL1
-
Try
-
SqlQuery = "Insert FileRegister values ('@Prefix','@File','@Plot','@Block','@Street','@Se ction','@Location','@town','@District','@Purpose', '@Applicant','@Address','@Remarks','@Title','@Size ','@Term')"
-
SqlQuery1 = "select * from FileRegister"
-
daAdapter = New SqlDataAdapter
-
SQLCom.Connection = SQLCon
-
SQLCom1.Connection = SQLCon
-
SQLCom.CommandType = 1
-
SQLCom1.CommandType = 1
-
-
SQLCom.CommandText = SqlQuery
-
SQLCom1.CommandText = SqlQuery1
-
daAdapter.InsertCommand = SQLCom
-
daAdapter.SelectCommand = SQLCom1
-
SQLCon.Open()
-
daAdapter.Fill(dTable, "FileRegister")
-
dtTable = dTable.Tables("FileRegister")
-
-
-
Dim r As DataRow = dtTable.NewRow
-
r(0) = Me.cboPrefix.Text.Trim()
-
r(1) = Convert.ToDouble(Me.txtFileNo.Text.Trim)
-
r(2) = Me.txtPlotNo.Text.Trim
-
r(3) = Me.txtBlockNo.Text.Trim
-
r(4) = Me.txtStreetName.Text.Trim
-
r(5) = Me.txtSectionNo.Text.Trim
-
r(6) = Me.txtLocation.Text.Trim
-
r(7) = Me.cboTown.Text.Trim
-
r(8) = Me.cboDistrict.Text.Trim
-
r(9) = Me.cboPurpose.Text.Trim
-
r(10) = Me.txtApplicant.Text.Trim
-
r(11) = Me.txtAddress.Text.Trim
-
r(12) = Me.txtRemarks.Text.Trim
-
r(13) = Me.txtTitleNo.Text.Trim
-
r(14) = Me.txtSize.Text.Trim
-
r(15) = Me.txtTerm.Text.Trim
-
-
dtTable.Rows.Add(r)
-
daAdapter.InsertCommand.Parameters.Add("@Prefix", SqlDbType.Char, 3, "Prefix")
-
daAdapter.InsertCommand.Parameters.Add("@File", SqlDbType.VarChar, 30, "file_No")
-
daAdapter.InsertCommand.Parameters.Add("@Plot", SqlDbType.VarChar, 120, "plot_no")
-
daAdapter.InsertCommand.Parameters.Add("@Block", SqlDbType.VarChar, 10, "Block_No")
-
daAdapter.InsertCommand.Parameters.Add("@Street", SqlDbType.VarChar, 60, "Street_Name")
-
daAdapter.InsertCommand.Parameters.Add("@Section", SqlDbType.VarChar, 30, "Section_No")
-
daAdapter.InsertCommand.Parameters.Add("@Location" , SqlDbType.VarChar, 40, "Location")
-
daAdapter.InsertCommand.Parameters.Add("@Town", SqlDbType.VarChar, 150, "Town")
-
daAdapter.InsertCommand.Parameters.Add("@District" , SqlDbType.VarChar, 150, "District")
-
daAdapter.InsertCommand.Parameters.Add("@Purpose", SqlDbType.VarChar, 150, "Purpose")
-
daAdapter.InsertCommand.Parameters.Add("@Applicant ", SqlDbType.VarChar, 50, "Applicant")
-
daAdapter.InsertCommand.Parameters.Add("@Address", SqlDbType.VarChar, 160, "Address")
-
daAdapter.InsertCommand.Parameters.Add("@Remarks", SqlDbType.VarChar, 600, "Remarks")
-
daAdapter.InsertCommand.Parameters.Add("@Title", SqlDbType.VarChar, 30, "Title")
-
daAdapter.InsertCommand.Parameters.Add("@Size", SqlDbType.VarChar, 50, "Size")
-
daAdapter.InsertCommand.Parameters.Add("@Term", SqlDbType.VarChar, 15, "Term")
-
-
-
daAdapter.Update(dtTable)
-
Catch
-
MsgBox Err.Description
-
End Try
-
-
End Sub
-
Ooo sorry I must have gotten the replies mixed up.
Ok I have found that when you do things like this:
r(0) = Me.cboPrefix.Text.Trim()
The values don't actually make their way into the correct named columns.
You would need to something like:
r("SomeColumnName") = Me.cboPrefix.Text.Trim()
To get the values in there
Please, I would to tell u that it worked!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: JEP |
last post by:
I lifted the following code directly from MSDN examples. However, the update command fails against my Northwind/SQL Server database.
The code is:
Private Sub cmdStart_Click(ByVal sender As...
|
by: DraguVaso |
last post by:
Hi,
I have a DataGrid bound to an sqlDataAdapter. When I do a
SqlDataAdapter.Update, on 1 row I got a red dot with a whie exclamation mark
in it, to show me there is an error, and the changes...
|
by: Steven Blair |
last post by:
Hi,
Here is a short decsription of my problem.
I have written a dll for Database accessing. I have one method which can
return a Dataset and another method which takes a Dataset and upates a...
|
by: Christoph |
last post by:
I'm delving into using ADO.NET for the first time. In fact, I've never
done any database work using C# at all so all of this is new to me.
Please bear that in mind if I am asking stupid questions....
|
by: LP |
last post by:
Hello,
In the past I used SqlCommandBuilder and SqlDataAdapter .Update method to
apply changes in a DataTable back to its table source in SQL Server. It
worked fine when DataSet had only 1...
|
by: archana |
last post by:
hi all
I have some doubts related to database handling in .net.
I am using sqldataadapter to update and insert rows into database.
What i want to know is at a time of inserting rows using...
|
by: languy |
last post by:
Hi there
I'm having a problem when using the SqlDataAdapter. When calling the
Update(DataSet, string) method I get the following error message "String or
binary data would be truncated". The...
|
by: Aaron |
last post by:
Hello,
The SqlDataAdapter has 4 properties of type SqlCommand, SelectCommand,
UpdateCommand, DeleteCommand, and InsertCommand. Setting the
SelectCommand allows the SqlDataAdapter to retrieve...
|
by: niccolem |
last post by:
HI:
Can someone tell me HOW to insert a automatic number after my user inserts new data from text boxes into the dataset. I keep getting this error:
Cannot insert the value NULL into column 'id',...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |