Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
its give error for "The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type."

Here, i have Null table 'Rate'
and i wont to get max no. of rate.id (primary and Auto Increment).
here problem is that if table is Empty then it's show Error
how can i handle it

C#
DataClasses1DataContext dm = new DataClasses1DataContext();
var item = dm.Rates.Max(o => o.Id);
 item = (item) + 1;
    
lblSrno.Containt=item.Tostring();


please help me
Posted
Updated 14-Nov-14 6:02am
v3
Comments
BillWoodruff 14-Nov-14 2:23am    
It's good programming practice to check for 'null before executing code that will rely on a non-null "something."

Try this...

C#
int? intIdt = db.Users.Max(u => (int?)u.UserId);


See this link for Explanation.
Null MaxId[^]
 
Share this answer
 
Hi,

You can write like this

C#
try
{
DataClasses1DataContext dm = new DataClasses1DataContext();
var item = dm.Rates.Max(o => o.Id);
if(item==null)return;
 item ++;
lblSrno.Containt=item.Tostring();
}
catch{

}


Check it whether it is working for you..
 
Share this answer
 
Comments
mukesh mourya 14-Nov-14 12:22pm    
It's not work
Jineesh TR 16-Nov-14 22:28pm    
then you check null for dm.Rates. I think this is null.
C#
DataClasses1DataContext dm = new DataClasses1DataContext();
var item = dm.Rates.Max(o => o.Id);
 item = (item==null ? 0:item) + 1;
lblSrno.Containt=item.Tostring();
 
Share this answer
 
Comments
mukesh mourya 14-Nov-14 12:37pm    
Not worked
You can try the below code.

var item = dm.Rates.Max(o => {if(o!=null && o.id!=null){ return o.id; } });
 
Share this answer
 
Comments
mukesh mourya 14-Nov-14 12:22pm    
It's not work :(

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