Error: cmd.ExecuteNonQuery()

Discussion in 'ASP.NET / ASP.NET Core' started by Steve, Dec 18, 2014.

  1. Hello
    The first part of my code should compare the user's email address in 'UserEmail' - the name of the TextBox into which the user types his email address - with the entry for him in my Access database in the column called 'strEmail'.
    Whether or not I type in his correct email address and press 'Submit', or leave the field purposely blank and then press 'Submit', I get the same Server Error in '/' Application error when I preview the Web page in my browser:
    System.InvalidOperationException: ExecuteNonQuery: Connection property has not been initialized.
    The error refers specifically to this line in my code:
    cmd.ExecuteNonQuery()
    This first part of my code (the second part deals with SMTP authentication) looks like this:
    Code:
    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.ExecuteNonQuery()
      conn.Close()
      Dim sbody As New StringBuilder()
      'Send link to reset password
      sbody.Append("<a href=http://usingasp.net/reset_pwd.aspx?email=" + LostPwd.Text & ")Please click here to reset your password</a>")
      'sbody.Append(("&code=" & code & "&strEmail=") + LostPwd.Text & ">Please click here to reset your password</a>")
      'Send email with password reset link to the user
      'Pass four parameters inside sbody: the Webmaster's email, the recipient's email, the subject line, and the sbody.ToString()
      Try
      Dim SMTPserver As New System.Net.Mail.SmtpClient("smtp.mail.server", 25)
      Dim SMTPMail As New System.Net.Mail.MailMessage("LostPwd", "strEmail")
      Dim mailAuthenticaion As New System.Net.NetworkCredential("[email protected] ", "SMTP server password")
      SMTPserver.EnableSsl = True
      SMTPserver.Credentials = mailAuthenticaion
      
      SMTPMail.CC.Add("")
      SMTPMail.Bcc.Add("[email protected]")  
      SMTPMail.To.Add("[email protected]") 'Webmaster's email
      SMTPMail.From = New MailAddress("LostPwd.Text".ToString()) 'Email of the sender
      SMTPMail.Subject = "Please reset your password"
      SMTPMail.Body = sbody.ToString()
      SMTPMail.IsBodyHtml = True 'True because HTML tags are included
      SMTPMail.Priority = MailPriority.High
      SMTPserver.Send(SMTPMail)
      Response.Write(LostPwd.Text = "A password reset link has been sent to your email address")
      cmd.ExecuteNonQuery()
      cmd.Dispose()
      conn.Close()
      'LostPwd.Text = "A password reset link has been sent to your email address"
      Catch ex As Exception
      Response.Write("Could not send the e-mail - error: " + ex.Message)
      Finally
      conn.Close() 'close the database
      End Try
      End Using
      End Sub
    Regarding the error, I thought I had connected to the database, opened it, checked UserEmail against strEmail, and then closed the database.

    What am I doing wrong, please?
     
  2. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    According to Microsoft, it's only for UPDATE, INSERT, or DELETE statements. For SELECT, you should be using cmd.ExecuteReader.
     
    mjp likes this.
  3. Thanks for your reply, Ray.

    I will have a mess about with it and post back.

    Thanks again!
     
    mjp likes this.

Share This Page