Error: 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 in ASP.Net

Last Reply 3 months ago By naliniragh

Posted 3 months ago

I am trying to retrieve items from the database and list them on my front end. I am getting the following error:

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.

Actually there was a variable in my model that was using the wrong datatype (I used string) and the database has it as bigint. Now im able to retrieve information from the database but the receipt no shows as null. I tried usingInt64 and Int32 but with them I cannot retrieve anything.

Please help. 

        public JsonResult AjaxInventory()
            //Get the id of active election
            FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value);
            //0 Username | 1 Fullname | 2 User Id | 3 Login Type | 4 Election Id
            string[] UserData = ticket.UserData.Split('|');

            // action inside a standard controller
            //int filteredResultsCount;
            //int totalResultsCount;
            //Load information
            var result = dbGetItemsList(UserData[4], UserData[2]);
            //totalResultsCount = result.Count;
            //filteredResultsCount = totalResultsCount;

            return Json(new
                // this is what datatables wants sending back
                //draw = 1,
                //recordsTotal = totalResultsCount,
                //recordsFiltered = filteredResultsCount,
                data = result
            } //, JsonRequestBehavior.AllowGet  

        private List<Models.ffsContributions> dbGetItemsList(string electionID, string candidateId)
            List<Models.ffsContributions> lstItems = dbFFS.Database.SqlQuery<Models.ffsContributions>
                                                    ("sp_GetContributions @electionId, @candidateId, @officeId, @contributionId,@contributorId, @receiptNoFrom, @receiptNoTo, @amountFrom, @amountTo, @acceptionTimeFrom, @acceptionTimeTo, @contributorTypeAbbr, @relationshipAbbr, @contributionTypeAbbr, @deliveryMethodAbbr",
                                                    new SqlParameter("@electionId", electionID),
                                                    new SqlParameter("@candidateId", candidateId),
                                                    new SqlParameter("@officeId", 7),
                                                    new SqlParameter("@contributionId", 1),
                                                    new SqlParameter("@contributorId", 26),
                                                    new SqlParameter("@receiptNoFrom", DBNull.Value),
                                                    new SqlParameter("@receiptNoTo", DBNull.Value),
                                                    new SqlParameter("@amountFrom", DBNull.Value),
                                                    new SqlParameter("@amountTo", DBNull.Value),
                                                    new SqlParameter("@acceptionTimeFrom", DBNull.Value),
                                                    new SqlParameter("@acceptionTimeTo", DBNull.Value),
                                                    new SqlParameter("@contributorTypeAbbr", DBNull.Value),
                                                    new SqlParameter("@relationshipAbbr", DBNull.Value),
                                                    new SqlParameter("@contributionTypeAbbr", DBNull.Value),
                                                    new SqlParameter("@deliveryMethodAbbr", DBNull.Value)


            return lstItems;
Posted 3 months ago Modified on 3 months ago

If you have changed the DataType and assigned it’s as Nullable.

 public Int64? columName { set; get; }

Then it will handle the Nullable value error which was showing before.

Problem might be in Storedprocedure which returning the value or column which you have assigning to the property.


Posted 3 months ago Modified on 3 months ago

you are using ! (NOT) oprator which is conditional operator if receiptNo is Long then you canot check like this.

if you want to set some default value to ReceiptNo if its null then check the below code.

SqlParameter param15 = new SqlParameter("@columname", model.ReceiptNo ?? null);
// or
SqlParameter param16 = new SqlParameter("@columname", model.ReceiptNo ?? 1);


Posted 3 months ago

The way I solved it in the end is using hasValue function as this variable type is long.

SqlParameter param6 = new SqlParameter("@receiptNo", model.ReceiptNo.HasValue ? model.ReceiptNo:0);


I agree, here is the link: