我是C#MVC ASP.NET的新手。我无法弄清楚为什么我会得到这个例外。有人可以帮我这个吗?
尝试登录后出现异常:
类型' System.NullReferenceException'的例外情况发生在App_Web_stjzfqjl.dll中但未在用户代码中处理
控制器代码:
public class LoginController : DatabaseController
{
// GET: Login
public ActionResult Login(string email, string wachtwoord)
{
int verificatie = 0;
try
{
Session.RemoveAll();
connection.Open();
string selectQuery = "SELECT wachtwoord, is_beheerder, is_manager, gebruiker_id, voornaam, achternaam from gebruiker where email = @emailadres";
MySqlCommand cmd = new MySqlCommand(selectQuery, connection);
MySqlParameter emailParam = new MySqlParameter("@emailadres", MySqlDbType.VarChar);
cmd.Parameters.Add(emailParam);
emailParam.Value = email;
MySqlDataReader dataReader = cmd.ExecuteReader();
while (dataReader.Read())
{
var gebruiker = new Gebruiker
{ voornaam = dataReader.GetString("voornaam"),
achternaam = dataReader.GetString("achternaam"),
is_beheerder = dataReader.GetInt32("is_beheerder"),
wachtwoord = dataReader.GetString("wachtwoord"),
is_manager = dataReader.GetInt32("is_manager")
};
if (gebruiker.wachtwoord == wachtwoord)
{
verificatie = 1;
}
if(gebruiker.is_beheerder == 1)
{
Session["Beheerder"] = gebruiker;
}
else
{
Session["Gebruiker"] = gebruiker;
}
}
}
catch (Exception e)
{
ViewBag.error = email + e;
return View();
}
finally
{
connection.Close();
}
if (verificatie == 1)
{
ViewBag.error = "Verificatie is GOED";
Response.Redirect("~/Home/Index");
}
else
{
ViewBag.error= email + " " + wachtwoord;
}
return View(gebruiker);
}
在布局视图中:
@model DeGeenery.Models.Gebruiker
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - The Greenery</title>
<link rel="shortcut icon" href="~/Content/images/favicon.png">
<link href="~/Content/Sitev2.css" rel="stylesheet" type="text/css" />
<script src="~/Scripts/modernizr-2.6.2.js"></script>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
</head>
<body>
<div class="wrapper">
<div class="header">
<div id="logo"><a href="index.html"><img src="~/Content/logo.png" /> </a></div>
<div id="basket"><a href=@Url.Action("Index", "Winkelwagen")><img src="~/Content/basket.png" /></a></div>
</div>
<div class="navbar">
<ul>
<li><a href=@Url.Action("Index", "Home")>Home</a></li>
<li><a href=@Url.Action("Index", "Webshop")>Webshop</a></li>
<li><a href=@Url.Action("Index", "OverOns")>Over ons</a></li>
<li><a href=@Url.Action("Index", "Contact")>Contact</a></li>
@if (Session["Gebruiker"] == null && Session["Beheerder"] == null)
{
<li style="float:right;"><a href=@Url.Action("Login","Login")>Login</a></li>
}
@if (Session["Beheerder"] != null)
{
<li style="float:right;"><a href=@Url.Action("Loguit", "Login")>Log uit</a></li>
<li style="float:right;"><a href=@Url.Action("Index", "Beheer")>Beheer</a></li>
<li style="float:right; color: white; margin-right: 10px;"><h4><a href="">Welkom, @Model.voornaam </a></h4></li>
}
else if (Session["Gebruiker"] != null)
{
<li style="float:right;"><a href=@Url.Action("Loguit","Login")>Log uit</a></li>
<li style="float:right;">Welkom, @ViewBag.voornaam ViewBag.achternaam</li>
}
</ul>
</div>
<div class="body">
@RenderBody()
</div>
<div class="footer">
Copyright De Greenery © 2016
</div>
</div>