简体   繁体   中英

store exception ex as string in db?

public static bool download()
{
    try
    {
        //perform up dates, if successful
        UpdateActionResultTable("DownloadPOS", "N/A - No Sales file to upload", "Success", "N/A");
        return true;
    }
    catch (Exeption ex)
    {
        UpdateActionResultTable("DownloadPOS", "N/A - No Sales file to upload", "Fail", ex.ToString());
        return false;
    }
}

If unsuccessful I want to store the exception as a string in the db. This is from the UpdateActionResultTable function but it throws error:

String or binary data would be truncated.

The statement has been terminated.

Any idea as to why?

works fine when the update is successful so my only guess is because of the exception?

public static void UpdateActionResultTable(string actionCalled, string salesFile, string result, string reason)
{
    EPOSEntities db = new EPOSEntities();

    ActionResult action = new ActionResult()
    {
         Action = actionCalled,
         File = salesFile,
         TimeStamp =  DateTime.Now,
         Result = result,
         Reason = reason                  
    };

    db.AddToActionResults(action);
    db.SaveChanges();
}

It means that your final parameter is too long:

ex.ToString()

It cannot be written to the database, because the column you are writing to has a lower size limit.

So, either make that string you write shorter (filter out unnecessary data or take only the first n characters) or make the field you are writing that string to in the DB bigger.

The column you have in your database where you are storing the string value for the exception is not large enough to hold the exception string you are giving it.

I would change that column in the database to NVARCHAR(MAX) . To do that just run the SQL script below on your database if you are using SQL Server.

SQL:

ALTER TABLE table_name
ALTER COLUMN column_name NVARCHAR(MAX)

Looks like your database field is not big enough to store the exception string

N/A takes only 3 characters, but exceptions can get quite big. So if your field is lets say varchar(50), "N/A" would be no problem because the field can hold up to 50 characters. But an exception stored as string will be way bigger

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM