错误如下:“/MP201004224”应用程序中的服务器错误。
--------------------------------------------------------------------------------未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。  堆栈跟踪: 
[NullReferenceException: 未将对象引用设置到对象的实例。]
   DB.GetExcelData(String xlsFile) +249
   ImportData.ImportExcelData(String xlsfile) +64
   ImportData.Button1_Click(Object sender, EventArgs e) +104
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565说明:是为了读取excel表的批量数据,然后插入到sqlserver数据库中.
用到的方法如下:
public DataTable GetExcelData(string xlsFile)
    {
        DataSet ds = null;
        try
        {
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + xlsFile + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
            using (OleDbConnection conn = new OleDbConnection(strConn))
            {
                conn.Open();
                //DataSet ds = new DataSet();
                ds = new DataSet();
                string strExcel = "SELECT * FROM [sheet1$]";
                OleDbDataAdapter da = new OleDbDataAdapter(strExcel, conn);
                da.Fill(ds);
                conn.Close();
            }
        }
        catch (Exception ex)
        {
            Console.Write("excel表中可能存在样式或格式不正确!可尝试复制粘贴到新的excel表在导入"+ex.Message);
        }
        return ds.Tables[0];
    } public bool ImportExcelData(string xlsfile)
    {
        bool flag = false;
        DataTable dt = new DB().GetExcelData(xlsfile);
        string brand = "";
        string medianame = "";
        string reporttime = "";
        string banweiname = "";
        string wenzhangtitle = "";
        string baodaozishu = "";
        string wenzhangjidiao = "";
        string area = "";
        string source = "";
        string linkadd = "";
        string advalue = "";
        try
        {
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {                    if (dt.Rows[i][0] != null )
                        brand = dt.Rows[i][0].ToString();    //品牌 
                    if(dt.Rows[i][1]!=null)
                        medianame = dt.Rows[i][1].ToString();   //媒体名称
                    //DateTime time = DateTime.Parse(reporttime);
                    if(dt.Rows[i][3]!=null)
                        reporttime = dt.Rows[i][3].ToString();   //报道时间
                    //reporttime = dr["报道时间"].ToString().Substring(0,11);  //转换成YY-MM-DD
                    if(dt.Rows[i][4]!=null)
                        banweiname = dt.Rows[i][4].ToString();   //版位名称
                    if( dt.Rows[i][5] !=null)
                        wenzhangtitle = dt.Rows[i][5].ToString().Replace("'", "''");    //文章标题
                    if (dt.Rows[i][6] != null)
                    {
                        baodaozishu = dt.Rows[i][6].ToString();   //报道字数
                    }
                    else
                    {
                        baodaozishu = "0";
                    }
                    if(dt.Rows[i][8] != null)
                        wenzhangjidiao = dt.Rows[i][8].ToString();     //文章基调
                    if (dt.Rows[i][9] != null )
                        area = dt.Rows[i][9].ToString();    //区域
                    if(dt.Rows[i][11] !=null)
                        source = dt.Rows[i][11].ToString();    //信息来源
                    if (dt.Rows[i][13] !=null)
                        linkadd = dt.Rows[i][13].ToString();   //链接地址
                    if (dt.Rows[i][17] != null)
                        advalue = dt.Rows[i][17].ToString();     //广告价值
                    //判断数据是否已经存在
                    string sqlstr = "select * from dailynews where reportpro='日常' and reporterid=12577 and brand='" + brand + "' and medianame='" + medianame + "' and reportdate='" + reporttime + "' and pagename='" + banweiname + "' and  title='" + wenzhangtitle + "'  and wordcount='" + baodaozishu + "'  and  newskey='" + wenzhangjidiao + "' and  area='" + area + "'  and  dlaiyuan='" + source + "'   and  comment='" + linkadd + "'  and advalues='" + advalue + "'";
                    DataSet ds = new DB().GetDataSet(sqlstr);
                    if (ds.Tables[0].Rows.Count == 0)
                    {
                        SqlConnection conn1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString);
                        conn1.Open();
                        SqlCommand cmd1 = new SqlCommand();
                        cmd1.Connection = conn1;
                        string sql = "insert into dailynews (reportpro,reporterid,brand,medianame,reportdate,pagename,title,wordcount,newskey,area,dlaiyuan,comment,advalues) values('日常',12577,'" + brand + "','" + medianame + "','" + reporttime + "','" + banweiname + "','" + wenzhangtitle + "','" + baodaozishu + "','" + wenzhangjidiao + "','" + area + "','" + source + "','" + linkadd + "','" + advalue + "')";
                        Console.Write(sql);
                        cmd1.CommandType = CommandType.Text;
                        cmd1.CommandText = sql;
                        cmd1.ExecuteNonQuery();
                        conn1.Close();
                    }
                }
                flag = true;
            }
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
        }
        return flag;
    }
说明:其它页面都运行正常啊,郁闷,是怎么回事呀?高手帮帮忙看看,急急急...... 

解决方案 »

  1.   

    调试到这里,
    DataTable dt = new DB().GetExcelData(xlsfile);
    然后F11跟踪进去,看错误出现在哪里,你调用了一个为Null的对象的方法。
      

  2.   

    补充说明:程序在VS中调试或直接运行都没出异常,一到IIS上运行就出这个问题
      

  3.   

    单步跟踪,有值为null
    操作权限
      

  4.   

    补充说明:发布后在VS里调试也没问题,就是到IIS中运行就出错,晕死了
    我就是管理员呀,操作权限在哪里设置呀