Results 1 to 6 of 6

Thread: Using stored proc to insert data to mssql database *Resolved*

  1. #1

    Thread Starter
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Using stored proc to insert data to mssql database *Resolved*

    Here is the routing I use:
    VB Code:
    1. Public Sub InsertData(ByRef SQL As String, ByRef parameters() As InputParameter)
    2.         '1. Create a connection
    3.         Dim myConnection As New SqlClient.SqlConnection(GetConnection)
    4.  
    5.         '2. Create the command object, passing in the SQL string or stored proc name
    6.         Dim sqcmd As New SqlClient.SqlCommand(SQL, myConnection)
    7.         sqcmd.Connection = myConnection
    8.         sqcmd.CommandType = CommandType.StoredProcedure
    9.  
    10.         '3. Add parameters as needed
    11.         If Not parameters Is Nothing Then
    12.             For Each parm As InputParameter In parameters
    13.                 sqcmd.Parameters.Add(New SqlClient.SqlParameter(parm.Name, parm.Type, parm.Length)).Value = parm.Value
    14.             Next
    15.         End If
    16.  
    17.         'Execute Stored Proc
    18.         myConnection.Open()
    19.         sqcmd.ExecuteNonQuery()
    20.         myConnection.Close()
    21.  
    22.     End Sub
    Here's the exception: Exception Details: System.Data.SqlClient.SqlException: String or binary data would be truncated. The statement has been terminated.

    It seems very straight forward, but I've rechecked everthing serveral times, and none of my values are larger than the stored procedure is expecting. I'm stumped, am I just doing something very wrong here?
    Last edited by wild_bill; Nov 28th, 2005 at 03:14 PM.

  2. #2
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Using stored proc to insert data to mssql database

    They may not be bigger than what your sproc is expecting, but what about your table itself?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Using stored proc to insert data to mssql database

    Sounds like one of your parameters is not the correct length for the data it is trying to pass.

    Also, it's not going to hurt but you're setting the Connection for the Command twice: once in the constructor and then explicitly afterwards.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Using stored proc to insert data to mssql database

    Must be the tables, as they said... try to compare the length of the data being passed with the size of the column in the table in the database.

  5. #5

    Thread Starter
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Using stored proc to insert data to mssql database

    It looks like a permissions issue. I went through query analyser and executed the proc under my user id, all went well. I logged in under the id my web form uses, and the proc through the error. All input parameters are identical. Go figure.

  6. #6

    Thread Starter
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Using stored proc to insert data to mssql database

    The user id automatically gets inserted into a field in the table. It slipped under my radar because it dynamically grabs the id, and I don't have to pass it as a parameter. This is also the reason why I had no problems executing the sp logged in as myself, and not the web user.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width