locked
A strange exception RRS feed

  • Question

  • I have this code:

    public void SaveMemoFromEditor (string dataTableName, string numeric_id, string memo)
            {
                // here we have a totally different situation from the previous: saveMemoFromRtfOthers
                if (richTextBox_pg_3.Text != String.Empty)
                {
                    DoServer.GetSrvConnOrSwitch("ComeAndGetThr");
                    using (SqlConnection conn = new SqlConnection(Globals.srv.ConnectionContext.ConnectionString))
                    {
                        conn.Open();
                        SqlCommand cmdm = new SqlCommand();
                        cmdm.Connection = conn;
                        cmdm.CommandType = CommandType.Text;                   
                        cmdm.CommandText = "UPDATE [dbo].[" + dataTableName + "] SET memo = @memo WHERE numeric_id = @numeric_id ";                    
                        cmdm = SetControls.createSqlParameter(cmdm, "memo", memo);
                        cmdm = SetControls.createSqlParameter(cmdm, "numeric_id", numeric_id);
                        cmdm.ExecuteNonQuery(); // <== Exception
                    }           // end of using conn

    I get this exception:

    System.Data.SqlClient.SqlException
      HResult=0x80131904
      Message=String or binary data would be truncated.
    The statement has been terminated.
      Source=.Net SqlClient Data Provider
      StackTrace:
       at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
       at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
       at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
       at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted)
       at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
       at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
       at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
       at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
       at ComeAndGet.Form1.SaveMemoFromEditor(String dataTableName, String numeric_id, String memo) in C:\VCSharp_Projects\ComeAndGet\ComeAndGet\Form1.cs:line 6412
       at ComeAndGet.Form1.pushSaveRTF_pg3_Click(Object sender, EventArgs e) in C:\VCSharp_Projects\ComeAndGet\ComeAndGet\Form1Add5.cs:line 299
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at ComeAndGet.Program.Main() in C:\VCSharp_Projects\ComeAndGet\ComeAndGet\Program.cs:line 16

    What is this. It is the first time I see something like this.

    Thanks, - MyCatAlex

    Tuesday, August 21, 2018 12:30 AM

Answers

  • It means the length of memo variable you're going to store in [memo] field of the table is larger than the size the field can hold.

    Check the database schema to see what the field width is, then decide whether to widen it, or to add a length check to truncate memo if it's too long.

    • Marked as answer by MyCatAlex Tuesday, August 21, 2018 12:46 PM
    Tuesday, August 21, 2018 12:57 AM
    Answerer
  • Hi MyCatAlex,

    Thank you for posting here.

    For your question, I could not make sure which of memo or numeric_id caused the error.

    I make a simple example to reproduce your error. 

    It caused by the data which you insert to the database is over than the Maximum length of your variable length. 

    Please check the Maximum length of your Data type. The data you insert is over than the Maximum length.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by MyCatAlex Tuesday, August 21, 2018 1:02 PM
    Tuesday, August 21, 2018 8:12 AM

All replies

  • It means the length of memo variable you're going to store in [memo] field of the table is larger than the size the field can hold.

    Check the database schema to see what the field width is, then decide whether to widen it, or to add a length check to truncate memo if it's too long.

    • Marked as answer by MyCatAlex Tuesday, August 21, 2018 12:46 PM
    Tuesday, August 21, 2018 12:57 AM
    Answerer
  • Hi MyCatAlex,

    Thank you for posting here.

    For your question, I could not make sure which of memo or numeric_id caused the error.

    I make a simple example to reproduce your error. 

    It caused by the data which you insert to the database is over than the Maximum length of your variable length. 

    Please check the Maximum length of your Data type. The data you insert is over than the Maximum length.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by MyCatAlex Tuesday, August 21, 2018 1:02 PM
    Tuesday, August 21, 2018 8:12 AM
  • It means the length of memo variable you're going to store in [memo] field of the table is larger than the size the field can hold.

    Check the database schema to see what the field width is, then decide whether to widen it, or to add a length check to truncate memo if it's too long.

    That was a correct diagnosis. You've saved me a lot of time.

    Normally I make memo fields varchar(MAX) but for this particular table I decided that 200 characters would suffice, however, I was saving an RTF control and it has many formatting characters. There was a curious event that I could not interpret. I was adding a few words to already existing comment in there and when I simply put in a few dots like .... the table accepted it! Apparently I was working just under 200 characters. But when I entered more information it crashed every time.

    Thanks again, - MyCatAlex


    • Edited by MyCatAlex Tuesday, August 21, 2018 1:04 PM
    Tuesday, August 21, 2018 12:45 PM