Getting an error while trying to retrieve items from the database

Last Reply 5 days ago By dharmendr

Posted 12 days 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.

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)

                    ).ToList();

            return lstItems;
        }

 

You are viewing reply posted by: naliniragh 11 days ago.
Posted 11 days ago

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. 

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html