I have a weird issue where I have a stored procedure that is inserting data into a database, it was working fine when we had the database in SQL Server 2005, but we have upgraded to SQL Server 2008 R2 and now it fails after two successful entries.
The error being returned from the SQL exception is
string or binary data would be truncated
the statement has been terminated
Has anyone come across this sort of issue before ? As I say this has only started since we migrated to SQL Server 2008.
I should also say, that I know what the truncation error means, but this is the issue, I have checked the values and lengths being sent to the database and none of them exceed the field sizes that are being inserted. Also I can run this multiple times from Management Studio and it never fails. So I’m thinking its something to do with the connection or sql command object maybe ??
Code for the connection to the database is below
Public Function AddDataToDatabase(ByVal ModuleName As String, ByVal ModuleKey As Integer, ByVal ShapeDescription As String, ByVal ShapeNotes As String, ByVal ShapeType As String, ByVal ShapeStatus As String, ByVal FeatureType As String) As AttributeEditingResult Implements IDataService.AddDataToDatabase
Dim sqlConn As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("MDC_Reference").ConnectionString)
Dim sqlCmd As SqlClient.SqlCommand = New SqlCommand
sqlCmd.CommandText = ConfigurationManager.AppSettings.Get("sp_Insert")
sqlCmd.CommandType = CommandType.StoredProcedure
sqlCmd.Connection = sqlConn
Dim myReader As SqlClient.SqlDataReader
Dim TableKey As Integer = 0
Dim Result As New AttributeEditingResult With {
.Success = False, _
.TableKey = TableKey}
sqlCmd.Parameters.Clear()
sqlCmd.Parameters.Add(New SqlParameter("@ModuleName", SqlDbType.VarChar, 50))
sqlCmd.Parameters.Add(New SqlParameter("@ModuleKey", SqlDbType.Int))
sqlCmd.Parameters.Add(New SqlParameter("@ShapeDescription", SqlDbType.VarChar, 255))
sqlCmd.Parameters.Add(New SqlParameter("@ShapeNotes", SqlDbType.VarChar, 1024))
sqlCmd.Parameters.Add(New SqlParameter("@ShapeType", SqlDbType.VarChar, 10))
sqlCmd.Parameters.Add(New SqlParameter("@ShapeStatus", SqlDbType.VarChar, 50))
sqlCmd.Parameters.Add(New SqlParameter("@FeatureType", SqlDbType.VarChar, 50))
sqlCmd.Parameters("@ModuleName").Value = ModuleName
sqlCmd.Parameters("@ModuleKey").Value = ModuleKey
sqlCmd.Parameters("@ShapeDescription").Value = ShapeDescription
sqlCmd.Parameters("@ShapeNotes").Value = ShapeNotes
sqlCmd.Parameters("@ShapeType").Value = ShapeType
sqlCmd.Parameters("@ShapeStatus").Value = ShapeStatus
If FeatureType = "NULL" Then
sqlCmd.Parameters("@FeatureType").Value = DBNull.Value
Else
sqlCmd.Parameters("@FeatureType").Value = FeatureType
End If
Try
sqlConn.Open()
myReader = sqlCmd.ExecuteReader()
While myReader.Read
Result.TableKey = CInt(myReader(0))
End While
sqlConn.Close()
sqlCmd.Dispose()
Result.Success = True
Catch ex As SqlException
sqlConn.Close()
sqlCmd.Dispose()
Result.Success = False
Result.ErrorMessage = ex.Message & vbNewLine & ex.StackTrace
End Try
Return Result
End Function
I have seen this error quite a lot before and each time it related to a varchar(x) and then inserting data into the table with that particular field set longer than x characters.
You mention that it isn’t the field lengths, but for me that is the only time I have seen that error. Perhaps your case is different.
You have got quite a lot of strings there which are finite lengths defined in the database. Take each one and test it’s length and log an error, or truncate the string yourself, depending on what is appropriate.
In C# it would look like this,
The problem with the failure and error is that it doesn’t tell you which field the error has occurred with.
I wrote myself a validator which looked at my database (using entity framework), compared the field lengths to the incoming data, and logs errors when a field is too long. Truncating wasn’t appropriate in my case the operation just had to be terminated.