简体   繁体   中英

Gettting Object reference not set to an instance of an object. using linq c# datatable angularjs?

I am create one small demo for view user list using datatable angularjs.in this datatable also include seraching,pagination,etc..and in sql server my table conatain this filed firstname,lastname,email,phoneno then email column is null. then first time load page my data will be show very well.but apply any pagination ,searching then getting error like this "Object reference not set to an instance of an object." form the my query using linq c#.

here is my c# code :

public ActionResult GetUserList(string searchRequest, string fromDate, string toDate)
    {
        try
        {
            var draw = Request.Form.GetValues("draw").FirstOrDefault();            

            var searchValue = Request.Form.GetValues("search[value]").FirstOrDefault();     

            List<UserInfo> UserList = new List<UserInfo>();

            if (searchRequest != null)
            {
                if (searchRequest == "All Time")
                    UserList = db.UserInfo.ToList();
                else if (searchRequest == "Today")
                {
                    DateTime date = DateTime.Today;
                    UserList = db.UserInfo.Where(t => DbFunctions.TruncateTime(t.datetime) == DbFunctions.TruncateTime(date)).ToList();
                }                   
            }

            //search
            if (!string.IsNullOrEmpty(searchValue))
            {
                UserList = UserList.Where(a =>
                    a.firstname.Contains(searchValue) ||
                    a.lastname.Contains(searchValue) ||
                    a.email.Contains(searchValue) ||
                    a.phoneno.Contains(searchValue) 
                    ).ToList();
            }


            var Details = UserList.Select(h => new
            {                
                h.firstname,
                h.lastname,                   
                h.email,
                h.phoneno,
                fullName = h.firstname + " " + h.lastname
            });
            return Json(new { draw = draw, data = Details });
        }
        catch (Exception ex)
        {
            throw;
        }

        return Json(null);
    }

this is my code and enter any value for the searching getting this error.i know why this error is getting in table my email column is null so email contain data or not so how maintain email in query any one know then please let me know?

replace the "Search" section by the following:

 if (!string.IsNullOrEmpty(searchValue))
        {
            UserList = UserList.Where(a =>
                (a.firstname != null ? a.firstname.Contains(searchValue) :false) ||
                (a.lastname != null ? a.lastname.Contains(searchValue) : false)  ||
                (a.email != null ?  a.email.Contains(searchValue) : false) ||
                (a.phoneno != null ? a.phoneno.Contains(searchValue) : false) 
                ).ToList();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM