I get the following error in the browser View
in jTable after the code has complied and ran:
Object reference not set to an instance of an object.
I debugged the code and at this line of the code
newlist = newlist.OrderBy(item => GetPropertyValue(item, sortExpression)).ToList();
I get the mentioned error. I also confirmed it by commenting out this line of the code.
I am trying to do the sorting
by hitting the table's column headers, but it's not doing it and gives the error out. Been on it for good few weeks trying to figure out but it just doesn't seem to work. Any help would be a great help and thanks in advance.
Here's the full code for your inspection:
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.Date).ToList().GetRange(jtStartIndex, jtPageSize);
if (string.IsNullOrEmpty(jtSorting)) { jtSorting = "Date 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 });
}
Edit: GetPropertyValue method:
public static object GetPropertyValue(object obj, string propertyName)
{
return obj == null ? null : obj.GetType().GetProperty(propertyName).GetValue(obj, null);
}