首页 新闻 会员 周边 捐助

Global.asax中执行表插入数据操作时出现【未将对象引用到实例】错误,求助!

0
悬赏园豆:30 [已解决问题] 解决于 2012-05-30 15:09

我在Global.asax中实现定时发送邮件,并记录到数据表中的需求,现在邮件可以发送成功,但往数据表Insert的时候总是出现【未将对象引用到实例】,如图:

 

单独拿出来放在普通.aspx.cs里执行可以(参数一样),为什么在Global.asax里就会出现这样的错误呢? 是不是有什么限制?请知道的朋友赐教,谢谢。

附:在Global.asax中的代码如下:

View Code
 try
            {
                //smtp.Send(message);
                TradeSite.WebSiteSet.Model.IEmailQueueInfo4 emailinfo = new TradeSite.WebSiteSet.Model.EmailQueueInfo();
                //emailinfo.EmailPriority = 0;
                //emailinfo.BodyFormat = 0;
                emailinfo.strEmailTo = MailTo;
                emailinfo.strEmailFrom = MailFrom;
                emailinfo.strEmailSubject = MailSubject;
                emailinfo.strEmailBody = MailContent;
                emailinfo.dtCreateDate = DateTime.Now;
                emailinfo.dtSendDate = DateTime.Now;
                //emailinfo.esEmailStatus = -1;
                emailinfo.EmailBodyFormat = System.Web.Mail.MailFormat.Html;
                emailinfo.strEmailError = "";
                emailinfo.strURL = "";
                if (emailinfo != null)
                {
                    TradeSite.WebSiteSet.BLL.EmailQueue.Insert(emailinfo);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

在普通.aspx.cs中执行的代码如下:

View Code
            TradeSite.WebSiteSet.Model.IEmailQueueInfo4 emailinfo = new TradeSite.WebSiteSet.Model.EmailQueueInfo();
            //emailinfo.EmailPriority = 0;
            //emailinfo.BodyFormat = 0;
            emailinfo.strEmailTo = "skybirdzw@163.com";
            emailinfo.strEmailFrom = "wxrczkw@163.com";
            emailinfo.strEmailSubject = "测试";
            emailinfo.strEmailBody = "测试111";
            emailinfo.dtCreateDate = DateTime.Now;
            emailinfo.dtSendDate = DateTime.Now;
            //emailinfo.esEmailStatus = -1;
            emailinfo.EmailBodyFormat = System.Web.Mail.MailFormat.Html;
            emailinfo.strEmailError = "";
            emailinfo.strURL = "";
            TradeSite.WebSiteSet.BLL.EmailQueue.Insert(emailinfo);
skybirdzw的主页 skybirdzw | 初学一级 | 园豆:3
提问于:2012-05-29 18:15
< >
分享
最佳答案
1

代码是放在Global.asax 的哪个方法中的?

收获园豆:22
dudu | 高人七级 |园豆:30778 | 2012-05-29 18:44

这是整个Global.asax中的测试代码,我在Application_Start中调用其他方法,这些被调用的方法都写在Global.asax中的。

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace WebApplication2
{
    public class Global : System.Web.HttpApplication
    {
        protected string fromEmailAddress = "wxrczkw@163.com";
        protected string fromEmailUser = "wxrczkw";
        protected string fromEmailPassWord = "******";
        protected string fromEmailSMTP = "smtp.163.com";
        void Application_Start(object sender, EventArgs e)
        {
            System.Timers.Timer timer = new System.Timers.Timer(10000); 
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            timer.AutoReset = true;
            timer.Enabled = true;
        }

        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (DateTime.Now.Hour == 18)
            {
                ScanMember();
            }
        }

        void ScanMember()
        {
            IList<TradeSite.Member.Model.MemberInfo> memberinfolist = TradeSite.Member.BLL.Member.GetAll();
            if (memberinfolist.Count > 0)
            {
                foreach (TradeSite.Member.Model.MemberInfo memberinfo in memberinfolist)
                {
                    if (memberinfo.ID == 469)
                    {
                        SendMail(fromEmailAddress, memberinfo.EMail, "网站未登录提醒", "您已经一个月未登录本站了!【rczkw.com】");
                    }
                }
            }
        }

        public void SendMail(string MailFrom, string MailTo, string MailSubject, string MailContent)
        {
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            message.To.Add(MailTo);
            message.Subject = MailSubject;
            message.From = new System.Net.Mail.MailAddress(MailFrom);
            message.Body = MailContent;
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.163.com");
            smtp.Credentials = new System.Net.NetworkCredential("wxrczkw", "******");
            try
            {
                smtp.Send(message);
                TradeSite.WebSiteSet.Model.IEmailQueueInfo4 emailinfo = new TradeSite.WebSiteSet.Model.EmailQueueInfo();
                emailinfo.strEmailTo = MailTo;
                emailinfo.strEmailFrom = MailFrom;
                emailinfo.strEmailSubject = MailSubject;
                emailinfo.strEmailBody = MailContent;
                emailinfo.dtCreateDate = DateTime.Now;
                emailinfo.dtSendDate = DateTime.Now;
                emailinfo.EmailBodyFormat = System.Web.Mail.MailFormat.Html;
                emailinfo.strEmailError = "";
                emailinfo.strURL = "";
                if (emailinfo != null)
                {
                    TradeSite.WebSiteSet.BLL.EmailQueue.Insert(emailinfo);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        /// <summary>
        /// 计算两个日期的时间间隔
        /// </summary>
        /// <param name="DateTime1">第一个日期和时间</param>
        /// <param name="DateTime2">第二个日期和时间</param>
        /// <returns>返回天数</returns>
        private int DateDiff(DateTime DateTime1, DateTime DateTime2)
        {
            int dateDiff = 0;
            TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
            TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
            TimeSpan ts = ts1.Subtract(ts2).Duration();
            dateDiff = Convert.ToInt32(ts.Days);
            return dateDiff;
        } 
        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}
skybirdzw | 园豆:3 (初学一级) | 2012-05-30 09:03

@skybirdzw: 把 try { 移至 if (emailinfo != null) 上面,确认是不是 TradeSite.WebSiteSet.BLL.EmailQueue.Insert(emailinfo); 引发的异常?

dudu | 园豆:30778 (高人七级) | 2012-05-30 11:53

@dudu: 还是报一样的错:

View Code
try
{
    if (emailinfo != null)
    {
        //换成这样写法也不行
        TradeSite.WebSiteSet.BLL.EmailQueue bll = (TradeSite.WebSiteSet.BLL.EmailQueue)Assembly.Load("TradeSite.WebSiteSet.Component").CreateInstance("TradeSite.WebSiteSet.BLL.EmailQueue");
        bll.InsertData(emailinfo);
    }
}
catch (Exception ex)
{
    throw ex;   
}
skybirdzw | 园豆:3 (初学一级) | 2012-05-30 13:50

@skybirdzw: 那可能是 Assembly.Load("TradeSite.WebSiteSet.Component").CreateInstance("TradeSite.WebSiteSet.BLL.EmailQueue"); 返回的是null。

dudu | 园豆:30778 (高人七级) | 2012-05-30 13:56

@dudu: 其实我觉得就是 TradeSite.WebSiteSet.BLL.EmailQueue.Insert(emailinfo);执行的时候出现的错误;我仔细查看了方法并没有发现什么异常写法,以下附上,请各位参考下:

View Code
//BLL层
public static void Insert(IEmailQueueInfo4 info)
        {
            IDAL.IEmailQueue dal = DALFactory.EmailQueue.Create();
            dal.Insert(info);
        }

//DAL层
 public void Insert(IEmailQueueInfo4 info)
        {
            if (info != null)
            {
                GetInsertParameters(info);
                ExecuteNonQuery();
            }
        }

 private void GetInsertParameters(IEmailQueueInfo4 info)
        {
            cmd = db.GetSqlStringCommand(SQL_INSERT);
            db.AddInParameter(cmd, PARM_PRIVORITY,   DbType.Int32, (int)info.EmailPriority);
            db.AddInParameter(cmd, PARM_BODY_FORMAT, DbType.Int32, info.EmailBodyFormat);
            db.AddInParameter(cmd, PARM_TO,          DbType.String, info.strEmailTo);
            db.AddInParameter(cmd, PARM_CC,          DbType.String, info.strEmailCc);
            db.AddInParameter(cmd, PARM_BCC,         DbType.String, info.strEmailBcc);
            db.AddInParameter(cmd, PARM_FROM,        DbType.String, info.strEmailFrom);
            db.AddInParameter(cmd, PARM_SUBJECT,     DbType.String, info.strEmailSubject);
            db.AddInParameter(cmd, PARM_BODY,        DbType.String, info.strEmailBody);
            db.AddInParameter(cmd, PARM_CREAT_DATE,  DbType.DateTime, info.dtCreateDate = DateTime.Now);
            db.AddInParameter(cmd, PARM_SEND_DATE,   DbType.DateTime, info.strSendDate);
            db.AddInParameter(cmd, PARM_STATUS,      DbType.Int32, (int)info.esEmailStatus);
            db.AddInParameter(cmd, PARM_URL,         DbType.String, HttpContext.Current.Request.RawUrl);
            db.AddInParameter(cmd, PARM_ERROR,       DbType.String, info.strEmailError);

        }

 public void ExecuteNonQuery()
        {
            if (db != null && cmd != null)
            {
                db.ExecuteNonQuery(cmd);
            }
        }
skybirdzw | 园豆:3 (初学一级) | 2012-05-30 14:01

@skybirdzw: 建议找出null究竟在哪里出现的,比如在 dal.Insert(info); 之前加上代码:

if(dal == null)
{
    throw new Exception("dal is null");
}
dudu | 园豆:30778 (高人七级) | 2012-05-30 14:12

@dudu: 彻底崩溃。。

加了异常输出却没执行到,说明对象都不是null啊,我是这样加的:

View Code
//BLL层
public static void Insert(IEmailQueueInfo4 info)
        {
            IDAL.IEmailQueue dal = DALFactory.EmailQueue.Create();
            if (dal == null)
            {
                throw new Exception("dal is null");
            }
            else
            {
                dal.Insert(info);
            }
        }

        public void InsertData(IEmailQueueInfo4 info)
        {
            IDAL.IEmailQueue dal = DALFactory.EmailQueue.Create();
            if (dal == null)
            {
                throw new Exception("dal is null");
            }
            else
            {
                dal.Insert(info);
            }
        }

Global.asax.cs中在操作之前也加了,同样没执行到,一下就跳到了throw ex;

View Code
TradeSite.WebSiteSet.BLL.EmailQueue bll = (TradeSite.WebSiteSet.BLL.EmailQueue)Assembly.Load("TradeSite.WebSiteSet.Component").CreateInstance("TradeSite.WebSiteSet.BLL.EmailQueue");
                    if (bll == null)
                    {
                        throw new Exception("bll is null");
                    }
                    else
                    {
                        bll.InsertData(emailinfo);
                    }
skybirdzw | 园豆:3 (初学一级) | 2012-05-30 14:27

@skybirdzw: 注释掉 smtp.Send(message); 与  TradeSite.WebSiteSet.BLL.EmailQueue.Insert(emailinfo); 看会不会出现异常。

dudu | 园豆:30778 (高人七级) | 2012-05-30 14:30

@dudu: 找到问题所在了!原来是DAL层的 GetInsertParameters方法搞的鬼!里面有一个给参数赋值:db.AddInParameter(cmd, PARM_URL, DbType.String, HttpContext.Current.Request.RawUrl == null ? "" : HttpContext.Current.Request.RawUrl);

而HttpContext.Current.Request.RawUrl似乎只对单次请求有效,在回发状态请求中就是null!

虽然郁闷了半天,好在终于知道问题所在了,谢谢你!

skybirdzw | 园豆:3 (初学一级) | 2012-05-30 14:49
其他回答(4)
0

看看异常的详细信息啊,第几行代码出错,空引用的异常最容易解决了。

我觉得是 TradeSite.WebSiteSet.BLL.EmailQueue.Insert(emailinfo);有问题

你的EmailQueue是不是需要初始化,在木有初始化的时候就调用了静态方法

收获园豆:2
Sanco | 园豆:236 (菜鸟二级) | 2012-05-29 18:32
TradeSite.WebSiteSet.Model.IEmailQueueInfo4 emailinfo = new TradeSite.WebSiteSet.Model.EmailQueueInfo();
            //emailinfo.EmailPriority = 0;
            //emailinfo.BodyFormat = 0;
            emailinfo.strEmailTo = "skybirdzw@163.com";
            emailinfo.strEmailFrom = "wxrczkw@163.com";
            emailinfo.strEmailSubject = "测试";
            emailinfo.strEmailBody = "测试111";
            emailinfo.dtCreateDate = DateTime.Now;
            emailinfo.dtSendDate = DateTime.Now;
            //emailinfo.esEmailStatus = -1;
            emailinfo.EmailBodyFormat = System.Web.Mail.MailFormat.Html;
            emailinfo.strEmailError = "";
            emailinfo.strURL = "";

这些就是初始化的啊。单独拿出来执行就没问题,放在Global.asax中就不行了。

支持(0) 反对(0) skybirdzw | 园豆:3 (初学一级) | 2012-05-29 18:39

@skybirdzw: 你说的这个是IEmailQueueInfo4的实例化

我说的是EmailQueue的初始化,里面是不是有一些静态变量需要初始化

支持(0) 反对(0) Sanco | 园豆:236 (菜鸟二级) | 2012-05-29 20:56

@Sanco: 这是BLL层方法代码:

View Code
/// <summary>
        /// 插入数据
        /// </summary>
        /// <param name="info"></param>
        public static void Insert(IEmailQueueInfo4 info)
        {
            IDAL.IEmailQueue dal = DALFactory.EmailQueue.Create();
            dal.Insert(info);
        }

里面没有任何静态成员的。

支持(0) 反对(0) skybirdzw | 园豆:3 (初学一级) | 2012-05-30 09:26
0

跟踪一下,看看具体是什么对象为空!

收获园豆:2
zqwuwei | 园豆:148 (初学一级) | 2012-05-29 19:14

跟踪不了,项目是分模块编译后放在服务器上拼起来的,只能分模块调试,在引用到其他模块时就跟踪不到了。

支持(0) 反对(0) skybirdzw | 园豆:3 (初学一级) | 2012-05-30 08:50

@skybirdzw: 把异常的详细信息贴出来看看。详细信息里面会说运行到哪行出错了,会说那个对象为null.

支持(0) 反对(0) zqwuwei | 园豆:148 (初学一级) | 2012-05-30 10:16

@zqwuwei: 

显示在跟踪信息是:

   在 WebApplication2.Global.SendMail(String MailFrom, String MailTo, String MailSubject, String MailContent) 位置 G:\WebApplication2\WebApplication2\Global.asax.cs:行号 141
   在 WebApplication2.Global.ScanMember() 位置 G:\WebApplication2\WebApplication2\Global.asax.cs:行号 61
   在 WebApplication2.Global.timer_Elapsed(Object sender, ElapsedEventArgs e) 位置 G:\WebApplication2\WebApplication2\Global.asax.cs:行号 44
   在 System.Timers.Timer.MyTimerCallback(Object state)

-------------------

显示的行号对应的代码:

行号 141:throw ex; 

行号 61: SendMail(fromEmailAddress, memberinfo.EMail, "网站未登录提醒", "您已经一秒钟未登录本站了!【rczkw.com】");

 行号 44:ScanMember();

支持(0) 反对(0) skybirdzw | 园豆:3 (初学一级) | 2012-05-30 11:36
0

.aspx.cs 文件中的命名空间 引用到 在Global.asax中试一下。

收获园豆:2
傲雪江南 | 园豆:174 (初学一级) | 2012-05-29 19:52

.aspx.cs就比Global.asax.cs多了这两个引用:

using System.Web.UI;
using System.Web.UI.WebControls;

支持(0) 反对(0) skybirdzw | 园豆:3 (初学一级) | 2012-05-30 09:52
0

我怀疑你的dbhelpr 有执行顺序问题

收获园豆:2
慧☆星 | 园豆:5722 (大侠五级) | 2012-05-30 10:53

没有吧,不放在Global.asax里就能运行正常,我崩溃了!

支持(0) 反对(0) skybirdzw | 园豆:3 (初学一级) | 2012-05-30 11:03
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册