对象引用未设置为对象的实例(但没有任何原因)

时间:2016-05-28 07:52:23

标签: c# asp.net .net exception ado.net

例外:

应用程序中的服务器错误。

Object reference not set to an instance of an object.

描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。

Source Error: 


Line 96:                     }
Line 97: 
Line 98:                     dtMajlisOrKabinat.Merge(dtTotalsalaries);
Line 99:                     dtMajlisOrKabinat.AcceptChanges();
Line 100:                    SqlDataAdapter querycountSalDays = new SqlDataAdapter("select count(distinct CONVERT(varchar,Convert(datetime,InsertedOn,103),103)) as SalaryDate from [SALARY EMP] join Hierarchy on [SALARY EMP].BCODE = Hierarchy.BCODE where SMonth = " + monthvar + " and SYear = " + yearvar + " and gender= " + gender + "   AND (UAdmin like '%" + uname + "%' or UserGroup like '%" + uname + "%')", MyGlobalVariables.con);

Source File: F:\Web Payroll\WebPayroll080316\WebPayroll080316 current\WebPayroll080316\DGrid.aspx.cs    Line: 98 

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   System.Data.Merger.MergeSchema(DataTable table) +1323
   System.Data.Merger.MergeTableData(DataTable src) +36
   System.Data.Merger.MergeTable(DataTable src) +181
   System.Data.DataTable.Merge(DataTable table, Boolean preserveChanges, MissingSchemaAction missingSchemaAction) +161
   System.Data.DataTable.Merge(DataTable table) +13
   WebPayroll080316.DGrid.btnshowgrid_Click(Object sender, EventArgs e) in F:\Web Payroll\WebPayroll080316\WebPayroll080316  current\WebPayroll080316\DGrid.aspx.cs:98
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9696694
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +204
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.1055.0`

异常细节

.Cs代码

using System;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web.UI.WebControls;

namespace mynamespace
{
public partial class GridViewClass : System.Web.UI.Page
{
    static int gender = 1;
    static bool DataLevel = true;

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

    protected void btnshowgrid_Click(object sender, EventArgs e)
    {


        string constr = "Data Source= MyMachineName,1433;Initial Catalog= dbname;Connection Timeout=1 ;User ID= 'username' ;Password= 'password'";
        MyGlobalVariables.con = new SqlConnection(constr);
        MyGlobalVariables.con.Open();



        string uname;


        uname = "myuser";

        int monthvar = ddlmonth.SelectedIndex + 1;
        int yearvar = Convert.ToInt16(ddlyear.SelectedValue);

        SqlCommand queryEmpCount;
        if(DataLevel)
        {
            queryEmpCount = new SqlCommand("StoredProDeptAndEmpCount", MyGlobalVariables.con);

        }
        else
        {
            queryEmpCount = new SqlCommand("StoredProAreaAndEmpCount", MyGlobalVariables.con);
        }

        queryEmpCount.CommandType = CommandType.StoredProcedure;
        SqlParameter Gender = queryEmpCount.Parameters.AddWithValue("Gender", gender);

        SqlParameter Uname = queryEmpCount.Parameters.AddWithValue("Uname", uname);

        SqlParameter joindate = queryEmpCount.Parameters.AddWithValue("joindate", "01-05-2016");


        SqlDataReader EmpCountdata = queryEmpCount.ExecuteReader();
        DataTable dtDeptorArea = new DataTable();

        dtDeptorArea.Load(EmpCountdata);

        if(dtDeptorArea.Rows.Count > 0)
        {
            SqlDataAdapter queryTotalSalary;

            if(DataLevel)
            {
                dtDeptorArea.PrimaryKey = new DataColumn[] { dtDeptorArea.Columns["DeptCode"] };
                queryTotalSalary = new SqlDataAdapter("select  Hierarchy.DeptCode , mydate.CountEmptotal from Hierarchy full outer join  (select COUNT(EmpCode) as  CountEmptotal,  Hierarchy.DeptCode  from [SALARY EMP] join Hierarchy on Hierarchy.BCODE= [SALARY EMP].BCODE where [SALARY EMP].BCODE in (select BCODE from [SALARY EMP] where (UAdmin like '%" + uname + "%' or UserGroup like '%" + uname + "%') and SMonth = " + monthvar + " and SYear = " + yearvar + " and Hierarchy.gender =" + gender + ") and (UAdmin like '%" + uname + "%' or UserGroup like '%" + uname + "%') and SMonth = " + monthvar + " and SYear = " + yearvar + "   group by Hierarchy.DeptCode)   as mydate  on Hierarchy.DeptCode= mydate.DeptCode where (UAdmin like '%" + uname + "%' or UserGroup like '%" + uname + "%') and gender =" + gender + " group by Hierarchy.Majlisname, Hierarchy.DeptCode, mydate.DeptCode, mydate.CountEmptotal order by Hierarchy.DeptCode", MyGlobalVariables.con);
            }
            else
            {
                dtDeptorArea.PrimaryKey = new DataColumn[] { dtDeptorArea.Columns["AreaCode"] };

                queryTotalSalary = new SqlDataAdapter("select  Hierarchy.AreaCode , mydate.CountEmptotal from Hierarchy full outer join  (select COUNT(EmpCode) as  CountEmptotal,  Hierarchy.AreaCode  from [SALARY EMP] join Hierarchy on Hierarchy.BCODE= [SALARY EMP].BCODE where [SALARY EMP].BCODE in (select BCODE from [SALARY EMP] where (UAdmin like '%" + uname + "%' or UserGroup like '%" + uname + "%') and SMonth = " + monthvar + " and SYear = " + yearvar + " and Hierarchy.gender =" + gender + ") and (UAdmin like '%" + uname + "%' or UserGroup like '%" + uname + "%') and SMonth = " + monthvar + " and SYear = " + yearvar + "   group by Hierarchy.AreaCode)   as mydate  on Hierarchy.AreaCode= mydate.AreaCode where (UAdmin like '%" + uname + "%' or UserGroup like '%" + uname + "%') and gender =" + gender + " group by Hierarchy.kbname, Hierarchy.AreaCode, mydate.AreaCode, mydate.CountEmptotal order by Hierarchy.AreaCode", MyGlobalVariables.con);
            }
            DataTable dtTotalsalaries = new DataTable();
            queryTotalSalary.Fill(dtTotalsalaries);

            if(DataLevel)
            {

                dtTotalsalaries.PrimaryKey = new DataColumn[] { dtTotalsalaries.Columns["DeptCode"] };

            }
            else
            {
                dtTotalsalaries.PrimaryKey = new DataColumn[] { dtTotalsalaries.Columns["AreaCode"] };
            }

            dtDeptorArea.Merge(dtTotalsalaries);
            dtDeptorArea.AcceptChanges();

            grdvsalaryreport.DataSource = dtDeptorArea;
            grdvsalaryreport.DataBind();
        }
    }

注意 all both procedure are working and also successfully filled in data table

此行发生异常:

dtDeptorArea.Merge(dtTotalsalaries);

0 个答案:

没有答案