请原谅我问了太多问题。
我整天都在做这件事,不能再忍受了。
我有一个名为 sp_signup() 的存储过程
存储过程首先检查用户是否已经注册。如果是,则通知用户他/她已经注册了该课程。
SET @ERROR = 'You have already signed up for this training'
这是一种享受。
如果没有,然后检查是否还有可用的座位。
如果仍有空位,请通过插入培训表为用户注册并通知用户她/她已注册。
SET @ERROR = 'You have been registered for this class'
这是一种享受。
如果没有剩余座位,则通过将用户的注册信息插入到waitingList 表中来将用户加入等候名单,并通知用户她/他已被列入等候名单。
SET @ERROR = 'Sorry, but this class is full. However, you have been placed on waiting list.'
这也是一种享受。
因此,存储过程在测试时效果很好。
但是,在我们调用存储过程的 .net 应用程序上,我们正在向用户发送电子邮件,通知他们他们要么已注册课程,要么已根据 @Error 消息的内容被列入等候名单。
这是我们遇到问题的地方,因为电子邮件没有发出。
以下是发送电子邮件的条件:
If Label1.Text = "You have been registered for this class" Then
'Email code goes here and is sent to inform users they have been registered for class
ElseIf Label1.Text = "Sorry, but this class is full. However, you have been placed on waiting list." Then
'Email code goes here and is sent inform to users they have been placed on waiting list
Else
End If
下面是进行存储过程调用的地方:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim username = Session("Username")
Try
Dim s As String
s = "sp_signup"
Dim connStr As String = ConfigurationManager.ConnectionStrings("DBConnectionString").ConnectionString
Dim conn As New SqlConnection(connStr)
Dim cmd = New SqlCommand(s, conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@cosID", Request.QueryString("cosId"))
cmd.Parameters.AddWithValue("@locID", Request.QueryString("locid"))
cmd.Parameters.AddWithValue("@dat", Request.QueryString("iddate"))
cmd.Parameters.AddWithValue("@UserName", username)
cmd.Parameters.Add("@ERROR", SqlDbType.[Char], 500)
cmd.Parameters("@ERROR").Direction = ParameterDirection.Output
conn.Open()
cmd.ExecuteNonQuery()
message = DirectCast(cmd.Parameters("@ERROR").Value, String)
Dim cmdGetKey As New SqlCommand("SELECT @@IDENTITY", conn)
Dim skey As Integer = cmdGetKey.ExecuteScalar()
Session("TrainingId") = skey
conn.Close()
btnSendEmail_Click()
'Display some feedback to the user to let them know it was processed
Label1.ForeColor = System.Drawing.Color.Green
Label1.Text = message
Catch
'If the message failed at some point, let the user know
Label1.ForeColor = System.Drawing.Color.Red
Label1.Text = message
End Try
End Sub
电子邮件代码已经过测试,可以在没有存储过程的情况下工作,如下所示:
Protected Sub btnSendEmail_Click()
Dim skey As Integer = Session("TrainingId")
'Response.Write(skey)
'Response.End()
Dim Conn As SqlConnection
'Dim param As SqlParameter
'Dim cmdcommand As SqlCommand
Conn = New SqlConnection(ConfigurationManager.ConnectionStrings("DBConnectionString").ConnectionString)
Conn.Open()
If Label1.Text = "You have been registered for this class" Then
Dim emailcmd As New SqlCommand("select distinct lg.Email, lg.fullname, c.CourseName, l.location, d.trainingDates, d.trainingTime, i.instructorName from tblTrainings t Inner Join tblCourses c on t.courseId = c.courseId " & _
" Inner Join tblLocations l on t.locationId = l.LocationId " & _
" Inner Join tblTrainingDates d on t.dateid=d.dateid " & _
" Inner Join tblCourseInstructor ic on c.courseId = ic.CourseId " & _
" Inner Join tblInstructors i on ic.instructorId = i.instructorId " & _
" Inner Join tblLogin lg on t.username = lg.username where lg.username = '" & Session("username") & "' AND t.CourseID = " & Request.QueryString("cosId") & " AND t.LocationID = " & Request.QueryString("locid") & " AND t.dateId = " & Request.QueryString("iddate") & " AND TrainingId = " & skey & ";", Conn)
Dim dr = emailcmd.ExecuteReader()
If dr.Read() Then
email = dr.GetString(0)
fullname = dr.GetString(1)
courses = dr.GetString(2)
Loc = dr.GetString(3)
tdates = dr.GetDateTime(4)
ttime = dr.GetString(5)
End If
'code for other email requests
Dim objSmtpClient As SmtpClient = New SmtpClient("relay.smtp", 25)
Dim objSender As MailAddress = New MailAddress("name.emailadd.com", "name.emailadd.com")
Dim objMail As MailMessage = New MailMessage("name.emailadd.com", "name.emailadd.com")
objMail.Bcc.Add("name.emailadd.com")
objMail.To.Add(email)
'objMail.CC.Add("name.emailadd.com")
objMail.Subject = "About Your Training: " & courses & ""
objMail.Body = " Dear " & fullname & " <br>You have just signed up for <b>" & courses & "</b> training. <br><br>This training will be held at <b>" & Loc & "</b> on <b>" & tdates & "</b> starting from <b>" & ttime & "</b>.<br><br> For more details about this training, please visit <a href='Training/'> Training/</a>. "
objMail.IsBodyHtml = True
objSmtpClient.Send(objMail)
dr.Close()
ElseIf Label1.Text = "Sorry, but this class is full. However, you have been placed on waiting list." Then
Dim waitcmd As New SqlCommand("select distinct lg.Email, lg.fullname, c.CourseName, wl.location, d.trainingDates, d.trainingTime, i.instructorName from tblWaitingList wl Inner Join tblCourses c on wl.courseId = c.courseId " & _
" Inner Join tblLocations l on wl.locationId = l.LocationId " & _
" Inner Join tblTrainingDates d on wl.dateid=d.dateid " & _
" Inner Join tblCourseInstructor ic on c.courseId = ic.CourseId " & _
" Inner Join tblInstructors i on ic.instructorId = i.instructorId " & _
" Inner Join tblLogin lg on wl.username = lg.username where lg.username = '" & Session("username") & "' AND wl.CourseID = " & Request.QueryString("cosId") & " AND wl.LocationID = " & Request.QueryString("locid") & " AND wl.dateId = " & Request.QueryString("iddate") & " AND TrainingId = " & skey & ";", Conn)
Dim dr = waitcmd.ExecuteReader()
If dr.Read() Then
email = dr.GetString(0)
fullname = dr.GetString(1)
courses = dr.GetString(2)
Loc = dr.GetString(3)
tdates = dr.GetDateTime(4)
ttime = dr.GetString(5)
End If
'code for other email requests
Dim objSmtpClient As SmtpClient = New SmtpClient("relay.smtp", 25)
Dim objSender As MailAddress = New MailAddress("name.emailadd.com", "name.emailadd.com")
Dim objMail As MailMessage = New MailMessage("name.emailadd.com", "name.emailadd.com")
objMail.Bcc.Add("name.emailadd.com")
objMail.To.Add(email)
'objMail.CC.Add("name.emailadd.com")
objMail.Subject = "About Your Training: " & courses & ""
objMail.Body = " Dear " & fullname & " <br>You have been placed on the waiting list for <b>" & courses & "</b> training. <br><br>This training will be held at <b>" & Loc & "</b> on <b>" & tdates & "</b> starting from <b>" & ttime & "</b>.<br><br> Should a seat become available, notification will be based on first-come, first-served bases.<br>For more details about this training, please visit <a href='http://#/'> http://Training/</a>. "
objMail.IsBodyHtml = True
objSmtpClient.Send(objMail)
dr.Close()
Else
End If
End Sub