摘要:我正在构建一个应用程序功能来报告丢失的数据。缺少记录的不同名称填充我的下拉列表。但是,当我开始按该特定名称(或任何名称)搜索完整报告时,出现此错误以及应用程序崩溃:
错误:用户代码未处理 NullReferenceException。
你调用的对象是空的。
我的视图(错误所在的位置):
看法:
@using (Html.BeginForm("Index", "Home", "POST"))
{
<div class="searchField">
<input type="text" class="search-query" name="heatSearch" placeholder="Search">
<button class="btn btn-success" type="submit">Search</button>
</div>
<br />
<select>
@foreach (var item in (ViewData["MissingChem"] as IEnumerable<string>))
{
<option> @item </option>
}
</select>
}
控制器:
public ActionResult Index()
{
Session["InitialLoad"] = "Yes";
HomeModel H = new HomeModel();
ViewData["MissingChem"] = H.MissingQueryResults();
return View();
}
模型:
public List<string> MissingQueryResults()
{
//HomeModel Tolerances = new HomeModel();
List<String> nameList = new List<String>();
SqlCommand missingQuery = new SqlCommand("SELECT heatname FROM dbo.chemistrytable WHERE heatname NOT IN (SELECT heatname FROM dbo.chemistrytable WHERE sampletype = 'AVE') AND analysistime between '01-01-2013' and Current_Timestamp AND heatname LIKE '[a,b,c,d]%' Order by heatname");// + heatName + "'");
SqlCommand mainquery = new SqlCommand("SELECT analysisv
alue.analysisid, heatname, analysistime, sampletype, grade, productid, element, value FROM dbo.AnalysisValue INNER JOIN dbo.ChemistryAnalysis ON dbo.AnalysisValue.AnalysisID = dbo.ChemistryAnalysis.AnalysisID Where heatname = '" + "' Order By analysisvalue.analysisid Asc, element");
using (SqlConnection conn = new SqlConnection(strSQLconnection))
{
missingQuery.CommandTimeout = 20000;//Dateadd(day, -1, Current_Timestamp)
conn.Open();
missingQuery.Connection = new SqlConnection(strSQLconnection);
missingQuery.Connection.Open();
using (var reader = missingQuery.ExecuteReader())
{
int fieldCount = reader.FieldCount;
while (reader.Read())
{
for (int i = 0; i < fieldCount; i++)
{
nameList.Add(reader[i].ToString().Trim());
}
}
}return nameList;
}
}
任何和所有的帮助将不胜感激!