-
Apr 12th, 2018, 07:52 PM
#1
Thread Starter
Hyperactive Member
Error: Object reference not set to an instance of an object
Hello
I am getting the following server error message from my aspx page (newPassword.aspx) that allows for a user to change his password after he has forgotten his old one.
The user does that by clicking on a link sent to him via email after he has typed in his email in my forgor.aspx page. The email to the user works as does the link, but not when he presses 'Send' after typing in a new password. There are no rules such as 'you cannot have the same password twice', etc.
Object reference not set to an instance of an object.
Code:
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
newPassword.resetBtn_Click(Object sender, EventArgs e) +658
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9815206
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +204
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
+1639
Which part of my code - which I can copy and paste here - should I be looking at as the source of the problem, please?
Thank you.
-
Apr 12th, 2018, 09:43 PM
#2
Re: Error: Object reference not set to an instance of an object
Did you read the stack trace? It's telling exactly what method the exception was thrown in. That's what stack traces do. They tell you exactly what methods were being executed at the time and the one at the top was the last one called.
-
Apr 16th, 2018, 02:55 PM
#3
Thread Starter
Hyperactive Member
Re: Error: Object reference not set to an instance of an object
This discussion
https://stackoverflow.com/questions/...ance-of-object
suggests that the error message, 'object reference not set to an instance of object', means that a variable I have used = nothing. I assume that means 'redundant', so why use it? Another contributor to the discussion states that 'when working with databases, you can get this error when you try to get a value from a field or row which doesn't exist - you get the object "reference not set to an instance of object" if tablename doesn't exists in the Dataset. The same for rows or fields in the datasets.'. I have checked my table name and the fields and rows in my own MDB (see attached screenshot, DB.jpg, please)
and they correspond to my code:
Code:
Using conn As OleDbConnection = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("students").ConnectionString)
If Not Page.IsPostBack Then
cmd = New OleDbCommand("SELECT strEmail,uniqueCode FROM university" & vbTab & "WHERE uniqueCode=@uniqueCode AND (strEmail=@strEmail)", conn)
cmd.Parameters.AddWithValue("@uniqueCode", Convert.ToString(Request.QueryString("uniqueCode")))
cmd.Parameters.AddWithValue("@strEmail", Convert.ToString(Request.QueryString("strEmail")))
End If
dr = cmd.ExecuteReader()
End Using
so I don't think the error is database-related. I assume also that if the user's email address that he has typed in in the online form field (http://www.dimadayoub.net/forgot.aspx) is incorrect or one or more fields in my table are incorrect, that I would not have been redirected to the 'reset your password' field after the user has clicked on the reset link in the email he receives.
So the error must lie elsewhere. From what I can see in my code, all the variables declared are equal to something.
So I am a bit stuck!
Thanks
-
Apr 16th, 2018, 03:36 PM
#4
Re: Error: Object reference not set to an instance of an object
I hope that screenshot you just posted doesn't contain valid usernames and passwords. And, even if those are "test" accounts or something, it demonstrates that you are storing passwords in plain text.
You have fields for hash and salt, so maybe you are working your way through the process of doing this all the right way and are in the early stages of development where you just want to have the passwords plain text for testing purposes.
I'm assuming there is more code than what you've posted that gets ran when someone goes through the process of setting a new password. Without seeing more code, though, all I can do is speculate that you are missing a "New" somewhere in your code when you are trying to create an object.
-
Apr 16th, 2018, 03:52 PM
#5
Re: Error: Object reference not set to an instance of an object
This line:
Code:
dr = cmd.ExecuteReader()
...should be BEFORE the End If, because the variable cmd will only be set up when the If block is entered (so it is only worth .ExecuteReader when you run the other code too).
-
Apr 16th, 2018, 05:28 PM
#6
Thread Starter
Hyperactive Member
Re: Error: Object reference not set to an instance of an object
Thanks, OptionBase1 for your reply.
The rest of the code on the aspx.vb file looks like this:
Code:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Using conn As OleDbConnection = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("students").ConnectionString)
If Not Page.IsPostBack Then
cmd = New OleDbCommand("SELECT strEmail,uniqueCode FROM university" & vbTab & "WHERE uniqueCode=@uniqueCode AND (strEmail=@strEmail)", conn)
cmd.Parameters.AddWithValue("@uniqueCode", Convert.ToString(Request.QueryString("uniqueCode")))
cmd.Parameters.AddWithValue("@strEmail", Convert.ToString(Request.QueryString("strEmail")))
dr = cmd.ExecuteReader()
End If
End Using
lblExpired.Text = "Reset password link has expired"
lblExpired.Visible = True
Return
dr.Close()
dr.Dispose()
End Sub
Protected Sub resetBtn_Click(sender As Object, e As System.EventArgs)
Using conn As OleDbConnection = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("students").ConnectionString)
Dim Sql As String = "UPDATE university SET uniqueCode='',[password]=@password WHERE uniqueCode=@uniqueCode AND (strEmail=@strEmail)"
Dim cmd As New OleDbCommand(Sql, conn)
conn.Open()
cmd.Parameters.AddWithValue("@uniqueCode", Convert.ToString(Request.QueryString("uniqueCode")))
cmd.Parameters.AddWithValue("@password", txtNewPwd.Text.Trim())
cmd.Parameters.AddWithValue("@strEmail", Convert.ToString(Request.QueryString("strEmail")))
cmd.ExecuteNonQuery()
lblStatus.Text = "Password updated"
lblStatus.Visible = True
txtNewPwd.Text = String.Empty
txtConfirmPwd.Text = String.Empty
End Using
End Sub
Yes, the passwords in the screenshot are fictitious. I haven't got round to working on the salt/hash yet - I need to resolve this password reset problem first, but thanks for pointing it out.
Steve
-
Apr 16th, 2018, 05:32 PM
#7
Thread Starter
Hyperactive Member
Re: Error: Object reference not set to an instance of an object
Thanks si_the_geek
I have corrected it in the code I have posted in my reply to OptionBase1.
I will upload the amended file and see if the error persists.
-
Apr 16th, 2018, 05:38 PM
#8
Re: Error: Object reference not set to an instance of an object
One thing I noticed is that in your resetBtn_Click method you are declaring your cmd variable with a Dim statement, but in Page_Load you aren't.
At the very least, for consistent code, I would use this in Page_Load:
Code:
Dim cmd As New OleDbCommand("SELECT strEmail,uniqueCode FROM university" & vbTab & "WHERE uniqueCode=@uniqueCode AND (strEmail=@strEmail)", conn)
-
Apr 16th, 2018, 06:05 PM
#9
Thread Starter
Hyperactive Member
Re: Error: Object reference not set to an instance of an object
Can I ask if I have dr.Close in the wrong place because I get the following error using the code posted to OptionBase1:
ExecuteReader requires an open and available Connection. The connection's current state is closed.
In my code, isn't ExecuteReader open and available before the connection is closed?
Code:
dr = cmd.ExecuteReader()
End If
End Using
lblExpired.Text = "Reset password link has expired"
lblExpired.Visible = True
Return
dr.Close()
dr.Dispose()
Thanks again
-
Apr 16th, 2018, 06:27 PM
#10
Re: Error: Object reference not set to an instance of an object
What is dr and where/how is it defined?
Why do you have that Return statement?
Why do you have that code after the Return statement? If dr is only used inside of your If Not IsPostBack block, then the closing and disposing of dr should also be inside that If Not IsPostBack block, but I'm not entirely following what you are trying to accomplish inside of Page_Load, so I don't know what additional code you need or what code you have that you may not need.
Also, in ResetBtn_Click you have a conn.Open statement, so I'm not sure why you wouldn't have one in Page_Load as well.
Last edited by OptionBase1; Apr 16th, 2018 at 06:31 PM.
-
Apr 16th, 2018, 06:35 PM
#11
Thread Starter
Hyperactive Member
Re: Error: Object reference not set to an instance of an object
I see what you mean. The code is more sloppy than I thought. This is what I have now:
Code:
If Not Page.IsPostBack Then
'Check if the email address and generated unique code is same then the panel for resetting password will be visible otherwise not
Dim cmd As New OleDbCommand("SELECT strEmail,uniqueCode FROM university" & vbTab & "WHERE uniqueCode=@uniqueCode AND (strEmail=@strEmail)", conn)
cmd = New OleDbCommand("SELECT strEmail,uniqueCode FROM university" & vbTab & "WHERE uniqueCode=@uniqueCode AND (strEmail=@strEmail)", conn)
cmd.Parameters.AddWithValue("@uniqueCode", Convert.ToString(Request.QueryString("uniqueCode")))
cmd.Parameters.AddWithValue("@strEmail", Convert.ToString(Request.QueryString("strEmail")))
dr = cmd.ExecuteReader()
dr.Close()
dr.Dispose()
End If
End Using
lblExpired.Text = "Reset password link has expired"
lblExpired.Visible = True
Return
'dr.Close()
'dr.Dispose()
Before, the dr.Close was unrelated to ExecuteReader on account of the EndIf EndUsing statements.
I will compile it and test it again.
Thanks again for your time.
-
Apr 16th, 2018, 06:36 PM
#12
Re: Error: Object reference not set to an instance of an object
I think you still need a conn.Open statement before your dr = cmd.ExecuteReader statement
-
Apr 16th, 2018, 07:14 PM
#13
Thread Starter
Hyperactive Member
Re: Error: Object reference not set to an instance of an object
I just don't understand this. Having incorporated your conn.Open(), I now get:
Parameter @uniqueCode has no default value.
What exactly is it looking for? The error seems to support the comment I quoted in my #3 that 'the error message, 'object reference not set to an instance of object', means that a variable I have used = nothing.
-
Apr 16th, 2018, 07:29 PM
#14
Re: Error: Object reference not set to an instance of an object
This is the nature of "crash at the first error" troubleshooting. Your code had (and possibly still has) multiple issues, and you need to work through them one at a time. Declaring cmd with Dim got rid of your first issue, doing a conn.Open got rid of the next one, and here you are at the third issue.
I'm unable to assist further at this time, hopefully someone else can give you some more pointers.
-
Apr 16th, 2018, 07:44 PM
#15
Thread Starter
Hyperactive Member
Re: Error: Object reference not set to an instance of an object
That's fine, OptionBase - very many thanks for all your troubles.
-
Apr 16th, 2018, 08:54 PM
#16
Re: Error: Object reference not set to an instance of an object
Firstly, why are you creating two commands? Secondly why are you pointlessly concatenating a Tab character into your SQL? As for the issue, have you actually debugged and determined exactly what value is being used to set that parameter? You don't just look at your code and throw your hands up because it looks right. You execute it and watch it line by line to make sure it actually does what you expect. If an error message tells you that a value is wrong then you look at that value, not just the code that uses it.
-
Apr 16th, 2018, 10:03 PM
#17
Thread Starter
Hyperactive Member
Re: Error: Object reference not set to an instance of an object
Thanks again.
This line points to the possible culprit, highlighted in VS, and the Exception Assistant appears:
Code:
dr = cmd.ExecuteReader()
System.Data.OleDb.OleDbException was unhandled by user code
ErrorCode=-2147217904
HResult=-2147217904
Message=Parameter @uniqueCode has no default value.
Source=Microsoft JET Database Engine
StackTrace:
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.OleDb.OleDbCommand.ExecuteReader()
at newPassword.Page_Load(Object sender, EventArgs e) in C:\Users\Steve\Documents\Visual Studio 2013\DimaFinal\newPassword.aspx.vb:line 34
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
MSDN here: https://msdn.microsoft.com/query/dev...ng-VB)&rd=true
indicates that my database connection code is suspect. The error, that is, Parameter @uniqueCode, has no default value, and is generated when a warning or error is returned by an OLE DB data source.
So it seems to be a database problem.
-
Apr 16th, 2018, 11:03 PM
#18
Re: Error: Object reference not set to an instance of an object
Originally Posted by SteveHi
So it seems to be a database problem.
Most unlikely. The warning returned by the OLE DB data source would be as a result of the SQL that you are sending to it being wrong. It's telling you that something is wrong with one of your parameters. It's telling you which parameter. It's telling you wants wrong with it. Why aren't you investigating that parameter, as I already suggested? I'd wager that this:
Code:
cmd.Parameters.AddWithValue("@uniqueCode", Convert.ToString(Request.QueryString("uniqueCode")))
is returning Nothing. That would be how a parameter has no value. ADO.NET doesn't work with Nothing. Database nulls are represented in ADO.NET by DBNull.Value.
By the way, what's the point of that Convert.ToString call? Indexing a NameValueCollection, which that QueryString property is, already returns a String reference so what does converting that String to a String do for you?
-
Apr 16th, 2018, 11:46 PM
#19
Thread Starter
Hyperactive Member
Re: Error: Object reference not set to an instance of an object
I have now changed
Code:
'cmd = New OleDbCommand("SELECT strEmail,uniqueCode FROM university" & vbTab & "WHERE uniqueCode=@uniqueCode AND (strEmail=@strEmail)", conn)
to
Code:
cmd = New OleDbCommand("SELECT strEmail,uniqueCode FROM university WHERE uniqueCode=@uniqueCode AND (strEmail=@strEmail)", conn)
and will work my way through your other points. I did take up your idea about going through the code line by line, but I never got as far as these two lines that you refer to:
Code:
cmd.Parameters.AddWithValue("@uniqueCode", Convert.ToString(Request.QueryString("uniqueCode")))
cmd.Parameters.AddWithValue("@strEmail", Convert.ToString(Request.QueryString("strEmail")))
Concerning those two lines, I will remove the 'Convert.ToString' as the job has already been undertaken by QueryString.
Thanks again.
-
Apr 17th, 2018, 12:17 AM
#20
Re: Error: Object reference not set to an instance of an object
Request.QueryString is a NameValueCollection. The Item property of a NameValueCollection, which you are invoking by indexing the property that way, will return a String containing the value for the specified key if it exists and Nothing if the key does not exist. All that is available in the documentation and takes a couple of minutes to access after clicking on the QueryString property and pressing F1. If there is no "uniqueCode" in your query string, you will get Nothing for the Value of that parameter and that will lead to an error when the database tries to process your query and, I believe, that error message.
-
Apr 17th, 2018, 12:51 AM
#21
Thread Starter
Hyperactive Member
Re: Error: Object reference not set to an instance of an object
TheNameValueCollection, as you probably know, is explained here https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx so I will try to get at least the gist of it.
I have checked my db for the email address that the user typed in to the forgotten email address field before receiving the token link. In the uniqueCode column there is a value, something like this a3c5c318882x457291b5551b0b322937.
-
Apr 17th, 2018, 01:13 AM
#22
Re: Error: Object reference not set to an instance of an object
Originally Posted by SteveHi
In the uniqueCode column there is a value, something like this a3c5c318882x457291b5551b0b322937.
That's not elevant. It's the value you're using to set the parameter that matters. Just debug your code. Put a breakpoint on the line I flagged earlier and then, when it's hit, test the value of Request.QueryString("uniqueCode").
-
Apr 17th, 2018, 10:35 AM
#23
Thread Starter
Hyperactive Member
Re: Error: Object reference not set to an instance of an object
I put a breakpoint here:
Code:
cmd.Parameters.AddWithValue("@uniqueCode", Convert.ToString(Request.QueryString("uniqueCode")))
In the Call Stack I get this:
App_Web_4bws0rdk.dll!newPassword.Page_Load(Object sender, System.EventArgs e) Line 31 Basic
which doesn't mean much to me.
This is what I can see:
So the error seems to be this:
An exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll but was not handled in user code
{"Parameter @uniqueCode has no default value."}
How to correct it is something else as VS does not provide guidance.
-
Apr 17th, 2018, 11:33 AM
#24
Re: Error: Object reference not set to an instance of an object
If I were in your shoes, the first thing I would be doing is taking several steps back and using code with known values to see if i get expected results. That might mean modifying your SQL statement to have hard-coded values in the query and not even using parameters to start with. Then, once that is determined to be working, then use parameters with hard-coded values rather than trying to pull values from Request.QueryString. And so on.
In order for Request.QueryString to return values, those values have to be passed at the end of the URL with name=value pairs, like ResetPassword.aspx?uniqueCode=xyz&strEmail=something@somedomain.com
If your URL's don't look like that, then you have to resolve that issue first before you can try to pull values with Request.QueryString
-
Apr 17th, 2018, 06:05 PM
#25
Re: Error: Object reference not set to an instance of an object
Originally Posted by SteveHi
I put a breakpoint here:
Code:
cmd.Parameters.AddWithValue("@uniqueCode", Convert.ToString(Request.QueryString("uniqueCode")))
Wonderful. That's half of what I said to do. Now where's the other half? Why have you not determined what the value of Request.QueryString("uniqueCode") is and relayed that to us? If you ask for help and then ignore instructions from those trying to help then what's the point? If you're not going to do as I suggest then you're wasting my time and I'm not likely to feel inclined to volunteer more of it. What is the issue here? Do you not know how to evaluate that expression? If not, say so. If you do, why are you refusing to do it?
-
Apr 17th, 2018, 07:16 PM
#26
Thread Starter
Hyperactive Member
Re: Error: Object reference not set to an instance of an object
No, I am not sure which stage to progress to beyond my last post.
-
Apr 17th, 2018, 07:42 PM
#27
Re: Error: Object reference not set to an instance of an object
You seem to lack any knowledge of how to debug your code if you can't evaluate a simple expression in the debugger. That should be your first order of business. Debugging is a critical skill for a developer and you aren't going to be able to get far without it. I suggest that you search the web and do some reading on debugging in VB.NET. You need to be able to set breakpoints, step through code and use tools like the Autos, Locals, Watch and Immediate windows to evaluate the state of your application as you step.
-
Apr 17th, 2018, 08:15 PM
#28
Thread Starter
Hyperactive Member
Re: Error: Object reference not set to an instance of an object
do some reading on debugging in VB.NET.
That sounds like good advice. I will post back when I have understood a bit more.
Thanks.
-
Apr 18th, 2018, 03:04 AM
#29
Re: Error: Object reference not set to an instance of an object
take this line -
Code:
Convert.ToString(Request.QueryString("uniqueCode")
and add it to a variable
Code:
dim test as string
test = Convert.ToString(Request.QueryString("uniqueCode")
bookmark this line and when you run the code hover over "test" and see what its value is.
Your error appears to suggest that it doesn't have a value, so checking whether it does or not should be your next step
Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you
-
Apr 18th, 2018, 06:11 AM
#30
Re: Error: Object reference not set to an instance of an object
-
Apr 18th, 2018, 06:21 AM
#31
Re: Error: Object reference not set to an instance of an object
Originally Posted by si_the_geek
The version of that page for VS 2015 and earlier was the second result in a Bing search for "vb.net debugging". The second line of that page includes a link to the VS 2017 version.
-
Apr 18th, 2018, 04:54 PM
#32
Thread Starter
Hyperactive Member
Re: Error: Object reference not set to an instance of an object
Many thanks NeedSomeAnswers.
test = Nothing as you said.
-
Apr 1st, 2020, 02:56 PM
#33
New Member
Re: Error: Object reference not set to an instance of an object
can any one help I'm getting the same error.
I trying to open my crystal report using crystal report viewer
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|