Hello

I am aiming to have an aspx file with a TextBox for the user to type in his email. When I leave the field blank, and press
'Submit', I get: "A password reset link has been sent to your email address"

Why don't I get the error message:

If UserEmail.Text = "" Then

        Label1.Text = "Please complete the required fields"
        Label1.Visible = True

    End If

as indicated in my code, please?

In fact, when I do type in an email address - an erroneous one - I still get the same message (the message is sent: I can see it in a folder on my C drive).

Here is my code:

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

    Dim conn As New OleDbConnection
    Dim OleDbConnection As New OleDbConnection
    Dim cmd As New OleDbCommand

    'Check if email field is empty

    If UserEmail.Text = "" Then

        Label1.Text = "Please complete the required fields"
        Label1.Visible = True

    End If

    ' Connect to Access and open

    conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|students.mdb;"

    conn.Open()


    'Check the user's email address in the strEmail column in Access corresponds to that entered by the user in 
    'the TextBox whose ID is "UserEmail"

    cmd = New OleDbCommand("SELECT strEmail FROM university WHERE strEmail=@strEmail", conn)

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

    cmd.Connection = conn

    cmd.ExecuteNonQuery()

   'close Access connection 

    conn.Close()

    Try

        Dim SMTPMail As New System.Net.Mail.MailMessage()

        SMTPMail.From = New MailAddress("John Doe <info@johndoe.net>") 'This is the Webmaster
        SMTPMail.[To].Add("you@yourcompany.com") 'This is the user
        SMTPMail.Bcc.Add("Webmaster@mySite.com")

        SMTPMail.Subject = "Link to reset your password"
        SMTPMail.SubjectEncoding = System.Text.Encoding.UTF8
        SMTPMail.IsBodyHtml = True 'IsBodyHtml is true because HTML tags are included
        SMTPMail.BodyEncoding = System.Text.Encoding.UTF8

        SMTPMail.Body = "Hello<br><br>Please click <a href='http://usingasp.net/reset_pwd.aspx'>here</a> to reset your password.<br><br>Regards<br><br>Dima Dayoub"

        Dim dirInfo As New DirectoryInfo("C:\TestEmails")

        If Not dirInfo.Exists Then

            Directory.CreateDirectory("C:\TestEmails")

        End If

        Dim SMTP As New SmtpClient()

        SMTP.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory

        SMTP.PickupDirectoryLocation = "C:\TestEmails"

        Dim mailAuthenticaion As New System.Net.NetworkCredential("info@mySite.com ", "SMTP server password")

        Dim SMTPserver As New System.Net.Mail.SmtpClient("smtp.mail.server", 25)

        SMTPserver.EnableSsl = True
        SMTPserver.Credentials = mailAuthenticaion

        SMTP.Send(SMTPMail)

        Label1.Text = "A password reset link has been sent to your email address"

        Label1.Visible = True


    Catch ex As Exception
        Label1.Text = "Error sending message. Please try again"
        Label1.Visible = False

    End Try
End Sub

Thanks for any help.

In your code, even if the text is equal to "" you continue to process code. What you need is to insert an "exit sub" if your if condition is true because you don't want the rest if the code to be processed.

Oh, right, I see what you mean - what's the point of all that code if we can't go any further? But how would I then resume the

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

because after the Exit Sub, that Sub has now come to an end, hasn't it?

Thanks.

If there is no data in the textbox you don't want to continue. If you do have data then you want your sub to continue. Hence using the if..else block.. Or if you don't explicitly use the else, then you want to exit the sub if there is no data for the Text property.

Also, rather the checking in the manner you proposed, I would recommend using the string.isNullOrEmpty method.

I'll check it out, Jorge, thank you!

Hello Jorge

I have simply used this in my passRecovery1.aspx file to check for a blank field:

 <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="UserEmail"
                    CssClass="formattedText" ErrorMessage="The Email field is required." />

The box with 'Email field required' disappears when I type in an email address and press 'Submit'. However, if I type in an invented email address that I know is not in the strEmail column of my Access database, I get the following message on screen:

"A password reset link has been sent to your email address". The email address is sent to the user because I can see it in my C:\TestEmails folder, but it shouldn't be sent because the email address does not exist in the database.

I don't get any debug errors, but something is clearly wrong.

Looks like you probably copied/pasted the above code because the logic is not correct.

I see you are performing a query to your DB but you are not storing the results of the query. So was there a match or not? You aren't checking.

Then regardless if there is a match or not you proceed with creating an email to send to

 SMTPMail.[To].Add("you@yourcompany.com")

You need a conditional statement after your lookup. If there is no records that match, you should not proceed with the password reset message.

Hello Jorge

I copied it from my Register.aspx page and pasted it under the TextBox called UserEmail.

You need a conditional statement after your lookup. If there is no records that match, you should not proceed with the password reset message.

Yes, spot on. It's obvious when it's pointed out!

commented: Great to see somebody working on the problem, using the answers, while asking a question, +13
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.