当我在我的数据库上执行此命令时,有时会收到 result=null。我想在发生这种情况时输入“其他”部分,但我收到
mscorlib.dll 中发生了“System.FormatException”类型的未处理异常。附加信息:输入字符串的格式不正确。
DB database = new DB();
int reservedSeats = 0;
using (database.sqlConnection)
{
database.sqlConnection.Open();
string command = "select SUM(nrLocuri) as result from Rezervari where idRand = @idRand and idShow=@idShow";
using (SqlCommand cmd = new SqlCommand(command, database.sqlConnection))
{
cmd.Parameters.Add("@idRand", SqlDbType.Int).Value = idRand;
cmd.Parameters.Add("@idShow", SqlDbType.Int).Value = idShow;
using (SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.Read())
if (dr["result"].ToString() != null)
reservedSeats = Convert.ToInt32(dr["result"].ToString());
else
return totalSeats;
}
}
}
return totalSeats-reservedSeats;