473,394 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

SqlDataAdapter.Update Error

54
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
Apr 16 '09 #1
8 2456
MrMancunian
569 Expert 512MB
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
Apr 16 '09 #2
Plater
7,872 Expert 4TB
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
Apr 16 '09 #3
iam_clint
1,208 Expert 1GB
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.
Apr 16 '09 #4
Benniit
54
@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
Expand|Select|Wrap|Line Numbers
  1. SqlQuery = "Insert FileRegister values ('@Prefix','@File','@Plot','@Block','@Street','@Se ction','@Location','@town','@District','@Purpose', '@Applicant','@Address','@Remarks','@Title','@Size ','@Term')"
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  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.
  9. 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.
  10.  
  11. Dim SQLCon As SqlConnection
  12. Dim SqlQuery, SqlQuery1 As String
  13. Dim daAdapter As SqlDataAdapter
  14. Dim SQLCom As SqlCommand
  15. Dim SQLCom1 As SqlCommand
  16.  
  17. strUsername = GetSetting("Lands", "Connection", "DataL1")
  18. strPassword = GetSetting("Lands", "Connection", "DataL2")
  19. strServerName = GetSetting("Lands", "Connection", "DataL3")
  20. strInitialCatalog = GetSetting("Lands", "Connection", "DataL4")
  21. DataL1 = strUsername & strPassword & strServerName & strInitialCatalog
  22. dTable = New DataSet
  23. dtTable = New DataTable
  24. SQLCom = New SqlCommand
  25. SQLCom1 = New SqlCommand
  26.  
  27. SQLCon = New SqlConnection
  28. SQLCon.ConnectionString = DataL1
  29. Try
  30. SqlQuery = "Insert FileRegister values ('@Prefix','@File','@Plot','@Block','@Street','@Se ction','@Location','@town','@District','@Purpose', '@Applicant','@Address','@Remarks','@Title','@Size ','@Term')"
  31. SqlQuery1 = "select * from FileRegister"
  32. daAdapter = New SqlDataAdapter
  33. SQLCom.Connection = SQLCon
  34. SQLCom1.Connection = SQLCon
  35. SQLCom.CommandType = 1
  36. SQLCom1.CommandType = 1
  37.  
  38. SQLCom.CommandText = SqlQuery
  39. SQLCom1.CommandText = SqlQuery1
  40. daAdapter.InsertCommand = SQLCom
  41. daAdapter.SelectCommand = SQLCom1
  42. SQLCon.Open()
  43. daAdapter.Fill(dTable, "FileRegister")
  44. dtTable = dTable.Tables("FileRegister")
  45.  
  46.  
  47. Dim r As DataRow = dtTable.NewRow
  48. r(0) = Me.cboPrefix.Text.Trim()
  49. r(1) = Convert.ToDouble(Me.txtFileNo.Text.Trim)
  50. r(2) = Me.txtPlotNo.Text.Trim
  51. r(3) = Me.txtBlockNo.Text.Trim
  52. r(4) = Me.txtStreetName.Text.Trim
  53. r(5) = Me.txtSectionNo.Text.Trim
  54. r(6) = Me.txtLocation.Text.Trim
  55. r(7) = Me.cboTown.Text.Trim
  56. r(8) = Me.cboDistrict.Text.Trim
  57. r(9) = Me.cboPurpose.Text.Trim
  58. r(10) = Me.txtApplicant.Text.Trim
  59. r(11) = Me.txtAddress.Text.Trim
  60. r(12) = Me.txtRemarks.Text.Trim
  61. r(13) = Me.txtTitleNo.Text.Trim
  62. r(14) = Me.txtSize.Text.Trim
  63. r(15) = Me.txtTerm.Text.Trim
  64.  
  65. dtTable.Rows.Add(r)
  66. daAdapter.InsertCommand.Parameters.Add("@Prefix", SqlDbType.Char, 3, "Prefix")
  67. daAdapter.InsertCommand.Parameters.Add("@File", SqlDbType.VarChar, 30, "file_No")
  68. daAdapter.InsertCommand.Parameters.Add("@Plot", SqlDbType.VarChar, 120, "plot_no")
  69. daAdapter.InsertCommand.Parameters.Add("@Block", SqlDbType.VarChar, 10, "Block_No")
  70. daAdapter.InsertCommand.Parameters.Add("@Street", SqlDbType.VarChar, 60, "Street_Name")
  71. daAdapter.InsertCommand.Parameters.Add("@Section", SqlDbType.VarChar, 30, "Section_No")
  72. daAdapter.InsertCommand.Parameters.Add("@Location" , SqlDbType.VarChar, 40, "Location")
  73. daAdapter.InsertCommand.Parameters.Add("@Town", SqlDbType.VarChar, 150, "Town")
  74. daAdapter.InsertCommand.Parameters.Add("@District" , SqlDbType.VarChar, 150, "District")
  75. daAdapter.InsertCommand.Parameters.Add("@Purpose", SqlDbType.VarChar, 150, "Purpose")
  76. daAdapter.InsertCommand.Parameters.Add("@Applicant ", SqlDbType.VarChar, 50, "Applicant")
  77. daAdapter.InsertCommand.Parameters.Add("@Address", SqlDbType.VarChar, 160, "Address")
  78. daAdapter.InsertCommand.Parameters.Add("@Remarks", SqlDbType.VarChar, 600, "Remarks")
  79. daAdapter.InsertCommand.Parameters.Add("@Title", SqlDbType.VarChar, 30, "Title")
  80. daAdapter.InsertCommand.Parameters.Add("@Size", SqlDbType.VarChar, 50, "Size")
  81. daAdapter.InsertCommand.Parameters.Add("@Term", SqlDbType.VarChar, 15, "Term")
  82.  
  83.  
  84. daAdapter.Update(dtTable)
  85. Catch
  86. MsgBox Err.Description
  87. End Try
  88.  
  89. End Sub
  90.  
