tags:

views:

1123

answers:

6

Hi experts,

How do we assign null value to int column in LINQ. eg.

 SQLDBDataContext sqlD = new SQLDBDataContext();
 var result = from p in sqlD.loadsRadius(Convert.ToInt32(Request["radius"]), .....

here if Request["radius"] is null gives an error.

I could pass 0 via this but I need to change in many already existing procedures.

Thanks in advance.
Anil

A: 

If the exception is a FormatException, my guess is that the value returned by Request["radius"] is actually the string "null". If Request["radius"] actually returned null, Convert.ToInt32() would return zero. You may try using using int.TryParse() to avoid the exception.

CodeChef
A: 

as suggested by Codechef..

int radius = -1;
int.TryParse(Request["radius"],out radius)
if(radius > 0) // you can remove this clause if you don't want this
{
SQLDBDataContext sqlD = new SQLDBDataContext();
 var result = from p in sqlD.loadsRadius(radius)
}
Pradeep Kumar Mishra
A: 

Look into nullable types: Here.

TGnat
A: 

You could use ?? operator like this:

Convert.ToInt32(Request["radius"] ?? "0")

If Request["radius"] equals to null, it will return "0" instead.

Alexander Prokofyev
+1  A: 

You need to case the int to a null, eg:

var result = from p in sqlD.loadsRadius(string.IsNullOrEmpty(Request["radius"]) ? (int?)null : int.Parse(Request["radius"]);
Slace
A: 

thanks for your answers. It worked Slace.

ANIL MANE