2012-06-06 37 views
0

我越來越對象引用不設置到對象的實例,即使我的列表不是空對象引用未設置爲一個對象的數據表的一個實例

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); 
    } 
} 

我在數據表DT得到錯誤= (數據表)OBJLIST;在else子句中。

有什麼想法或建議嗎?

謝謝,

+1

什麼是eslist ?你爲什麼認爲它可以被轉換成一個'DataTable'? –

+0

eslist是函數中的記錄列表。我需要DataTable進行分頁。 pds是pageddatasource。 – asifa

+0

任何建議或想法,請緊急 – asifa

回答

1

有一個在數據的問題。一些值爲null。通過給數據變量分配一個默認值來解決。

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

相關問題