Why GetUser getting NullReferenceException???

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

Cowboy \(Gregory A. Beamer\)

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
 
L

Lloyd Sheen

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
 
C

Cirene

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top