Click here to Skip to main content
14,872,121 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all...,
Plz help me,
i am passing null value in int datatype then error is occured following is my code:
C#
int FID = convert.Toint32(linkbutton.CommandArgument);


some records FID have values but some records FID have NULL. when it have null value the error occured "Input string was not in correct format" i want to handle this error.


Thanks in advance.....
Posted
Updated 13-May-12 23:21pm
v2

Here is the try catch version of it with Pokemon Exception handing

C#
try
{
 int FID = convert.Toint32(linkbutton.CommandArgument);
 //here the FID is a valid value so go ahead and do soemthing
}
catch(Exception ex)
{
  //darn it, invalid value found. lets think about handling it or ignoring it
}


also If you prefer not to handle an exception if the conversion fails, you can call the Int32.TryParse method instead. It returns a Boolean value that indicates whether the conversion succeeded or failed.
   
v2
Comments
dA.d 14-May-12 5:45am
   
thanks again......
i want to pass FID value in delete stored procedure for that how can get FID in procedure.
i use like
DeleteStoreProcedureName(FID);
but compilation error is FID not exist.
Rahul Rajat Singh 14-May-12 5:48am
   
Perhpas you are calling it outsice the try block and after catch block. i recommend moving it inside the try block or change the try block to this one


int FID = 0;
try
{
FID = convert.Toint32(linkbutton.CommandArgument);
//here the FID is a valid value so go ahead and do soemthing
}
dA.d 14-May-12 5:57am
   
thanks for helping me...
Rahul Rajat Singh 14-May-12 6:02am
   
You are most welcome
VJ Reddy 15-May-12 4:35am
   
My 5!
Hi,
Try this:

C#
if(linkbutton.CommandArgument != null){
    int FID = convert.Toint32(linkbutton.CommandArgument);
}
else{
   int FID = 0; //Compare your data with 0 if it is required.
}


All the best.
   
Hi,

Try like this:

C#
if ((txtValue.Text).Trim() == "")
{
     cmd.Parameters("@FID").Value = DBNull.Value
}
else
{
    Pass the actual value;

}
   
v2
Comments
dA.d 14-May-12 5:48am
   
Thanks for replay......
but i want to handle error using try and catch blocks,
and that FID value i want to pass in delete stored procedure as parameter value.
Or simply :


C#
int FID = 0;
try
{
 FID = convert.Toint32(linkbutton.CommandArgument);
 //here the FID is a valid value so go ahead and do soemthing
}
catch(Exception ex)
{
  FID = 0;
}
   

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