Razor (web pages) Object reference not set to an instance of an object

Hi experts!

Thanks in advance for reading this. For the following code:

@using System.Configuration;
@{
    var contactname = Request.Form["ContactName"];
    var contactemail = Request.Form["ContactEmail"];
    var contacttext = Request.Form["ContactText"];


  if ((!WebSecurity.IsAuthenticated) && (!IsPost))
    {
        var dbUser = Database.Open("SomeDB");
        var selectUser = "SELECT * FROM SomeTable WHERE UserId = @0";
        var myUser = dbUser.QuerySingle(selectUser, WebSecurity.CurrentUserId);

        if (contactname.Trim() == "")
        {
            contactname = myUser.Username;
        }
        
        if (contactemail.Trim() == "")
        {
            contactemail = myUser.email;
        }
       }
   }
   
  
  <form action="" method="post">
<div class="formStyle01">
    <div class="row">
        <div class="label">
            <label for="ContactName">
                Your Name:</label></div>
        <div class="input">@Html.TextBox("ContactName", contactname, new { maxlength = 100 })
            @Html.ValidationMessage("ContactName")</div>
    </div>
    <div class="row">
        <div class="label">
            <label for="ContactEmail">
                Your Email:</label></div>
        <div class="input">@Html.TextBox("ContactEmail", contactemail, new { maxlength = 250 })
            @Html.ValidationMessage("ContactEmail")</div>
    </div>
    ..
    ..
    ..
    ..
    <div>
    <input type="submit" value="Submit" /></div>
</form>

Open in new window


It error on this line:

        if (contactname.Trim() == "") <--
       
and the error msg is this:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 36:         var myUser = dbUser.QuerySingle(selectUser, WebSecurity.CurrentUserId);
Line 37:
Line 38:         if (contactname.Trim() == "")
Line 39:         {
Line 40:             contactname = myUser.Username;
 
Any idea?

TIA!
LVL 5
allanau20Asked:
Who is Participating?
 
Pratima PharandeConnect With a Mentor Commented:
var contactname = " "
    var contactemail = " "
    var contacttext = " "
if (Request.Form["ContactName"] != null)
    {
       contactname = Request.Form["ContactName"];
    }

if (Request.Form["ContactEmail"] != null)
    {
       contactemail = Request.Form["ContactEmail"];
}
if (Request.Form["ContactText"] != null)
{
 contacttext = Request.Form["ContactText"];
}
0
 
Pratima PharandeCommented:
check whether Request.Form["ContactName"] is exists or not ?
0
 
allanau20Author Commented:
Sorry; about how would you do that to the existing code? Thanks!
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
allanau20Author Commented:
I just started Razor, btw, .... thanks!
0
 
allanau20Author Commented:
so, tried this:

contactname = myUser.Username; 

Open in new window


and got this error:

Cannot perform runtime binding on a null reference
0
 
Pratima PharandeCommented:
try


change this
var contactname = Request.Form["ContactName"];
    var contactemail = Request.Form["ContactEmail"];
    var contacttext = Request.Form["ContactText"];


TO

var contactname = ''
    var contactemail = ''
    var contacttext = ''
if ((Request.Form["ContactName"] != null))
   
      var contactname = Request.Form["ContactName"];

if ((Request.Form["ContactEmail"] != null))
   
      var contactemail = Request.Form["ContactEmail"];
if ((Request.Form["ContactText"] != null))
   
      var contacttext = Request.Form["ContactText"];
   
   
0
 
allanau20Author Commented:
ok; I will try it now; thanks!
0
 
allanau20Author Commented:
got this error:



Error      3      Expected a "{" but found a "v".  Block statements must be enclosed in "{" and "}".  You cannot use single-statement control-flow statements in CSHTML pages. For example, the following is not allowed:    @if(isLoggedIn)      <p>Hello, @user</p>    Instead, wrap the contents of the block in "{}":    @if(isLoggedIn) {      <p>Hello, @user</p>  }        C:\Data\TheChurchPortal\TheChurchPortal\Contact.cshtml      24       

on these lines:

var contactname = Request.Form["ContactName"];
0
 
Pratima PharandeCommented:

var contactname = ''
    var contactemail = ''
    var contacttext = ''
if (Request.Form["ContactName"] != null)
    {
      var contactname = Request.Form["ContactName"];
    }

if (Request.Form["ContactEmail"] != null)
    {
      var contactemail = Request.Form["ContactEmail"];
}
if (Request.Form["ContactText"] != null)
{
var contacttext = Request.Form["ContactText"];
}
   
      
   
   
0
 
allanau20Author Commented:
almost there I think...

it gives this error:

Error      1      Empty character literal      

on these lines:

var contactname = ''

0
 
Pratima PharandeCommented:
var contactname = ""
    var contactemail = ""
    var contacttext = ""

if (Request.Form["ContactName"] != null)
    {
      var contactname = Request.Form["ContactName"];
    }

if (Request.Form["ContactEmail"] != null)
    {
      var contactemail = Request.Form["ContactEmail"];
}
if (Request.Form["ContactText"] != null)
{
var contacttext = Request.Form["ContactText"];
}
0
 
allanau20Author Commented:
Error on this line:

var contactname = Request.Form["ContactName"];

Error      1      A local variable named 'contactname' cannot be declared in this scope because it would give a different meaning to 'contactname', which is already used in a 'parent or current' scope to denote something else


      
0
 
Pratima PharandeCommented:
var contactname = ''
    var contactemail = ''
    var contacttext = ''
if (Request.Form["ContactName"] != null)
    {
      contactname = Request.Form["ContactName"];
    }

if (Request.Form["ContactEmail"] != null)
    {
     contactemail = Request.Form["ContactEmail"];
}
if (Request.Form["ContactText"] != null)
{
contacttext = Request.Form["ContactText"];
}
0
 
allanau20Author Commented:
Thanks for sticking with me..

back to the same error:

Error      1      Empty character literal       

on these lines:

var contactname = ''

Then tried this:

var contactname = "";

it error on:

Error      1      The name 'contactname' does not exist in the current context


0
 
allanau20Author Commented:
EUREKA!!!

That did it; thanks so much for helping me; I will get better as I work more with web pages.
Thanks again!
0
 
allanau20Author Commented:
super duper!
0
 
Pratima PharandeCommented:
glad to help you :)
0
All Courses

From novice to tech pro — start learning today.