这是我第一次在这里提问。我正在开发一个项目,我必须更新一个表,并使用事务插入到另一个表中。
事务块的插入部分将来自所选gridview行的内容。
我得到的错误是:
变量名称' @ userid2'已经宣布。变量名在查询批处理或存储过程中必须是唯一的。
如何解决此错误?
这是我的代码:
Protected Sub btnmodifyaccessdepot_Click(sender As Object, e As EventArgs) Handles btnmodifyaccessdepot.Click
Using con As New SqlConnection(ConString)
con.Open()
Dim sqlTran As SqlTransaction = con.BeginTransaction()
Dim command As SqlCommand = con.CreateCommand()
command.Transaction = sqlTran
Try
command.CommandText = "UPDATE Rhema_TechnicalAccess set Active=@Active,Requester= @Requester,Approver=@Approver,Issuer=@Issuer where userid=@userid"
command.Parameters.AddWithValue("@Active", lblactiveadd.Text)
command.Parameters.AddWithValue("@Requester", lblrequesterAdd.Text)
command.Parameters.AddWithValue("@Approver", lblapproveradd.Text)
command.Parameters.AddWithValue("@Issuer", lblisssueradd.Text)
command.Parameters.AddWithValue("@Userid", lblselecteduserid.Text)
command.ExecuteNonQuery()
For Each row As GridViewRow In gridadddepot.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim chkdepots As CheckBox = DirectCast(row.FindControl("chkdepots"), CheckBox)
If chkdepots.Checked Then
Dim lbldepot As TextBox = DirectCast(row.FindControl("lbldepot"), TextBox)
command.CommandText = "INSERT INTO Rhema_TechnicalAccessDepot VALUES(@userid2,@Depot,@Access,@TechnicalAccessID)"
command.Parameters.AddWithValue("@userid2", lblselecteduserid.Text)
command.Parameters.AddWithValue("@Depot", lbldepot.text)
command.Parameters.AddWithValue("@Access", lbluseraccessAdd.Text)
command.Parameters.AddWithValue("@technicalaccessid", lbltechnicalaccessid.Text)
End If
End If
Next
command.ExecuteNonQuery()
sqlTran.Commit()
lblmessage2.Text = "Both records were written to database."
Catch ex As Exception
lblmessage2.Text = ex.Message
Try
sqlTran.Rollback()
Catch exRollback As Exception
lblmessage2.Text = exRollback.Message
End Try
End Try
End Using
End Sub