我有一个 LINQ VB.NET 项目。我有一个表“用户”。我将所有用户详细信息输入到表中。这包括和电子邮件地址。我有一种随机生成密码的方法。我有另一种方法,它根据在文本框中输入的内容调用电子邮件地址,并将密码发送到该电子邮件地址。我的问题是我不知道如何将新生成的密码插入到属于我刚刚调用的电子邮件地址的行中。输入电子邮件地址的文本框是“txtEmail”。可以假设“用户”的输入已经完成,减去密码。以下方法生成密码:
Public Function GeneratePassword() As String
'String variables tells how many characters password will contain.
Dim PasswordLength As Int16 = "12"
'Empty string will hold randomly generated password
Dim NewPassword As String = ""
'Characters allowed in this new password
Dim allowedChars As String = ""
allowedChars = "1,2,3,4,5,6,7,8,9,0"
allowedChars += "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,"
allowedChars += "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,"
'Declare array to house each character generated from allowed list of characters
Dim sep As Char() = {","c}
Dim arr As String() = allowedChars.Split(sep)
Dim IDString As String = ""
Dim temp As String = ""
'utilize the "random" class
Dim rand As New Random()
'loop through the character generation process until PasswordLength is met
For i As Integer = 0 To Convert.ToInt32(PasswordLength) - 1
temp = arr(rand.[Next](0, arr.Length))
IDString += temp
NewPassword = IDString
Next
'places new password in variable
Return NewPassword
End Function
接下来,以下方法从数据库中获取电子邮件地址,调用“GeneratePassword”方法并将密码通过电子邮件发送给用户。它不会将密码保存到数据库,这是我遇到的问题。
Protected Sub btnPassword_Click(sender As Object, e As System.EventArgs) Handles btnPassword.Click
If True Then
'establish connection with database
Dim db As New OrionDataClassesDataContext()
'initialise global variabel to take email string entered in text box
GlobalVariables.SearchUserEmail = txtEmailAddress.Text
'initialise global variable to hold email address retrieved from database
GlobalVariables.CurrentEmailAddress = (From u In db.Users
Where u.Email = SearchUserEmail
Select u.Email).FirstOrDefault
'calls generatepassword method
Dim stNewPassword As String = GeneratePassword()
'create new mailmessage to construct and sendemail
Dim Mail As New MailMessage
Try
Mail.Subject = "Your Password Has Been Reset"
'gets email address from database when user enters it
Mail.To.Add(GlobalVariables.CurrentEmailAddress)
'address for email to be sent from
Mail.From = New MailAddress("XXX@XXXX.com")
Mail.Body = "Your new pasword is: " + stNewPassword + ". Please keep this safe."
'define smpt server to be used
Dim SMTP As New SmtpClient("smtp.gmail.com")
SMTP.EnableSsl = True
'authenticate connection with smpt server and email address
SMTP.Credentials = New System.Net.NetworkCredential("XXXX@XXX.com", "XXXXX")
SMTP.Port = 587
SMTP.Send(Mail)
'alert when message is sent correctly
MsgBox("Email sent successfully!")
txtEmailAddress.Text = ""
Catch ex As Exception
'displays error message when email is not sent
MsgBox("Email not sent - Check address")
txtEmailAddress.Text = ""
End Try
End If
End Sub
我一直在玩一些场景,但我似乎总是最终只是试图创建一个全新的行条目,这不是我想要做的。任何意见,将不胜感激。