Why GetUser getting NullReferenceException???

  • Thread starter Thread starter Cirene
  • Start date Start date
C

Cirene

I'm creating my own create user wizard.

Here's my code...
If Membership.GetUser(txtEzUsername.Text).ProviderUserKey.ToString
<> "" Then
Me.lblStatus.Text = "Username is in use. Please select another
Username."
Exit Sub
End If

Here's the error:
System.NullReferenceException was unhandled by user code
Message="Object reference not set to an instance of an object."
Source="App_Web_5nh_bhxb"
StackTrace:
at NewUserWizard.cmdCreate_Click(Object sender, EventArgs e) in
F:\work-related\websites\Lexitel\NewUserWizard.aspx.vb:line 87 at
System.Web.UI.WebControls.Button.OnClick(EventArgs e) at
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) at
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,
String eventArgument) at
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,
Boolean includeStagesAfterAsyncPoint)
InnerException:

How can I check if GetUser returns nothing (ie the user doesnt exist)?

Thanks.
 
You are blowing up on this part:

..ProviderUserKey.ToString

You can avoid this by doing this:

Dim user As MembershipUser = Membership.GetUser(txtEzUsername.Text)

If user is nothing then
'There is no user
Else
'Do what you want
End if

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

********************************************
| Think outside the box! |
********************************************
 
Cirene said:
I'm creating my own create user wizard.

Here's my code...
If Membership.GetUser(txtEzUsername.Text).ProviderUserKey.ToString
<> "" Then
Me.lblStatus.Text = "Username is in use. Please select another
Username."
Exit Sub
End If

Here's the error:
System.NullReferenceException was unhandled by user code
Message="Object reference not set to an instance of an object."
Source="App_Web_5nh_bhxb"
StackTrace:
at NewUserWizard.cmdCreate_Click(Object sender, EventArgs e) in
F:\work-related\websites\Lexitel\NewUserWizard.aspx.vb:line 87 at
System.Web.UI.WebControls.Button.OnClick(EventArgs e) at
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) at
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,
String eventArgument) at
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,
Boolean includeStagesAfterAsyncPoint)
InnerException:

How can I check if GetUser returns nothing (ie the user doesnt exist)?

Thanks.


The statement
If Membership.GetUser(txtEzUsername.Text).ProviderUserKey.ToString

expects that a user will be found. The .ProviderUserKey.ToString
expects a user object so you will have to test the
Membership.GetUser(txtEzUsername.Text) for nothing before you attempt to
use the object.

I would suggest a two step statement, the first to attempt to populate a
user object and if it is not nothing then do the .ProviderUserKey etc.

LS
 
Thanks Cowboy and Lloyd (and everyone else)!

Cowboy (Gregory A. Beamer) said:
You are blowing up on this part:

.ProviderUserKey.ToString

You can avoid this by doing this:

Dim user As MembershipUser = Membership.GetUser(txtEzUsername.Text)

If user is nothing then
'There is no user
Else
'Do what you want
End if

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

********************************************
| Think outside the box! |
********************************************
 
Back
Top