Note that there are some explanatory texts on larger screens.

plurals
  1. PONullReferenceException in embeded if clause
    primarykey
    data
    text
    <p>I am currently trying to program a forgot password page where the user enter their username, their email address and a message which will be sent to the administrator of the site. Want I want is the following: After the user clicks on the button the username and the email address should be checked if they are associated. (Those values are saved in my database) I managed to programm everything but I have a problem which I cannot solve. Everytime the Razor engine renders the page I'll get a NullReferenceException was unhandled by user code. I know why this is happening but as I said I cannot fix this.</p> <p>Here is the code:</p> <pre><code>@model MvcApplication1.ViewModels.ContactAdminViewModel @using MvcApplication1.Queries @{ Layout = null; } &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;script src="@Url.Content("~/Scripts/jquery-1.7.1.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" /&gt; &lt;title&gt;SendMail&lt;/title&gt; &lt;/head&gt; &lt;body&gt; @using (Html.BeginForm("SendMail", "ContactAdmin", FormMethod.Post)) { @Html.ValidationSummary(true) &lt;div&gt; &lt;p&gt; @Html.LabelFor(m =&gt; m.username, "username") @Html.EditorFor(m =&gt; m.username) &lt;p&gt; @Html.ValidationMessageFor(m =&gt; m.username) &lt;/p&gt; &lt;/p&gt; &lt;p&gt; @Html.LabelFor(m =&gt; m.email, "email") @Html.EditorFor(m =&gt; m.email) &lt;p&gt; @Html.ValidationMessageFor(m =&gt; m.email) &lt;/p&gt; &lt;/p&gt; &lt;p&gt; @Html.LabelFor(m =&gt; m.message, "Your message") &lt;p&gt; @Html.TextAreaFor(m =&gt; m.message, new { cols = "35", rows = "10", @style = "resize:none" }) &lt;p&gt; @Html.ValidationMessageFor(m =&gt; m.message) &lt;/p&gt; &lt;/p&gt; &lt;/p&gt; &lt;p&gt; &lt;input id="send-mail" type="submit" class="button" value="Send" /&gt; &lt;/p&gt; &lt;/div&gt; &lt;script type="text/javascript"&gt; $(document).ready(function () { jQuery('#send-mail').click(function () { @if (@DQL.CheckUsernameAndEmail(Model.username, Model.email)) { &lt;text&gt; alert("Your Message will be sent"); &lt;/text&gt; } else { &lt;text&gt; alert("Your username is not associated with the email adress"); &lt;/text&gt; } }); }); &lt;/script&gt; } &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Any tips on how to solve that problem are highly appreciated :)</p> <h3>EDIT</h3> <p>The DQL.cs is a C# Class where I wrote down all my queries. It's actually the model that is null. I forgot to write that :/ I'm really sorry. Nevertheless here is the code from the DQL.cs which checks if the username is associated with the email address:</p> <pre><code> public static bool CheckUsernameAndEmail(string username, string email) { bool validateUser = false; var query = from u in db.User where (u.email.Equals(email) &amp;&amp; u.username.Equals(username)) select u; if (query.Count() != 0) validateUser = true; return validateUser; } </code></pre> <p>This the Controller code:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using MvcApplication1.ViewModels; using MvcApplication1.Database_Queries; namespace MvcApplikation1.Controllers { public class ContactAdminController : Controller { [HttpGet] public ActionResult SendMail() { return View(); } [HttpPost] public ActionResult SendMail(ContactAdminViewModel contactAdmin) { if (ModelState.IsValid) { if (DQL.CheckUsernameAndEmail(contactAdmin.username, contactAdmin.email)) { MvcApplication1.Mail.SendMail.SendForgotPassword(contactAdmin.username, contactAdmin.email, contactAdmin.message); return RedirectToAction("LogIn", "Account"); } } else { ModelState.AddModelError("", "Your username is not associated with the email adress"); return RedirectToAction("LogIn", "Account"); } return RedirectToAction("LogIn", "Account"); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload