String or binary data would be truncated?

Howlleo

Member
Joined
Jan 14, 2008
Messages
13
Programming Experience
Beginner
I am pathetically grateful. It works now.

But have you noticed anything wrong with the WriteClasses() sub? Because I'm getting an error about truncation. I've double checked, and the fields being written to are 2 characters, 6 characters, and 6 characters, and the parameters being passed are also 2, 6, and 6 characters long.

I believe they're all strings, but might be objects. Would that make a difference? Error message is below code.

VB.NET:
    Sub WriteClasses(ByVal CreditsArray)
        Dim i As Integer
        Dim EnrollDb As SqlConnection
        Dim cmdEnroll As SqlCommand
        EnrollDb = New SqlConnection("Server=LONNA\SQLEXPRESS;Integrated Security=True;database=LGordonTouroReg")
        cmdEnroll = New SqlCommand("INSERT INTO Enrollment (SectionID, Semester, Year, ClassID, StudentID) VALUES ('@SectionID', 'Fall', '2007', '@ClassID', '@StudentID')", EnrollDb)
        cmdEnroll.Parameters.AddWithValue("@SectionID", CreditsArray(i, 2))
        cmdEnroll.Parameters.AddWithValue("@ClassID", CreditsArray(i, 0))
        cmdEnroll.Parameters.AddWithValue("@studentID", Profile.StudentID)
        EnrollDb.Open()
        For i = 0 To 12
            cmdEnroll.ExecuteNonQuery()
            Response.Write(CreditsArray(i, 0) & " has been added to your schedule.")
        Next i
        EnrollDb.Close()
    End Sub

ERROR MESSAGE:
Server Error in '/LGordonTouroReg' Application.
________________________________________
String or binary data would be truncated.
The statement has been terminated.
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.
The statement has been terminated.

Source Error:

Line 188: EnrollDb.Open()
Line 189: For i = 0 To 12
Line 190: cmdEnroll.ExecuteNonQuery()
Line 191: Response.Write(CreditsArray(i, 0) & " has been added to your schedule.")
Line 192: Next i

Source File: C:\Users\Lonna\Documents\Visual Studio 2005\WebSites\LGordonTouroReg\RegistrationPage.aspx Line: 190

Stack Trace:

[SqlException (0x80131904): String or binary data would be truncated.
The statement has been terminated.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +177
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +68
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +199
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2411
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +147
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +1038
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +314
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +413
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +115
ASP.registrationpage_aspx.WriteClasses(Object CreditsArray) in C:\Users\Lonna\Documents\Visual Studio 2005\WebSites\LGordonTouroReg\RegistrationPage.aspx:190
ASP.registrationpage_aspx.NoDouble(Object CreditsArray) in C:\Users\Lonna\Documents\Visual Studio 2005\WebSites\LGordonTouroReg\RegistrationPage.aspx:175
ASP.registrationpage_aspx.CheckPrereq(Object creditsArray) in C:\Users\Lonna\Documents\Visual Studio 2005\WebSites\LGordonTouroReg\RegistrationPage.aspx:153
ASP.registrationpage_aspx.RegisterButton_Click(Object sender, EventArgs e) in C:\Users\Lonna\Documents\Visual Studio 2005\WebSites\LGordonTouroReg\RegistrationPage.aspx:25
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +75
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +97
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4921
 
VALUES ('@SectionID', 'Fall', '2007', '@ClassID', '@StudentID')", these are all plain strings, no paramaters, while this:
VALUES (@SectionID, 'Fall', '2007', @ClassID, @StudentID)", has three parameters, see the difference?
 
Back
Top