这对我来说很奇怪。
我试图获取此代码的存储过程版本,但没有成功。
这是一个培训应用程序。用户必须先注册才能创建帐户。
然后他们使用他们的帐户注册课程。
首先,代码检查该用户是否已注册特定课程。
如果没有,则注册用户。如果是,请通知该用户他/她已经注册。
这一点有效。
如果用户尚未注册并正在尝试注册,请检查是否仍有可用席位。如果是,请注册用户。如果没有更多座位可用,则通过将用户插入名为 tblWaitinglist 的表中来将用户列入等候名单。
到目前为止,每次我尝试插入一条新记录时,它都会说,课程已满,用户正在等待列表中。
这不是真的。类完全是空的。
共有45个座位。
到目前为止,所有座位都可用。
我到底做错了什么?
它甚至没有插入等候名单。它只是给出了它所做的信息。
请在下面查看我的代码,并提前非常感谢。
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim username = Session("Username")
Dim connStr As String = ConfigurationManager.ConnectionStrings("DBConnectionString").ConnectionString
Dim conn As New SqlConnection(connStr)
conn.Open()
Try
Dim s As String
Dim counter As Integer
'If user already registered for a class, alert user
s = "SELECT Count(*) FROM tblTrainings WHERE Username = '" & username & "' AND CourseID = " & Request.QueryString("cosId") & " AND LocationID = " & Request.QueryString("locid") & " AND dateId = " & Request.QueryString("iddate") & ""
'Response.Write(s)
'Response.End()
Dim connSt As String = ConfigurationManager.ConnectionStrings("DBConnectionString").ConnectionString
Dim connc As New SqlConnection(connSt)
Dim cmdc As New SqlCommand(s, connc)
connc.Open()
cmdc.ExecuteNonQuery()
counter = cmdc.ExecuteScalar()
' Now let's see if we found existing record of registration
If counter = 0 Then 'User has not registered for this training. In that case, check to see there are still seats available.
Dim SeatsAvailable As Integer
SeatsAvailable = 0
s = " SELECT SeatsAvailable = (Select Seating_Capacity - (Select count(*) from tblTrainings where courseId = @cosId) from tblLocations WHERE LocationId = @Locid)"
'Response.Write(s)
'Response.End()
Dim cmdB As New SqlCommand(s, conn)
cmdB.Parameters.AddWithValue("@cosID", Request.QueryString("cosId"))
cmdB.Parameters.AddWithValue("@locID", Request.QueryString("locid"))
cmdB.ExecuteNonQuery()
If SeatsAvailable > 0 Then 'Ok there are still seats available. Sign this user up.
s = "INSERT INTO tblTrainings (CourseId, LocationId, dateId,username) VALUES (@CosID, @LocID, @dat, @Username)"
Dim cmd = New SqlCommand(s, conn)
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)
'Response.Write(s)
'Response.End()
cmd.ExecuteNonQuery()
Dim cmdGetKey As New SqlCommand("SELECT @@IDENTITY", conn)
Dim skey As Integer = cmdGetKey.ExecuteScalar()
Session("TrainingId") = skey
conn.Close()
confirmRegistraction()
Label1.ForeColor = System.Drawing.Color.Red
Label1.Text = "Congratulations! You have been registered for this class. Please check your email inbox for details"
Else 'No seats remain. So, put user on waiting list
s += "INSERT INTO tblWaitingList (CourseId, LocationId, dateId,username) VALUES (@CosID, @LocID, @dat, @Username)"
Dim cmd = New SqlCommand(s, conn)
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)
'Response.Write(s)
'Response.End()
cmd.ExecuteNonQuery()
Dim cmdGetKey As New SqlCommand("SELECT @@IDENTITY", conn)
Dim skey As Integer = cmdGetKey.ExecuteScalar()
Session("TrainingId") = skey
conn.Close()
onWaitingList()
'Display some feedback to the user to let them know it was processed
Label1.ForeColor = System.Drawing.Color.Red
Label1.Text = "Sorry, but this class is full. However, you have been placed on waiting list."
End If
Else
'Alert user that s/he has already registered for this class
Label1.ForeColor = System.Drawing.Color.Red
Label1.Text = "You have already signed up for this training."
End If
Catch
'If the message failed at some point, let the user know
Label1.ForeColor = System.Drawing.Color.Red
Label1.Text = "Your record failed to save, please try again."
End Try
End Sub