我坚持使用此代码。请帮我解决这个问题。
我想要做的是:我有下载列表,其中包含天数(1,2,3等),我想要做的是我必须将这些天映射到我的数据库,并将其显示给网格视图。
但问题是我认为我的映射存在问题,当我选择第1天时,收到错误消息:Data is Null. This method or property cannot be called on Null values
。
如果我选择除1之外的任何一天,它会在gridview中显示值;但是我想要一个与dropdownlist索引匹配的单个值与表索引匹配。
这是我的代码:
protected void btnSubmit_Click(object sender, EventArgs e)
{
Label1.Visible = true;
Label2.Visible = true;
double dbstan_minmoist = 0.0;
double dbstan_mintemp = 0.0;
double dbstan_sunshine = 0.0;
double dbstan_capacity = 0.0;
double dbcurr_minmoist = 0.0;
double dbcurr_mintemp = 0.00;
double dbcurr_capacity = 0.0;
double dbcurr_sunshine = 0.0;
if (ddl.SelectedIndex >0)
{
int id = Convert.ToInt32(ddl.SelectedIndex);
int c = 1;
SqlConnection con = new SqlConnection(@"Data Source");
con.Open();
var strquery = "select Min_Moisture,Min_Temperature,Sunshine,Field_Capacity from tbl_Standard_values";
SqlCommand cmd = new SqlCommand(strquery, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (c== id)
{
dbstan_mintemp = dr.GetDouble(dr.GetOrdinal("Min_Temperature"));
dbstan_minmoist = dr.GetDouble(dr.GetOrdinal("Min_Moisture"));
dbstan_sunshine = dr.GetDouble(dr.GetOrdinal("Sunshine"));
dbstan_capacity = dr.GetDouble(dr.GetOrdinal("Field_Capacity"));
break;
}
else
{
c=c+1;
}
}
SqlConnection con1 = new SqlConnection(@"Data Source=");
con1.Open();
var strquery1 = "select distinct Min_Moisture,Mim_Temperature,Sunshine,Field_Capacity from tbl_Cornels_Development";
SqlCommand cmd1 = new SqlCommand(strquery1, con1);
int d = 1;
SqlDataReader dr1 = cmd1.ExecuteReader();
while (dr1.Read())
{
if (d == id)
{
dbcurr_mintemp = dr1.GetDouble(dr1.GetOrdinal("Mim_Temperature"));
dbcurr_minmoist = dr1.GetDouble(dr1.GetOrdinal("Min_Moisture"));
dbcurr_sunshine = dr1.GetDouble(dr1.GetOrdinal("Sunshine"));
dbcurr_capacity = dr1.GetDouble(dr1.GetOrdinal("Field_Capacity"));
break;
}
else
{
d=d+1;
}
}
//first case
if ((dbcurr_mintemp < dbstan_mintemp) && (dbcurr_minmoist < dbstan_minmoist) && (dbcurr_capacity < dbstan_capacity) && (dbcurr_sunshine < dbstan_sunshine))
{
ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('LightIrrigation');", true);
}
//second case
else if ((dbcurr_mintemp < dbstan_mintemp) && (dbcurr_minmoist < dbstan_minmoist) && (dbcurr_capacity < dbstan_capacity) && (dbcurr_sunshine > dbstan_sunshine))
{
ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('LightIrrigation');", true);
}
GridView1.DataSource = dr;
GridView1.DataBind();
GridView2.DataSource = dr1;
GridView2.DataBind();
con.Close();
}
else
{
ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Please select a day');", true);
}
}
}
答案 0 :(得分:0)
我发现很难按照问题和代码进行操作,但经过一些阅读后,我认为您的问题可能就是这些问题:
var strquery1 = "select distinct Min_Moisture,Mim_Temperature,Sunshine,Field_Capacity from tbl_Cornels_Development";
...
dbcurr_mintemp = dr1.GetDouble(dr1.GetOrdinal("Mim_Temperature"));
应为:
var strquery1 = "select distinct Min_Moisture,Min_Temperature,Sunshine,Field_Capacity from tbl_Cornels_Development";
...
dbcurr_mintemp = dr1.GetDouble(dr1.GetOrdinal("Min_Temperature"));
也就是说,你有Mim而不是Min。