Click here to Skip to main content
16,004,761 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
dl_books.DataSource = (from st in obj.Book_details
                                      join d in obj.Rating_tables on st.Book_Id equals d.Book_id into pl
                                      where st.Category == "Health "
                                      orderby st.Book_Id descending


                                      select new { st.Book_Id , rat = pl.Average(c=>Convert.ToDecimal(c.Rating))  ,st.Price ,st.Name ,st.Image, }).Take(6);
               dl_books.DataBind();

this is my code and below is my error may be it is just bcz 4 some books i dnt having rates so then database is producing null and dts d problem any idea how to resolve this 

HTML
The null value cannot be assigned to a member with type System.Decimal which is a non-nullable value type.
Posted
Comments
JoCodes 19-Nov-13 7:02am    
Try a null coalesce operator like Convert.ToDecimal(c.Rating??0)
Omprakash Kukana 20-Nov-13 2:23am    
The null value cannot be assigned to a member with type System.Decimal which is a non-nullable value type.
this error is again m getting
JoCodes 20-Nov-13 2:32am    
Rating is non nullable int type , right?
Omprakash Kukana 20-Nov-13 2:43am    
yes
JoCodes 20-Nov-13 2:42am    
Can you try (c => (Decimal?) c.Rating) instead of c=>Convert.ToDecimal(c.Rating) .. Check and let me know

1 solution

Hi try this one
C#
dl_books.DataSource = (from st in obj.Book_details
                                      join d in obj.Rating_tables on st.Book_Id equals d.Book_id into pl
                                      where st.Category == "Health "
                                      orderby st.Book_Id descending
 

                                      select new { st.Book_Id , rat = pl.Average(c=>(decimal)c.Rating)  ,st.Price ,st.Name ,st.Image, }).Take(6);
               dl_books.DataBind();
 
Share this answer
 
Comments
Omprakash Kukana 2-Dec-13 23:07pm    
(decimal)c.Rating
cannot convert type string to decimal

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900