更新查询时出错... [英] Error in Update query...
问题描述
更新记录时收到错误。我正在使用参数化更新查询。下面是代码和错误。
----------------------------------- --------------------------
代码
------- -------------------------------------------------- ----
I am getting error when updating records. I am using parameterized update query. below is the code and error.
-------------------------------------------------------------
code
-------------------------------------------------------------
Private Sub btnupdate_Click(sender As Object, e As EventArgs) Handles btnupdate.Click
Try
con.Open()
ss = "UPDATE salary_master set empname=@empname,bsalary=@bsalary,hra=@hra,da=@da,pf=@pf,medamt=@medamt,allowance=@allowance," & _
"others=@others,desig=@desig,remarks=@remarks WHERE empid=@empid"""
com.Parameters.AddWithValue("@empid", txtempid.Text)
com.Parameters.AddWithValue("@empname", txtempname.Text)
com.Parameters.AddWithValue("@desig", txtdesig.Text)
com.Parameters.AddWithValue("@bsalary", txtbsalary.Text)
com.Parameters.AddWithValue("@hra", txthra.Text)
com.Parameters.AddWithValue("@da", txtda.Text)
com.Parameters.AddWithValue("@pf", txtpf.Text)
com.Parameters.AddWithValue("@medamt", txtmed.Text)
com.Parameters.AddWithValue("@allowance", txtallow.Text)
com.Parameters.AddWithValue("@others", txtother.Text)
com.Parameters.AddWithValue("@remarks", txtremarks.Text)
com.ExecuteNonQuery()
MsgBox("Record Updated Successfully !", MsgBoxStyle.Information)
Catch ex As Exception
MsgBox(ex.Message)
Finally
con.Close()
End Try
End Sub
----------------------------------------------- -------------------------
错误: -
------ -------------------------------------------------- ----------------
已经声明了变量名'@empid'。变量名在查询批处理或存储过程中必须是唯一的。
----------------------------- ---------------------------------------------
------------------------------------------------------------------------
error:-
------------------------------------------------------------------------
The variable name '@empid' has already been declared. Variable names must be unique within a query batch or stored procedure.
--------------------------------------------------------------------------
推荐答案
无法看到您的完整代码,但是您重复使用相同的命令会导致此问题。在添加之前尝试清除参数
can't see your full code but you are reuse same command will be causing this issue. try to clear the parameters before adding
com.CommandText = ss 'set the new command text
com.Parameters.Clear() 'clear the existing parameters
com.Parameters.AddWithValue("@empid", txtempid.Text)
com.Parameters.AddWithValue("@empname", txtempname.Text)
com.Parameters.AddWithValue("@desig", txtdesig.Text)
com.Parameters.AddWithValue("@bsalary", txtbsalary.Text)
com.Parameters.AddWithValue("@hra", txthra.Text)
com.Parameters.AddWithValue("@da", txtda.Text)
com.Parameters.AddWithValue("@pf", txtpf.Text)
com.Parameters.AddWithValue("@medamt", txtmed.Text)
com.Parameters.AddWithValue("@allowance", txtallow.Text)
com.Parameters.AddWithValue("@others", txtother.Text)
com.Parameters.AddWithValue("@remarks", txtremarks.Text)
com.ExecuteNonQuery()
这篇关于更新查询时出错...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!