0

リストがnullでないのに、オブジェクト参照がオブジェクトのインスタンスに設定されていません

  protected void gridview_bind()
{
    try
    {
        CompanyMasterClass co = new CompanyMasterClass();
        co.strcompany_code = Request.Cookies["userinfo"]["companycode"].ToString();
        ResultClass objress = co.fn_GetNameNumberStyle();
        if (objress.bStatus)
        {
            eslist<CompanyMasterClass> OBJLISTS = objress.objData as eslist<CompanyMasterClass>;

            if (OBJLISTS.Count > 0)
            {
                if (OBJLISTS[0].strdatestyle.ToString() == "DM")
                {
                    mskextCreatedOn.Mask = "99/99/9999";
                    mskextCreatedOn.CultureName = "en-GB";
                    //mskextCreatedOn.UserDateFormat="DayMonthYear";
                    mskextUpdatedOn.Mask = "99/99/9999";
                    mskextUpdatedOn.CultureName = "en-GB";
                }
                if (OBJLISTS[0].strdatestyle.ToString() == "MD")
                {
                    mskextCreatedOn.Mask = "99/99/9999";
                    mskextCreatedOn.CultureName = "en-US";
                    //mskextCreatedOn.UserDateFormat="DayMonthYear";
                    mskextUpdatedOn.Mask = "99/99/9999";
                    mskextUpdatedOn.CultureName = "en-US";
                }
            }
        }
        //mm.getlookup(ddlUserType, "user_type", "s");
        CustomerMasterClass cs = new CustomerMasterClass();

        ResultClass objres = cs.fn_GetCustomerMasterGrid();
        if (objres.bStatus)
        {
            eslist<CustomerMasterClass> OBJLIST = objres.objData as eslist<CustomerMasterClass>;
            if (OBJLIST.Count > 0)
            {
                if ((bool)ViewState["filter"] == true)
                {
                    DataTable dts = (DataTable)ViewState["searchtable"];
                    pds.DataSource = dts.DefaultView;
                    pds.AllowPaging = true;
                    pds.PageSize = Convert.ToInt16(ddlPerPage.SelectedValue);
                    //pds.PageSize = 5;
                    DataView dvs = dts.DefaultView;

                    if (this.ViewState["SortExp"] != null)
                    {
                        dvs.Sort = this.ViewState["SortExp"].ToString()
                                 + " " + this.ViewState["SortOrder"].ToString();
                    }
                    pds.CurrentPageIndex = CurrentPage;
                    lnkbtnNext.Enabled = !pds.IsLastPage;
                    lnkbtnPrevious.Enabled = !pds.IsFirstPage;
                    if (pds.IsLastPage)
                    {
                        //lnkbtnNext.Style.Add("color", "#CCCCCC");
                        lnkbtnNext.Style.Add("background-color", "#CCCCCC");
                        lnkbtnNext.Style.Add("border-width", "0px");
                    }
                    else
                    {
                        //lnkbtnNext.Style.Remove("color");
                        lnkbtnNext.Style.Remove("background-color");
                        lnkbtnNext.Style.Remove("border-width");
                    }
                    if (pds.IsFirstPage)
                    {
                        //lnkbtnPrevious.Style.Add("color", "#CCCCCC");
                        lnkbtnPrevious.Style.Add("background-color", "#CCCCCC");
                        lnkbtnPrevious.Style.Add("border-width", "0px");
                    }
                    else
                    {
                        //lnkbtnPrevious.Style.Remove("color");
                        lnkbtnPrevious.Style.Remove("background-color");
                        lnkbtnPrevious.Style.Remove("border-width");
                    }
                    gvCustomerMaster.DataSource = pds;
                    gvCustomerMaster.DataBind();
                    doPaging();
                    // gvGroupMaster.DataSource = OBJLIST;
                    //gvGroupMaster.DataBind();
                    gridmain.Visible = true;
                    //frmvwGroup.Visible = false;
                    lblTotalRecords.Text = dts.Rows.Count.ToString();

                }
                else
                {
                        DataTable dt = (DataTable)OBJLIST;
                        //dt = OBJLIST.ToString();
                        ViewState["nosearch"] = dt;
                        pds.DataSource = dt.DefaultView;
                        pds.AllowPaging = true;
                        pds.PageSize = Convert.ToInt16(ddlPerPage.SelectedValue);
                        //pds.PageSize = 5;
                        DataView dv = dt.DefaultView;

                        if (this.ViewState["SortExp"] != null)
                        {
                            dv.Sort = this.ViewState["SortExp"].ToString()
                                     + " " + this.ViewState["SortOrder"].ToString();
                        }
                        pds.CurrentPageIndex = CurrentPage;
                        lnkbtnNext.Enabled = !pds.IsLastPage;
                        lnkbtnPrevious.Enabled = !pds.IsFirstPage;
                        if (pds.IsLastPage)
                        {
                            //lnkbtnNext.Style.Add("color", "#CCCCCC");
                            lnkbtnNext.Style.Add("background-color", "#CCCCCC");
                            lnkbtnNext.Style.Add("border-width", "0px");
                        }
                        else
                        {
                            //lnkbtnNext.Style.Remove("color");
                            lnkbtnNext.Style.Remove("background-color");
                            lnkbtnNext.Style.Remove("border-width");
                        }
                        if (pds.IsFirstPage)
                        {
                            //lnkbtnPrevious.Style.Add("color", "#CCCCCC");
                            lnkbtnPrevious.Style.Add("background-color", "#CCCCCC");
                            lnkbtnPrevious.Style.Add("border-width", "0px");
                        }
                        else
                        {
                            //lnkbtnPrevious.Style.Remove("color");
                            lnkbtnPrevious.Style.Remove("background-color");
                            lnkbtnPrevious.Style.Remove("border-width");
                        }
                        gvCustomerMaster.DataSource = pds;
                        gvCustomerMaster.DataBind();
                        doPaging();
                        // gvGroupMaster.DataSource = OBJLIST;
                        //gvGroupMaster.DataBind();
                        gridmain.Visible = true;
                        //frmvwGroup.Visible = false;
                        lblTotalRecords.Text = OBJLIST.Count.ToString();
                        //this.GetUsersPageWise(1);

                }
            }
        }
    }
    catch (Exception ex)
    {
        ErrorLogClass ObjError = new ErrorLogClass("customer_master.aspx", "customer_master()", ex);
    }
}

DataTable dt =(DataTable)OBJLIST;でエラーが発生します。else節で。

何かアイデアや提案をお願いしますか?

ありがとう、

4

2 に答える 2

1

データに問題がありました。一部の値は null です。データ変数にデフォルト値を割り当てることで解決しました。

于 2012-07-09T06:21:21.243 に答える
0

このようなデータテーブルにリストをキャストすることはできません。どちらかを使用

static DataTable ConvertListToDataTable(eslist<CompanyMasterClass> list)
{
    // New table.
    DataTable table = new DataTable();

    // Get max columns.
    int columns = 0;
    foreach (var array in list)
    {
    if (array.Length > columns)
    {
        columns = array.Length;
    }
    }

    // Add columns.
    for (int i = 0; i < columns; i++)
    {
    table.Columns.Add();
    }

    // Add rows.
    foreach (var array in list)
    {
    table.Rows.Add(array);
    }

    return table;
}

または、次のリンクを参照して くださいhttp://www.codeproject.com/Articles/83271/Convert-a-Generic-List-to-a-Datatable

于 2012-06-06T09:57:47.103 に答える