Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting error when updating records. I am using parameterized update query. below is the code and error.
-------------------------------------------------------------
code
-------------------------------------------------------------
VB
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

------------------------------------------------------------------------
error:-
------------------------------------------------------------------------
The variable name '@empid' has already been declared. Variable names must be unique within a query batch or stored procedure.
--------------------------------------------------------------------------
Posted
Comments
[no name] 8-Mar-15 0:04am    
empid is primary key? then y are u update that value in ur parameter? remove the com.Parameters.AddWithValue("@empid", txtempid.Text) and check?

1 solution

can't see your full code but you are reuse same command will be causing this issue. try to clear the parameters before adding
VB
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()
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900