我使用的是SQL Server数据库,我有一些错误代码:
offerDayCount = (int)reader["day"];
我收到以下错误:
数据为空。无法在Null值上调用此方法或属性
我尝试了以下操作:
offerDayCount = reader.GetInt32(reader.GetOrdinal("day") as int? ?? 0);
和:
if (reader[“day”] != null)
offerDayCount = (int)reader["day"];
还有更多代码行:
using (SqlCommand com = new SqlCommand("select ID,month,day,startdate,endDate,offer from [enddate] where ID=@ID", con))
{
com.Parameters.AddWithValue("@ID", ID.Text);
using (SqlDataReader reader = com.ExecuteReader())
{
if (reader.Read())
{
//get information from enddate table
var offer = reader["offer"].ToString();
offerDayCount = (int)reader["day"];
startDate = (DateTime)reader["startDate"];
if (reader["endDate"] != null)
endDate = (DateTime)reader["endDate"];
if (reader["month"] == null && offer != null)
{
endDate = DateTime.Now.Date;
}
con.Close();
con.Open();
//count the visit from checkin table
using (var com2 = new SqlCommand("SELECT COUNT(*) as count From checkin WHERE time >= @STARTDATE and (time <= @ENDDATE)",con))
{
com2.Parameters.AddWithValue("@STARTDATE", startDate);
com2.Parameters.AddWithValue("@ENDDATE", endDate);
using (SqlDataReader reader2 = com2.ExecuteReader())
{
if (reader2.Read())
{
visitedCount = (int)reader2["count"];
if (offer != null && visitedCount < offerDayCount)
goodForVisit = true;
if (offer != null && DateTime.Now >= startDate && DateTime.Now <= endDate)
goodForVisit = true;
}
}
}
}
}
}
您能帮我解决我的问题吗?