Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6937327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:20:56+00:00 2026-05-27T12:20:56+00:00

I have a weird issue where I have a stored procedure that is inserting

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:20:57+00:00Added an answer on May 27, 2026 at 12:20 pm

    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,

    if (ModuleName.Length > 50)
    {
        ModuleName = ModuleName.Substring(0, 50);
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a weird issue that only seems to be affecting IE 7. The
I have a weird issue with printing data out. I use printf to print
I have got one weird issue. I am working on an asp .net mvc
I'm fairly new to SVN and I have a weird behavior issue. I'm working
I have a weird issue. I receive the following error that causes a force-close:
I seem to have some weird issue going on that I am sure will
I'm running a weird issue here. I have code that makes jquery ajax calls
Hey guys I got a weird issue.. I have a datalist that's only loaded
I'm having a weird issue that I can't track down... For context, I have
I am having some weird issue here. I have a database table which has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.