Asp.net 记录插入到错误的表中。你知道为什么以及如何修复它吗?
这对我来说很奇怪 我试图获取此代码的存储过程版本,但没有成功 这是一个培训应用程序。用户必须先注册才能创建帐户 然后他们用自己的账户注册上课 首先,代码检查此用户是否已注册特定类 如果没有,请向用户注册。如果是,则通知该用户他/她已注册 这个钻头有效 如果用户尚未注册并且正在尝试注册,请检查是否还有可用的座位。如果是,请向用户注册。如果没有更多的座位可用,将用户插入名为tblWaitinglist的表中,将其放入等待列表 到目前为止,每次我尝试插入一条新记录时,它都会说,类已满,用户正在等待列表中 事实并非如此。类是完全空的 总共有45个座位 到目前为止,所有座位都有空 我到底做错了什么 它甚至没有插入到等待列表中。它只是传递了它确实做到了的信息 请看下面我的代码,并提前感谢Asp.net 记录插入到错误的表中。你知道为什么以及如何修复它吗?,asp.net,vb.net,Asp.net,Vb.net,这对我来说很奇怪 我试图获取此代码的存储过程版本,但没有成功 这是一个培训应用程序。用户必须先注册才能创建帐户 然后他们用自己的账户注册上课 首先,代码检查此用户是否已注册特定类 如果没有,请向用户注册。如果是,则通知该用户他/她已注册 这个钻头有效 如果用户尚未注册并且正在尝试注册,请检查是否还有可用的座位。如果是,请向用户注册。如果没有更多的座位可用,将用户插入名为tblWaitinglist的表中,将其放入等待列表 到目前为止,每次我尝试插入一条新记录时,它都会说,类已满,用户正在等待列表
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
看起来您从未将SeatAvailable设置为零以外的任何值。您的s变量可能就是您想要查看的
插入失败是另一个问题。您应该使用
或try/catch/finally
来清理代码中的资源。问题似乎是SeatsAvailable
始终为0。你试过调试吗?也许是因为你从来没有给它分配过0以外的任何东西s=“SELECT SeatsAvailable[…]”
不会神奇地将查询的输出写入名为seatsailable的变量中,您必须从查询结果中分配它。不幸的是,您调用了ExecuteNonQuery()
,这不允许您这样做。首先@CodeCaster,很抱歉代码和文本堆积如山。有时,很难说什么时候足够就足够了,因为总是会出现并说,“没有足够的信息或代码”来回答您的问题,SeatsAvailable并不总是0。如果我在SSMS上运行该查询,它将输出正确的值,在本例中为45。同样,正如前面所说的,即使它说,你们已经被放到了waitinglist中,一条记录也应该被插入到waitinglist表中。未向表中插入任何内容。您的程序未在SSMS中运行查询。它也不会将查询结果分配给任何对象。设置断点。检查seatsavaailable
变量。看,它总是0。请看一下ExecuteScalar()
method.FYI,这并不是真正使用存储过程,因为您仍然在使用内嵌式SQL。@Dourglas和CodeCaster,实际上我在一件事上错了。正在将记录插入WaitingList表,是,消息是正确的。但问题是,当SeatsAvailable大于0时,它应该插入到训练表中。我的逻辑错了吗?我现在开始工作了。我删除了SeatsAvailable=0,改为使用:SeatsAvailable=cmdB.ExecuteScalar(),它可以工作。你们是最棒的。非常感谢。