Forums - C# Corner

Forum guidelines
Nalini Raghavan

Nalini Raghavan

  • 1.8k
  • 25
  • 925

Getting an error when trying to retrieve data from the datab

Oct 11 2017 2:38 PM
Hello,
 
This is the error i get when I try to retrieve data from the database.
 
"The cast to value type 'Single' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type."
 
Here is my code:
  1. public JsonResult AjaxInventory()  
  2. {  
  3. //Get the id of active election  
  4. FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value);  
  5. //0 Username | 1 Fullname | 2 User Id | 3 Login Type | 4 Election Id  
  6. string[] UserData = ticket.UserData.Split('|');  
  7. // action inside a standard controller  
  8. //int filteredResultsCount;  
  9. //int totalResultsCount;  
  10. //Load information  
  11. var result = dbGetItemsList(UserData[4], UserData[2]);  
  12. //totalResultsCount = result.Count;  
  13. //filteredResultsCount = totalResultsCount;  
  14. return Json(new  
  15. {  
  16. // this is what datatables wants sending back  
  17. //draw = 1,  
  18. //recordsTotal = totalResultsCount,  
  19. //recordsFiltered = filteredResultsCount,  
  20. data = result  
  21. //, JsonRequestBehavior.AllowGet  
  22. );  
  23. }  
  24.   
  25. private List dbGetItemsList(string electionID, string candidateId)  
  26. {  
  27. //SqlParameter param1 = new SqlParameter("@candidateId", candidateID);  
  28. //ElectionID = @electionId and  
  29. List lstItems = dbFFS.Database.SqlQuery  
  30. ("sp_GetContributions @electionId, @candidateId, @officeId, @contributionId,@contributorId, @receiptNoFrom, @receiptNoTo, @amountFrom, @amountTo, @acceptionTimeFrom, @acceptionTimeTo, @contributorTypeAbbr, @relationshipAbbr, @contributionTypeAbbr, @deliveryMethodAbbr",  
  31. new SqlParameter("@electionId", electionID),  
  32. new SqlParameter("@candidateId", candidateId),  
  33. new SqlParameter("@officeId", 7),  
  34. new SqlParameter("@contributionId", 1),  
  35. new SqlParameter("@contributorId", 26),  
  36. new SqlParameter("@receiptNoFrom", DBNull.Value),  
  37. new SqlParameter("@receiptNoTo", DBNull.Value),  
  38. new SqlParameter("@amountFrom", DBNull.Value),  
  39. new SqlParameter("@amountTo", DBNull.Value),  
  40. new SqlParameter("@acceptionTimeFrom", DBNull.Value),  
  41. new SqlParameter("@acceptionTimeTo", DBNull.Value),  
  42. new SqlParameter("@contributorTypeAbbr", DBNull.Value),  
  43. new SqlParameter("@relationshipAbbr", DBNull.Value),  
  44. new SqlParameter("@contributionTypeAbbr", DBNull.Value),  
  45. new SqlParameter("@deliveryMethodAbbr", DBNull.Value)  
  46. ).ToList();  
  47. //Call Stored Procedure  
  48. //List lstItems = db.Database.SqlQuery(  
  49. // "GetWebInventoryList @electionId, @candidateId",  
  50. // new SqlParameter("electionId", electionID),  
  51. // new SqlParameter("candidateId", candidateID)  
  52. // ).ToList();  
  53. return lstItems;  
  54. }

Answers (3)