忘记密码asp.net

时间:2014-11-10 13:34:32

标签: asp.net vb.net

这是关于忘记密码。面临的错误是索引超出此声明的范围"如果ds.Tables(0).Rows.Count> 0然后"

    Dim com As New MySqlCommand
    Dim dr As MySqlDataReader
    conn.Open()
    Dim query As String

    query = "select Password, CustomerName from userdetail where Email = @Email"

    com = New MySqlCommand(query, conn)

    com.Parameters.AddWithValue("@Email", Email.Text)

    dr = com.ExecuteReader



    If dr(0).ToString > 0 Then

        Dim Smtp_Server As New SmtpClient
        Dim e_mail As New MailMessage()

        Smtp_Server.UseDefaultCredentials = False
        Smtp_Server.Credentials = New Net.NetworkCredential("xxx", "xxx")
        Smtp_Server.Port = 587
        Smtp_Server.EnableSsl = True
        Smtp_Server.Host = "smtp.gmail.com"

        e_mail = New MailMessage()
        e_mail.From = New MailAddress("xxx")
        e_mail.To.Add(Email.Text)
        e_mail.Subject = "Your Password Details"
        e_mail.IsBodyHtml = True
        e_mail.Body = "Hi, <br/>Please check your Login Detailss<br/><br/>Your Username: " &
                        Convert.ToString(ds.Tables(0).Rows(0)("CustomerName")) & "<br/><br/>Your Password: " &
                        Convert.ToString(ds.Tables(0).Rows(0)("Password")) & "<br/><br/>"
        Smtp_Server.Send(e_mail)

    Else
        Label7.Text = "The Email you entered not exists"

    End
    conn.Close()

我的数据库设计: UserId,密码,客户名称,联系人,电子邮件,状态

2 个答案:

答案 0 :(得分:0)

您需要填充ds,我假设它是Dataset

Dim queryString As String = 
                "select Password, CustomerName from userdetail where Email = @Email"

Dim cmd As SqlCommand = conn.CreateCommand()
Dim da As New SqlDataAdapter()
cmd.Parameters.AddWithValue("@Email", Email.Text)
cmd.CommandText = queryString
da.SelectCommand = cmd
Dim ds As New DataSet()

conn.Open()
da.Fill(ds)

然后,您可以检查DataSet以获取记录。您还需要确保以下内容适用:

  1. Email字段唯一
  2. 发送给用户的链接,使他们能够重置密码,而不是发送明文。

答案 1 :(得分:0)

好的,您已更改了问题,现在您在MySqlDataReadersDataSet之间混淆了

MySqlDataReader解决方案应该更像

    Using cn As New MySqlConnection("YOURCONNECTIONSTRING")
        cn.Open()

        Using cmd As New MySqlCommand("select Password, CustomerName from userdetail where Email = @Email", conn)
            cmd.Parameters.AddWithValue("@Email", Email.Text)
            Dim dr As MySqlDataReader
            dr = cmd.ExecuteReader

            If dr.Read() Then

                'Did find a record to access the fields use = dr("FieldName").ToString()


            Else

                'didnt find a record
            End If

            dr.Close()
            dr = Nothing

        End Using

    End Using

Using将负责为您处理命令和连接。

然而,我还没有对此进行过测试,因此强调了#34;看起来更像是&#34;所有这些都说大卫的评论是完全正确的。在电子邮件中发送密码永远不会好。