Reply
Apr 17 '09 #5
Plater
7,872 Expert 4TB
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.
Apr 17 '09 #6
Benniit
54
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
Expand|Select|Wrap|Line Numbers
  1. SqlQuery = "Insert FileRegister values ('@Prefix','@File','@Plot','@Block','@Street','@Se ction','@Location','@town','@District','@Purpose', '@Applicant','@Address','@Remarks','@Title','@Size ','@Term')"
  2.  
  3.  
  4. Dim SQLCon As SqlConnection
  5. Dim SqlQuery, SqlQuery1 As String
  6. Dim daAdapter As SqlDataAdapter
  7. Dim SQLCom As SqlCommand
  8. Dim SQLCom1 As SqlCommand
  9.  
  10. strUsername = GetSetting("Lands", "Connection", "DataL1")
  11. strPassword = GetSetting("Lands", "Connection", "DataL2")
  12. strServerName = GetSetting("Lands", "Connection", "DataL3")
  13. strInitialCatalog = GetSetting("Lands", "Connection", "DataL4")
  14. DataL1 = strUsername & strPassword & strServerName & strInitialCatalog
  15. dTable = New DataSet
  16. dtTable = New DataTable
  17. SQLCom = New SqlCommand
  18. SQLCom1 = New SqlCommand
  19.  
  20. SQLCon = New SqlConnection
  21. SQLCon.ConnectionString = DataL1
  22. Try
  23. SqlQuery = "Insert FileRegister values ('@Prefix','@File','@Plot','@Block','@Street','@Se ction','@Location','@town','@District','@Purpose', '@Applicant','@Address','@Remarks','@Title','@Size ','@Term')"
  24. SqlQuery1 = "select * from FileRegister"
  25. daAdapter = New SqlDataAdapter
  26. SQLCom.Connection = SQLCon
  27. SQLCom1.Connection = SQLCon
  28. SQLCom.CommandType = 1
  29. SQLCom1.CommandType = 1
  30.  
  31. SQLCom.CommandText = SqlQuery
  32. SQLCom1.CommandText = SqlQuery1
  33. daAdapter.InsertCommand = SQLCom
  34. daAdapter.SelectCommand = SQLCom1
  35. SQLCon.Open()
  36. daAdapter.Fill(dTable, "FileRegister")
  37. dtTable = dTable.Tables("FileRegister")
  38.  
  39.  
  40. Dim r As DataRow = dtTable.NewRow
  41. r(0) = Me.cboPrefix.Text.Trim()
  42. r(1) = Convert.ToDouble(Me.txtFileNo.Text.Trim)
  43. r(2) = Me.txtPlotNo.Text.Trim
  44. r(3) = Me.txtBlockNo.Text.Trim
  45. r(4) = Me.txtStreetName.Text.Trim
  46. r(5) = Me.txtSectionNo.Text.Trim
  47. r(6) = Me.txtLocation.Text.Trim
  48. r(7) = Me.cboTown.Text.Trim
  49. r(8) = Me.cboDistrict.Text.Trim
  50. r(9) = Me.cboPurpose.Text.Trim
  51. r(10) = Me.txtApplicant.Text.Trim
  52. r(11) = Me.txtAddress.Text.Trim
  53. r(12) = Me.txtRemarks.Text.Trim
  54. r(13) = Me.txtTitleNo.Text.Trim
  55. r(14) = Me.txtSize.Text.Trim
  56. r(15) = Me.txtTerm.Text.Trim
  57.  
  58. dtTable.Rows.Add(r)
  59. daAdapter.InsertCommand.Parameters.Add("@Prefix", SqlDbType.Char, 3, "Prefix")
  60. daAdapter.InsertCommand.Parameters.Add("@File", SqlDbType.VarChar, 30, "file_No")
  61. daAdapter.InsertCommand.Parameters.Add("@Plot", SqlDbType.VarChar, 120, "plot_no")
  62. daAdapter.InsertCommand.Parameters.Add("@Block", SqlDbType.VarChar, 10, "Block_No")
  63. daAdapter.InsertCommand.Parameters.Add("@Street", SqlDbType.VarChar, 60, "Street_Name")
  64. daAdapter.InsertCommand.Parameters.Add("@Section", SqlDbType.VarChar, 30, "Section_No")
  65. daAdapter.InsertCommand.Parameters.Add("@Location" , SqlDbType.VarChar, 40, "Location")
  66. daAdapter.InsertCommand.Parameters.Add("@Town", SqlDbType.VarChar, 150, "Town")
  67. daAdapter.InsertCommand.Parameters.Add("@District" , SqlDbType.VarChar, 150, "District")
  68. daAdapter.InsertCommand.Parameters.Add("@Purpose", SqlDbType.VarChar, 150, "Purpose")
  69. daAdapter.InsertCommand.Parameters.Add("@Applicant ", SqlDbType.VarChar, 50, "Applicant")
  70. daAdapter.InsertCommand.Parameters.Add("@Address", SqlDbType.VarChar, 160, "Address")
  71. daAdapter.InsertCommand.Parameters.Add("@Remarks", SqlDbType.VarChar, 600, "Remarks")
  72. daAdapter.InsertCommand.Parameters.Add("@Title", SqlDbType.VarChar, 30, "Title")
  73. daAdapter.InsertCommand.Parameters.Add("@Size", SqlDbType.VarChar, 50, "Size")
  74. daAdapter.InsertCommand.Parameters.Add("@Term", SqlDbType.VarChar, 15, "Term")
  75.  
  76.  
  77. daAdapter.Update(dtTable)
  78. Catch
  79. MsgBox Err.Description
  80. End Try
  81.  
  82. End Sub
  83.  
Apr 17 '09 #7
Plater
7,872 Expert 4TB
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
Apr 17 '09 #8
Benniit
54
Please, I would to tell u that it worked!
Apr 17 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
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...
2
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...
1
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...
3
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....
3
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...
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...
1
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...
2
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...
1
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',...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.