Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sqldataadapter.update error: String or binary data would be truncated

Status
Not open for further replies.

bebblebrox

IS-IT--Management
Aug 9, 2004
39
0
0
US
ok so here's the error i'm getting (stack trace and all):

Code:
Server Error in '/305C06' Application.
--------------------------------------------------------------------------------

String or binary data would be truncated. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: String or binary data would be truncated.

Source Error: 


Line 163:
Line 164:        'update database
Line 165:        mda.Update(mds, "Customers")
Line 166:
Line 167:        'bind
 

Source File: c:\inetpub\[URL unfurl="true"]wwwroot\305C06\StepByStep6-26.aspx.vb[/URL]    Line: 165 

Stack Trace: 


[SqlException: String or binary data would be truncated.]
   System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
   System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable)
   _305C06.StepByStep6_26.btnAdd_Click(Object sender, EventArgs e) in c:\inetpub\[URL unfurl="true"]wwwroot\305C06\StepByStep6-26.aspx.vb:165[/URL]
   System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   System.Web.UI.Page.ProcessRequestMain()


now for the code snippets: this is the handler for a button to add a new customer

Code:
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim dr As DataRow = mds.Tables("Customers").NewRow
        'set values
        dr(0) = txtCustomerID.Text
        dr(1) = txtCompanyName.Text
        dr(2) = txtContactName.Text

        'add row
        mds.Tables("Customers").Rows.Add(dr)

        'update database
        mda.Update(mds, "Customers")
****previous line causes error****************

        'bind

        LoadData()

    End Sub

here's the dataadapter & sqlcommand setup:
Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'set up the sqldataadapter 
        Dim cmdSelect As SqlCommand = mcnn.CreateCommand
        cmdSelect.CommandType = CommandType.Text
        cmdSelect.CommandText = "Select CustomerID, CompanyName, ContactName from Customers"

 
        Dim cmdInsert As SqlCommand = mcnn.CreateCommand
        cmdInsert.CommandType = CommandType.Text
        cmdInsert.CommandText = "Insert into Customers(CustomerID, CompanyName, " & _
                                "ContactName) values ('@CustomerID', '@CompanyName', " & _
                                "'@ContactName')"
        cmdInsert.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID")
        cmdInsert.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName")
        cmdInsert.Parameters.Add("@ContactName", SqlDbType.NVarChar, 30, "ContactName")
        cmdInsert.Parameters("@CustomerID").SourceVersion = DataRowVersion.Original


        'setup data adapter and fill dataset
        mda.SelectCommand = cmdSelect
        mda.UpdateCommand = cmdUpdate
        mda.DeleteCommand = cmdDelete
        mda.InsertCommand = cmdInsert

        mda.Fill(mds, "Customers")

        'load datagrid
        If Not IsPostBack Then
            LoadData()
        End If

    End Sub

i can post all the code if it will help. the form contains a DataGrid which all the existing data is bound too & LoadData is a sub that binds the data.

thanks
 
Check to make sure that the length of the string being passed to the stored procedure is not longer than the column length in the database table. For example:

In the procedure, I have the parameter @foo, which will populate the column foo in the database table.

sproc:
@foo varchar(255)

table:
foo varchar(50)

The above snippet will bail (if the value of @foo is greater than 50).

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
i double checked that and my string is the same length.

any other ideas?
 
Atmoic Chip is right. You have a column in a SQL table that has a character length too small for one of your values.

It may not be your "Customers" column if you've double checked it. I would check every column you're updating because the error you're getting can be nothing other than a column character length issue.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top