Ok, I saw a few similar questions regarding this. But I was unable to cobble together an answer to solve my particular problem.
Basically, I'm attempting to get data from SQL Server to print it onto a view (simple, right?). The problem is: I have a number of other things happening on the view. So, I have to use one model (I think).
Here is what I have (I am very (very very very) new to ASP.NET MVC):
Model
Here are the pref I'm attempting to write to (the model/class is Registrar)
//display props
public string QUID_Disp { get; set; }
public string QU_User_Disp { get; set; }
public string Admin_User_Disp { get; set; }
public string Reg_User_Disp { get; set; }
public List<Registrar> Events { get; set; }
Here is my SQL call in the controller
if (sqlStuff.Admin == "Yes")
{
Registrar users = new Registrar();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["internet"].ConnectionString))
{
con.Open();
using (SqlCommand da = new SqlCommand("select * from Registrar_Tool_Users", con))
using (SqlDataReader reader = da.ExecuteReader())
{
while (reader.Read())
{
users.Events.Add (new Registrar { QU_User_Disp = reader["QU_User"].ToString(), QUID_Disp = reader["QUID"].ToString(), Admin_User_Disp = reader["Reg_Admin"].ToString() });
}
return View(users.Events);
}
}
And view:
@model QUTools.Models.Registrar
//Things that are working here......
@{
foreach (var item in Model.Events)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.QU_User_Disp)
</td>
<td>
@Html.DisplayFor(modelItem => item.QUID_Disp)
</td>
<td>
@Html.DisplayFor(modelItem => item.Admin_User_Disp)
</td>
</tr>
}
}
The error occurs on the line
users.Events.Add (new Registrar...
and says:
An exception of type 'System.NullReferenceException' occurred in QUT.dll but was not handled in user code