I'm trying to figure out what's causing a Cannot perform runtime binding on a null reference
error from this code:
var query = "SELECT Id, UserName, List_Order, LoggedIn " +
"FROM AspNetUsers" +
"WHERE LoggedIn = 1" +
"ORDER BY List_Order ASC";
var conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
var cmd = new SqlCommand(query, conn);
conn.Open();
var rdr = cmd.ExecuteReader();
var n = 0;
while(rdr.Read())
{
if (Convert.ToString(rdr["UserName"]) != null)
{
ViewBag.speakers[n] = new string[4] {
Convert.ToString(rdr["Id"]),
Convert.ToString(rdr["UserName"]),
Convert.ToString(rdr["List_Order"]),
Convert.ToString(rdr["LoggedIn"])
};
//Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot
//perform runtime binding on a null reference
n++;
}
}
The n++
increment seems to be the cause of this error and I don't understand why.
Updated code to reflect possible solutions. However, the error still remains.
Tried this with the same result:
if (!string.IsNullOrWhiteSpace(Convert.ToString(rdr["UserName"]))) {
List<string> speakers = new List<string>();
speakers.Add(Convert.ToString(rdr["Id"]));
speakers.Add(Convert.ToString(rdr["UserName"]));
speakers.Add(Convert.ToString(rdr["List_Order"]));
speakers.Add(Convert.ToString(rdr["LoggedIn"]));
ViewBag.speakers[n] = speakers;
n++;
}
ASP.NET MVC 5 is a web framework based on Model-View-Controller (MVC) architecture. Developers can build dynamic web applications using ASP.NET MVC framework that enables a clean separation of concerns, fast development, and TDD friendly.
The main difference between ASP.NET Core and ASP.NET MVC 5 is their cross-platform approach. ASP.NET Core can be used on Windows, Mac, or Linux, whereas ASP.NET MVC 5 can only be used for applications on Windows. The ASP.NET Core MVC is a framework for building web apps and APIs, optimized for use with ASP.NET Core.
They are the same thing. C# is the language you have used to do your development, but ASP.NET MVC is the framework you used to do it.
There are two issues in your code:
Consider this:
public ActionResult Index()
{
int n = 0;
ViewBag.speakers[n] = 5;
return View();
}
This simplified piece of code throws Cannot perform runtime binding on a null reference
since speakers is not defined (null reference).
You can fix it by defining speakers
in the dynamic ViewBag before the loop:
ViewBag.speakers = new List<string>();
The second issue:
ViewBag.speakers[n] = speakers;
speakers in your code is a List, you might want to define ViewBag.speakers
as a List<List<string>>
and call .Add(speakers)
instead of accessing using an index (you might get index was out of range)
Calling the .ToString()
method on a null column value can result in the Cannot perform runtime binding on a null reference
. Use Convert.ToString()
instead; it will return an empty string if you attempt to convert a null
value and won't require additional code for null
checking.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With