By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,890 Members | 1,269 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,890 IT Pros & Developers. It's quick & easy.

SqlDataAdapter.Update Error

P: 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
Share this Question
Share on Google+
8 Replies


MrMancunian
Expert 100+
P: 569
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
Expert 5K+
P: 7,872
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
Expert 100+
P: 1,208
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

P: 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
Expert 5K+
P: 7,872
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

P: 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
Expert 5K+
P: 7,872
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

P: 54
Please, I would to tell u that it worked!
Apr 17 '09 #9

Post your reply

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