c#.net中的错误处理 [英] error handling in c#.net
问题描述
大家好...,
请帮我,
我在int数据类型中传递了null值,然后发生以下错误,这是我的代码:
hi all...,
Plz help me,
i am passing null value in int datatype then error is occured following is my code:
int FID = convert.Toint32(linkbutton.CommandArgument);
一些记录FID具有值,但是一些记录FID具有NULL.当它具有null值时,发生错误输入字符串的格式不正确",我想处理此错误.
在此先感谢.....
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.....
推荐答案
这里是Pokemon Exception Handing的试用版
Here is the try catch version of it with Pokemon Exception handing
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
}
另外,如果您不想在转换失败的情况下不处理异常,则可以改为调用Int32.TryParse
方法.它返回一个布尔值,该值指示转换是成功还是失败.
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.
试试这个:
Hi,
Try this:
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:
if ((txtValue.Text).Trim() == "")
{
cmd.Parameters("@FID").Value = DBNull.Value
}
else
{
Pass the actual value;
}
这篇关于c#.net中的错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!