Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对象引用未设置为对象的实例(但没有任何原因)_C#_Asp.net_.net_Exception_Ado.net - Fatal编程技术网

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

C# 对象引用未设置为对象的实例(但没有任何原因),c#,asp.net,.net,exception,ado.net,C#,Asp.net,.net,Exception,Ado.net,例外情况: dtDeptorArea.Merge(dtTotalsalaries); “/”应用程序中出现服务器错误 Object reference not set to an instance of an object. 描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源 异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例 Source Error: Line 96:

例外情况:

dtDeptorArea.Merge(dtTotalsalaries);
“/”应用程序中出现服务器错误

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();
        }
    }
注意

此行出现异常:

dtDeptorArea.Merge(dtTotalsalaries);

请显示完整的堆栈跟踪,并将其简化为。一步一步地调试代码…我调试所有代码,但没有找到原因。异常不在您所说的行上。您可以找到实现。看起来您正在合并无法合并的内容。合并前查看两个表的内容。请显示完整的堆栈跟踪,并将其简化为一个。一步一步地调试代码…我调试所有代码,但没有找到原因。异常不在您所说的行上。您可以找到实现。看起来您正在合并无法合并的内容。合并前查看两个表的内容。