[英]C# InvalidCastException when returning single value from SQL database
I am trying to return a value from a SQL database. 我正在尝试从SQL数据库返回一个值。 However everytime I execute the following method I get an error stating, "InvalidCastException was unhandled. Object cannot be cast from DBNull to other types."
但是,每次执行以下方法时,都会出现错误,指出“未处理InvalidCastException。无法将对象从DBNull强制转换为其他类型。”
Any light you could shed on this would be greatly appreciated. 您可能会对此有所了解,将不胜感激。 The method I am using is as follows.
我使用的方法如下。
Thanks. 谢谢。
public static int ScrapTotal2(string prdTypeV, string startDateV, string prtCodeV)
{
int scrapTotal2;
SqlParameter prdType = new SqlParameter("@prdType", SqlDbType.VarChar);
prdType.Value = prdTypeV;
SqlParameter startDate = new SqlParameter("@startDate", SqlDbType.VarChar);
startDate.Value = startDateV;
SqlParameter prtCode = new SqlParameter("@prtCode", SqlDbType.VarChar);
prtCode.Value = prtCodeV;
SqlCommand scrapTotal2SC = new SqlCommand("SELECT SUM([QTY_SCRP]) FROM [TBL_PRDMST] WHERE [PRD_CODE] LIKE @prdType AND [PRD_DATE] = @startDate AND [PRT_CODE] LIKE @prtCode", DataAccess.myConnection);
scrapTotal2SC.Parameters.Add(prdType);
scrapTotal2SC.Parameters.Add(startDate);
scrapTotal2SC.Parameters.Add(prtCode);
DataAccess.myConnection.Open();
scrapTotal2 = Convert.ToInt32(scrapTotal2SC.ExecuteScalar());
DataAccess.myConnection.Close();
return scrapTotal2;
}
Your SELECT statement returns NULL value. 您的SELECT语句返回NULL值。 You have to check if it is null first:
您必须先检查它是否为空:
object result = scrapTotal2SC.ExecuteScalar();
if (result == DBNull.Value)
{
/* write your code */
}
else
{
scrapTotal2 = Convert.ToInt32(result);
}
Since you didn't specify, I'm going to assume that this line is the problem: 由于您未指定,因此我假设这是问题所在:
scrapTotal2 = Convert.ToInt32(scrapTotal2SC.ExecuteScalar());
When you get a NULL in a database, it will be cast to a type of DBNull in your code. 当您在数据库中获得NULL时,它将在代码中转换为DBNull类型。 This type cannot be cast to an
Int32
. 此类型不能强制转换为
Int32
。
You can check for it like this: 您可以像这样检查它:
var obj = scrapTotal2SC.ExecuteScalar()
if (obj == DBNull.Value)
{
// whatever logic you want to handle nulls
} else {
scrapTotal2 = Convert.ToInt32(obj);
}
Seems like the error message is clear - your query returns null
(which in .NET would be a DbNull
). 似乎错误消息很清楚-您的查询返回
null
(在.NET中为DbNull
)。
This can't be converted to an Int32
, as integers are value types and cannot be null
. 这不能转换为
Int32
,因为整数是值类型,不能为null
。
Either check the return value for DbNull
and return a default value from your data layer, or check your query and database to ensure that a null
will not be returned. 检查
DbNull
的返回值并从数据层返回默认值,或者检查查询和数据库以确保不返回null
。
Some SQL server think that sum of empty list is null and not 0. So you have to handle the conversion from null to int yourself. 一些SQL Server认为空列表的总和为null而不是0。因此,您必须自己处理从null到int的转换。 Not convert directly the result of the query to int.
不直接将查询结果转换为int。
尝试scrapTotal2 = Convert.ToInt32(((string)scrapTotal2SC.ExecuteScalar()));
The error message is because the return value is NULL
and that you are trying to store in a INT
type variable ... which can't be done. 该错误消息是因为返回值为
NULL
,并且您试图将其存储在INT
类型变量中……这是无法完成的。
Probably what you can do is ... 可能您可以做的是...
if(retval is null) then assign var = 0.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.