Null Reference not set to an instance of an object

I am working on an asp.net site and the problem I am getting is an error saying the following:

'NULL REFERENCE NOT SET TO AN INSTANCE OF AN OBJECT'

and below is the line of code where I get this problem:

            drLog.UserName =  Membership.GetUser().UserName;

The problem is when I set the line of code above to be  the following which is:

drLog.UserName = new Membership.GetUser().UserName;

I get an error that says the following:
System.Web.Security.Membership.GetUser(object, bool)' is a 'method' but is used like a 'type'

What can I do to get past this problem?  Thanks!
VBBRettAsked:
Who is Participating?
 
abelCommented:
Ah, you are on C#, not on VB, you didn't specify the language in the question. The code I showed you was VB and you are programming in C# (btw: please use the code snippet window for larger snippets of code). So, in C# this would yield the same, but the syntax is of course slightly different:

MembershipUser myUser = Membership.GetUser();
if(myUser == null)
{
    // there's no user, let's call it "<NOT_LOGGED_IN>"
    // you probably want to replace this line:
    drLog.UserName = "<NOT_LOGGED_IN>";
}
else
{
    // there's a user:
    drLog.UserName = myUser.UserName;
}

Open in new window

0
 
abelCommented:
> System.Web.Security.Membership.GetUser(object, bool)' is a 'method' but is used like a 'type'

that is because of the new keyword on the method GetUser and that cannot be done. Try the following:

Dim myUser as MembershipUser = Membership.GetUser()If myUser Is Nothing Then    ' no user found that's logged inElse    ' there's a user, do something    drLog.UserName = myUser.UserNameEnd If
0
 
VBBRettAuthor Commented:
Would I put this right above the code where I have the error?
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
VBBRettAuthor Commented:
So how would I do this given that I have the following as code on my page?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using SMMC.IT.BusinessObjects;
using SMMC.IT.DataDefinition.EntityObjects;


namespace SMMC.IT.Web.UI
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {

        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            Exception oErr = Server.GetLastError().GetBaseException();
            CommonSystem oCommon = new CommonSystem();
           
            CommonDataSet.ErrorLogDataTable dtLog = new CommonDataSet.ErrorLogDataTable();
            CommonDataSet.ErrorLogRow drLog = (CommonDataSet.ErrorLogRow) dtLog.NewErrorLogRow();
           

            drLog.UserName = Membership.GetUser().UserName;
            drLog.ErrorMsg = oErr.Message.ToString();
            drLog.StackTrace = oErr.StackTrace.ToString();
            dtLog.AddErrorLogRow(drLog);
            oCommon.InsertErrorLog(dtLog);

            Server.ClearError();
            System.Web.HttpContext.Current.Response.Redirect("ErrorPage.htm", true);
           
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}
0
 
VBBRettAuthor Commented:
Does anybody have an idea?
0
 
VBBRettAuthor Commented:
So where would I place this code that you sent me in the large block of code that I sent out?  Please let me know, thank you!
0
 
abelCommented:
Like I said earlier, on the line that's shining up with the error. In other words:

    drLog.UserName = Membership.GetUser().UserName;

0
 
VBBRettAuthor Commented:
It worked, you are the MAN!  Thank you so much!!
0
 
VBBRettAuthor Commented:
Your answer helped me very much, once again.  Thank you!
0
 
abelCommented:
You're welcome, glad I could be of some help ;)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.