这对我来说很奇怪。
我试图获取此代码的存储过程版本,但没有成功。
这是一款培训应用。用户必须先注册才能创建帐户。
然后他们使用他们的帐户报名参加课程。
首先,代码会检查此用户是否已注册特定类。
如果不是,请先注册用户。如果是,请通知该用户他/她已经注册。
这位有效。
如果用户尚未签名并且正在尝试注册,请检查是否仍有座位可用。如果是,请注册用户。如果没有更多席位可用,请通过插入名为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
答案 0 :(得分:1)
看起来你永远不会将SeatsAvailable设置为零以外的任何东西。您的变量可能就是您想要查看的内容。
INSERT失败是另一个问题。