我需要将异常详细信息插入到sql server数据库中

时间:2016-10-20 12:16:25

标签: c# asp.net sql-server linq exception

我的问题是我有一个aspx页面,它试图在其代码文件中捕获bocks,它将使用catch中的异常对象处理异常,现在当程序执行到达此catch块时,它调用公共方法GetExceptionDetails返回一个长字符串文本,其中包含异常的所有属性的值,但不包含属性的名称。现在,当我将属性值插入到表对象的数据库字段时,所有内容都直到代码到达db.submitchanges()时,其中弹出的异常语句读取sqldatetimeoverflow并在sqltypesexception下。请帮我弄清楚这个问题,下面是整个代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication5
{
    public partial class EnterMarks : System.Web.UI.Page
    {
        public float average,total;
       public string grade,chk,exmessage;

        DataClasses1DataContext db = new DataClasses1DataContext();
        protected void Page_Load(object sender, EventArgs e)
        {   

            if (Request.QueryString["StudentID"] != null)
            {
                Label1.Text = Request.QueryString["StudentID"];

            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            List<int> stdID = new List<int>();
            tblGrade tb = new tblGrade();
            total = (float)(Convert.ToDouble(TextBox1.Text) + Convert.ToDouble(TextBox2.Text) + Convert.ToDouble(TextBox3.Text));
            average = total / 3;
            if (average > 85 && average < 90)
            {
                grade = "AA";
            }
            else if(average>80 && average<85)
            {
                grade = "A";
            }
            else if (average > 75 && average < 80)
            {
                grade = "BB";
            }
            else if (average > 70 && average < 75)
            {
                grade = "B";
            }
            else if (average > 65 && average < 70)
            {
                grade = "C";
            }
            else if (average > 60 && average < 65)
            {
                grade = "CC";
            }
            else if (average > 55 && average < 60)
            {
                grade = "D";
            }
            else
            {
                grade = "DD";
            }
            //var query = from m in db.tblGrades
            //            where m.StudentID == Convert.ToInt32(Request.QueryString["StudentID"])
            //            select m;
            // foreach (var q in query)
            //{

                tb.StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);
                tb.Grade = grade;
                db.tblGrades.InsertOnSubmit(tb);
                db.SubmitChanges();
            Response.Redirect("WebForm1.aspx");

        }

        protected void Button2_Click(object sender, EventArgs e)
         {
            //var query1 = from n in db.tblContacts where n.StudentID == int.Parse(TextBox4.Text) select n;
             tblExcDet te = new tblExcDet();
            var query1 = from n in db.tblContacts select n.StudentID;
            //try
            //{


                foreach (var q in query1)
                {
                    if (q.Equals((int.Parse(TextBox4.Text))))
                    {
                        Label2.Text = "ID Found";
                    }
                }
                try
                {


                    int? i = null;
                    tblContact tc = new tblContact();
                    tc.StudentID = (int)i ;
                    //db.tblContacts.InsertOnSubmit(tc);
                    db.SubmitChanges();
                }
                catch (Exception ex)
                {

                    exmessage = GetExceptionDetails(ex);
                    te.ExMessage = exmessage.Split('*')[0];
                    te.ExData = exmessage.Split('*')[1];
                    te.ExInner = exmessage.Split('*')[2];
                    te.ExTargetSite = exmessage.Split('*')[3];
                    te.ExStackTrace = exmessage.Split('*')[4];
                    te.ExHelplink = exmessage.Split('*')[5];
                    te.ExSource = exmessage.Split('*')[6];
                    te.ExHresult = exmessage.Split('*')[7];
                    db.tblExcDets.InsertOnSubmit(te);
                    db.SubmitChanges();
                    Label2.Text = "Can't assign null value into a table id";
                }






        }
        //public static string GetExceptionDetails(Exception ex) 
        //{
        //    var properties = ex.GetType().GetProperties();
        //    var fields  = properties.Select(property=>new{
        //        name = property.Name,value = property.GetValue(ex,null)
        //    }).Select(x => String.Format(
        //                 "{0} = {1}",
        //                 x.name,
        //                 x.value != null ? x.value.ToString() : String.Empty
        //             ));
        //    return String.Join("*", fields);
        //}
        public static string GetExceptionDetails(Exception ex)
        {
            var properties = ex.GetType().GetProperties();
            var fields = properties.Select(property => new
            {
                name = property.Name,
                value = property.GetValue(ex, null)
            }).Select(x => String.Format(
                         "{0}",
                         x.value != null ? x.value.ToString() : String.Empty
                     ));
            return String.Join("*", fields);
        }

    }
}

此外,我在使用LINQ结构将数据插入到sql server数据库中。

1 个答案:

答案 0 :(得分:0)

您是否在设计器中将字段设置为自动生成?如果这不是问题,我建议设置数据上下文操作的日志记录到控制台并检查生成的实际SQL以确保它插入该列,然后向后追溯到找到问题。

 context.Log = Console.Out;

FWIW,我经常设置我的&#34; CreatedTime&#34;和&#34; LastUpdatedTime&#34;列在设计器中自动生成(和只读)并为它们提供合适的默认值或使用数据库触发器设置插入或更新时的值。当您将其设置为自动生成时,即使已修改,也不会将其包含在插入/更新中。如果列不允许空值,则需要提供另一种设置值的方法,即默认约束和/或触发器。

您可能还想尝试显式转换为

SqlDbType.DateTime 

在执行updatechanges之前