Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I hope you can help me.

I am trying to sort the jQuery JTable columns, but when I click on the table headers for sorting I get this error "Object reference not set to an instance of an object" in this line of the code and the marked ones is the real culprit.

C#
if (sortDirection == SortDirection.ASC)
                   {
                    // newlist = newlist.OrderBy(item => GetPropertyValue(item, sortExpression)).ToList(); // this one gives error when the page loads in view. So I always comment it out the line
                   }
                   else
                   {
                    newlist = newlist.OrderByDescending(item => GetPropertyValue(item, sortExpression)).ToList(); // this is gives when I sort the columns in View
                   }


Here is my full code for your inspection.

C#
internal enum SortDirection { ASC, DESC }

       public static object GetPropertyValue(object obj, string propertyName)
       {
           return obj == null ? null : obj.GetType().GetProperty(propertyName).GetValue(obj, null);
       }

       [HttpPost]
       public JsonResult TopPlayedInVenueList1(string StartDate = "", string EndDate = "", int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
       {
           try
           {

               if (Request.IsAuthenticated == true)
               {
                   string Path = @"C:\\5Newwithdate-1k.xls";
                   OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= '" + Path + "';Extended Properties=" + (char)34 + "Excel 8.0;IMEX=1;" + (char)34 + "");
                   OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", con);
                   con.Close();
                   System.Data.DataTable data = new System.Data.DataTable();
                   da.Fill(data);

                   List<TopPlayed> daa = new List<TopPlayed>();

                   foreach (DataRow p in data.Rows)
                   {
                       TopPlayed top = new TopPlayed()
                       {
                           TrackID = Convert.ToInt32(p.Field<double>("TrackID")),
                           Date = p.Field<DateTime>("DateTimes"),
                           TrackName = p.Field<string>("TrackName"),
                           ArtistName = p.Field<string>("ArtistName"),
                           Times = Convert.ToInt32(p.Field<double>("Times"))
                       };

                       daa.Add(top);
                   }

                   var listOrder = daa.Where(i => i.Date >= Convert.ToDateTime(StartDate) && i.Date <= Convert.ToDateTime(EndDate)).ToList();

                   if (jtStartIndex + 150 > listOrder.ToList().Count)
                   {
                       int val = listOrder.ToList().Count - jtStartIndex;
                       jtPageSize = val;
                   }

                   var newlist = listOrder.OrderByDescending(i => i.Times).ToList().GetRange(jtStartIndex, jtPageSize);

                   if (string.IsNullOrEmpty(jtSorting)) { jtSorting = "Times ASC"; }

                    SortDirection sortDirection = jtSorting.ToLower().Contains("desc") ? SortDirection.DESC : SortDirection.ASC;
                    string sortExpression = sortDirection == SortDirection.DESC ? jtSorting.ToLower().Replace(" desc", "") :
                    jtSorting.ToLower().Contains(" asc") ? jtSorting.ToLower().Replace(" asc", "") : jtSorting;

                   if (sortDirection == SortDirection.ASC)
                   {
                 // newlist = newlist.OrderBy(item => GetPropertyValue(item, sortExpression)).ToList();
                   }
                   else
                   {
                    newlist = newlist.OrderByDescending(item => GetPropertyValue(item, sortExpression)).ToList();
                   }

                   return Json(new { Result = "OK", Records = newlist, TotalRecordCount = listOrder.ToList().Count });
               }
               return Json(new { Result = "ERROR" });
           }
           catch (Exception ex)
           {
               return Json(new { Result = "ERROR", Message = ex.Message });
           }
       }

I am stuck in this sorting bit for almost two months now and I hope that someone can help here. Thanks in advance.
Posted
Comments
Herman<T>.Instance 12-May-14 11:20am    
do you know by debugging what exactly is null? Leads GetPropertyValue to NULL?
MNamrata 12-May-14 11:26am    
Did you set sortexpression?
Member 10812800 13-May-14 2:42am    
Thanks for your reply. I have set sortExpression here only.

string sortExpression = sortDirection == SortDirection.DESC ? jtSorting.ToLower().Replace(" desc", "") :
jtSorting.ToLower().Contains(" asc") ? jtSorting.ToLower().Replace(" asc", "") : jtSorting;
Member 10812800 13-May-14 2:41am    
Thanks for your reply. GetPropertyValue seems to be NULL as it's not giving any value in debug. Though, while sorting the header columns, sortExpression shows table header values.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900