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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
allanau20Author Commented:
I just started Razor, btw, .... thanks!
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.