为什么我得到"用户已经存在"信息?

时间:2015-02-12 16:16:11

标签: sql-server vb.net sql-server-2008

我对下面的代码片段有点困惑。

当用户尝试注册培训时,代码会检查此用户是否已经注册了此课程。

如果为true,则我们定义的错误消息会引发异常。

我尝试将我的帐户用于测试目的,并且我不断收到我已经报名参加课程的消息,但事实并非如此。

我在这里缺少什么?

 BEGIN
 Declare @Counter INT

    SELECT @Counter = COUNT(*) 
             FROM [Trainings]
             WHERE UserId = @userId AND CourseID = @cosId AND LocationID = @locid AND dateId = @dat
    IF @Counter > 0
     BEGIN
    --User has already signed up for this class. 
       RAISERROR ('You have already signed up for this training.',16,1)
       RETURN
      END
    ELSE
     BEGIN
    Declare @SeatsAvailable INT 
      --User has not already signed up for this class, then check to see if seat is still available
      SELECT @SeatsAvailable = (Select Seating_Capacity  - (Select count(*) from tblTrainings where courseId = @cosId) from Locations WHERE LocationId = @Locid)
     END
    END 

Protected Sub btnSAP_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnSAP.Click
    Dim userid = Session("userid")
    Dim Conn As SqlConnection
    Conn = New SqlConnection(ConfigurationManager.ConnectionStrings("DBConnectionString").ConnectionString)
    Conn.Open()
    Try

        Dim cmd As SqlCommand = New SqlCommand("signUp", 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.ExecuteNonQuery()
        Label1.ForeColor = Color.DarkGreen
        Label1.Text = "Congratulations! You have been registered for this class. An email has been placed in your inbox with registration details. If email is not in your inbox within 10 minutes (usually, immediately), please look into your spam box."

    Catch ex As Exception

        'If the message failed at some point, let the user know
        Label1.ForeColor = System.Drawing.Color.Red
        Label1.Text = ex.Message

    End Try
    Conn.Close()
End Sub

0 个答案:

没有答案