Hello

I have one field on a Web form for a user to type in his email address. If I leave the field blank and press submit, I get this error:

Server Error in '/' Application.

System.NullReferenceException: Object reference not set to an instance of an object.

The error refers to this line in my aspx.vb file:

cmd.Parameters.AddWithValue("@strEmail", Request.QueryString("UserEmail").ToString())

'strEmail' is the name of the column for email in my MS Access database, and 'UserEmail' is the ID of the TextBox which the user types his email address into.

If I go to my aspx.vb file and debug, I can see the error here (attached screenshot). What does the error mean, please, and how may I correct it?

Thank you.

One of your objects is null. Either cmd, Request, or Request.QueryString("UserEmail").

Thanks for your reply, pritaeas

It seems that the error is related to this:

Click Here

If I place the mouse over cmd, I get: cmd| Nothing but I am not sure what I am supposed to do about it! Is the error implying that OleDbCommand is incorrect - as in Dim cmd As OleDbCommand? - since in the original code that was SQL Command but I have changed it to suit my Access database.

Sorry if I am a bit vague.

Dim cmd As OleDbCommand

This just defines your variable, but does not create an instance. You'll need something like this (not sure how VB syntax should be in this case).

Dim cmd As OleDbCommand = New OleDbCommand()

Yes, that sounds right, but even with:

 Dim cmd As OleDbCommand = New OleDbCommand

I still get that same error. I have this:

Protected Sub btnPassSend_Click(sender As Object, e As EventArgs) Handles btnPassSend.Click

       conn = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("students").ConnectionString)

        conn.Open()

        If conn.State = ConnectionState.Closed Then

            conn.Open()

        End If

        Using conn As OleDbConnection = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("students").ConnectionString)

          adp = New OleDbDataAdapter("SELECT strEmail FROM university WHERE strEmail=@strEmail", conn)

            cmd.Parameters.AddWithValue("@strEmail", Request.QueryString("UserEmail").ToString())
            cmd.ExecuteNonQuery()
            conn.Close()

I don't really need that first 'conn', do I? The:

 conn = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("students").ConnectionString)

because I have 'Using' shortly afterwards.

You should only use one connection. The second lacks an Open() though.

I have got rid of the first connection, tidied things up a bit, and now have this:

Protected Sub btnPassSend_Click(sender As Object, e As EventArgs) Handles btnPassSend.Click

Using conn As OleDbConnection = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("students").ConnectionString)

            If conn.State = ConnectionState.Closed Then
                conn.Open()

            End If

            Dim sbody As New StringBuilder()

 body.Append("<a href=http://usingasp.net/reset_pwd.aspx?email=" + LostPwd.Text & ")Please click here to reset your password</a>")

'SMTP code that a) sends email to user with pwd reset link and informs him on 
'screen by means of a message TextBox, or b) informs the user on screen by means 
'of a message TextBox that no email can be sent

conn.Close() 'close the database

End Using

    End Sub

I don't really need that final conn.Close, do I, because it is closed further upfield?

Check Request.QueryString("UserEmail"). Is the value null? Note that the return type is string, so the '.ToString()' should be removed
From MSDN:
The following code example shows two ways to get the value of a query string variable named “fullname”. In each case, if the URL is http://www.contoso.com/default.aspx?fullname=Fadi%20Fakhouri, then the value returned is "Fadi Fakhouri" because the %20 is URL-decoded into a space character. If the URL doesn’t have a fullname query string ID, the returned value would be null.

Hello SteveDotNet

I have changed a couple of things since my original post. My code now looks like this:

Dim cmd As OleDbCommand = New OleDbCommand

        Dim conn As New OleDbConnection()
        Dim adp As OleDbDataAdapter

    Protected Sub btnPassSend_Click(sender As Object, e As EventArgs) Handles btnPassSend.Click

        Dim Connection As String = "Data Source=|DataDirectory|students.mdb;"

        Using conn As OleDbConnection = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("students").ConnectionString)

            If conn.State = ConnectionState.Closed Then
                conn.Open()

            End If

            adp = New OleDbDataAdapter("SELECT strEmail FROM university WHERE strEmail=@strEmail", conn)

            cmd.Parameters.AddWithValue("@strEmail", UserEmail.Text)

            cmd.ExecuteReader()

           conn.Close()

            Dim sbody As New StringBuilder()

           sbody.Append("<a href=http://usingasp.net/reset_pwd.aspx?email=" + LostPwd.Text & ")Please click here to reset your password</a>")

If I leave the email field blank and press 'Send', I get the following error:

System.InvalidOperationException: ExecuteReader: Connection property has not been initialized.

The error refers to this line: cmd.ExecuteReader()

I have looked online but can't find a solution or they don't seem appropriate.

Thanks again for your reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.