none
ERROR The variable name '@' has already been declared RRS feed

  • Question

  • Hi,

    Please help me with this. 

    System.Data.SqlClient.SqlException: 'The variable name '@MBid' has already been declared. Variable names must be unique within a query batch or stored procedure.'

    Public Function SYSSerial() As Boolean
            If K9ConfigConn.State = ConnectionState.Closed Then K9ConfigConn.Open()
            DR.Close()
    
            Dim PCMBid As String = K9Core.GetSysSerialNo
            Dim PCCPUid As String = K9Core.GetCPUID
    
            CMD.Connection = K9ConfigConn
            CMD.CommandType = CommandType.Text
            With CMD
                .Parameters.AddWithValue("@MBid", PCMBid)
                .Parameters.AddWithValue("@CPUid", PCCPUid)
            End With
            CMD.CommandText = "SELECT * FROM k9info WHERE mbid=@MBid AND cpuid=@CPUid"
    
            DR = CMD.ExecuteReader()
    
            If DR.Read = True Then
                Return True
                wasSuccessful = True
            ElseIf DR.Read = False Then
                Return False
                wasSuccessful = False
            End If
    
            CMD.Dispose()
            K9ConfigConn.Close()
    
            If wasSuccessful = True Then Return True Else Return False
        End Function


    Thank you....


    RohanD

    Wednesday, July 24, 2019 11:42 AM

Answers

  • Since CMD is defined outside the method on the second execute of the method (SYSSerial) there are already parameters defined.  When you execute the second time you are adding the parameters again so you will get the error.

    Lloyd Sheen

    • Marked as answer by RAT_SL Wednesday, July 24, 2019 8:00 PM
    Wednesday, July 24, 2019 12:16 PM
  • Hello,

    Either clear the parameters or use cmd.Parameters.Add rather than AddWithValue, using Add you define the parameter then assign the value as many times as needed.  

    https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.parameters?view=netframework-4.8


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by RAT_SL Wednesday, July 24, 2019 8:01 PM
    Wednesday, July 24, 2019 12:39 PM
    Moderator
  • Thank you so much...!!!

    RohanD

    • Marked as answer by RAT_SL Wednesday, July 24, 2019 8:00 PM
    Wednesday, July 24, 2019 8:00 PM

All replies

  • Since CMD is defined outside the method on the second execute of the method (SYSSerial) there are already parameters defined.  When you execute the second time you are adding the parameters again so you will get the error.

    Lloyd Sheen

    • Marked as answer by RAT_SL Wednesday, July 24, 2019 8:00 PM
    Wednesday, July 24, 2019 12:16 PM
  • Hello,

    Either clear the parameters or use cmd.Parameters.Add rather than AddWithValue, using Add you define the parameter then assign the value as many times as needed.  

    https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.parameters?view=netframework-4.8


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by RAT_SL Wednesday, July 24, 2019 8:01 PM
    Wednesday, July 24, 2019 12:39 PM
    Moderator
  • Thank you so much...!!!

    RohanD

    • Marked as answer by RAT_SL Wednesday, July 24, 2019 8:00 PM
    Wednesday, July 24, 2019 8:00 PM
  • Thank you so much...!!!

    RohanD

    Wednesday, July 24, 2019 8:00 PM