none
Error: Must declare the scalar variable - VB.Net/SQL Server Express 2005 RRS feed

  • Question

  • Hi,

    I've been through all discussions with the above title but I couldn't get the solution. The code and error are as follows.

    Using con As SqlConnection = Utils.GetConnection
                    con.Open()
                    Using cmd As SqlCommand = con.CreateCommand
                        Dim sql As String = "INSERT INTO Newspapers(Id, OfficeId, Name, Address1, Address2, City, State, Pincode, Label, Phone, Fax, Mobile, Email, CR) " & _
                                            "VALUES(@uId, @OfficeId, @Name, @Address1, @Address2, @City, @State, @Pincode, @Label, @Phone, @Fax, @Mobile, @Email, @CR)"
    
                        cmd.CommandType = CommandType.Text
                        cmd.CommandText = sql
    
                        Dim dpId As SqlParameter = cmd.CreateParameter
                        dpId.ParameterName = "@uId"
                        dpId.Value = e.Id
                        cmd.Parameters.Add(dpId)
    
                        Dim dpName As SqlParameter = cmd.CreateParameter
                        dpId.ParameterName = "@Name"
                        dpId.Value = e.Name
                        cmd.Parameters.Add(dpName)
    
                        Dim dpOfficeId As SqlParameter = cmd.CreateParameter
                        dpOfficeId.ParameterName = "@OfficeId"
                        dpOfficeId.Value = e.Office.Id
                        cmd.Parameters.Add(dpOfficeId)
    
                        Dim dpAddress1 As SqlParameter = cmd.CreateParameter
                        dpAddress1.ParameterName = "@Address1"
                        dpAddress1.Value = e.Address1
                        cmd.Parameters.Add(dpAddress1)
    
                        Dim dpAddress2 As SqlParameter = cmd.CreateParameter
                        dpAddress2.ParameterName = "@Address2"
                        dpAddress2.Value = e.Address2
                        cmd.Parameters.Add(dpAddress2)
    
                        Dim dpCity As SqlParameter = cmd.CreateParameter
                        dpCity.ParameterName = "@City"
                        dpCity.Value = e.City
                        cmd.Parameters.Add(dpCity)
    
                        Dim dpState As SqlParameter = cmd.CreateParameter
                        dpState.ParameterName = "@State"
                        dpState.Value = e.State
                        cmd.Parameters.Add(dpState)
    
                        Dim dpPincode As SqlParameter = cmd.CreateParameter
                        dpPincode.ParameterName = "@Pincode"
                        dpPincode.Value = e.PinCode
                        cmd.Parameters.Add(dpPincode)
    
    
                        Dim dpLabel As SqlParameter = cmd.CreateParameter
                        dpLabel.ParameterName = "@Label"
                        dpLabel.Value = e.Name
                        cmd.Parameters.Add(dpLabel)
    
                        Dim dpPhone As SqlParameter = cmd.CreateParameter
                        dpPhone.ParameterName = "@Phone"
                        dpPhone.Value = e.Phone
                        cmd.Parameters.Add(dpPhone)
    
                        Dim dpFax As SqlParameter = cmd.CreateParameter
                        dpFax.ParameterName = "@Fax"
                        dpFax.Value = e.Fax
                        cmd.Parameters.Add(dpFax)
    
                        Dim dpMobile As SqlParameter = cmd.CreateParameter
                        dpMobile.ParameterName = "@Mobile"
                        dpMobile.Value = e.Mobile
                        cmd.Parameters.Add(dpMobile)
    
                        Dim dpEmail As SqlParameter = cmd.CreateParameter
                        dpEmail.ParameterName = "@Email"
                        dpEmail.Value = e.Email
                        cmd.Parameters.Add(dpEmail)
    
    
                        Dim dpCommissionRate As SqlParameter = cmd.CreateParameter
                        dpCommissionRate.ParameterName = "@CR"
                        dpCommissionRate.Value = e.CommissionRate
                        cmd.Parameters.Add(dpCommissionRate)
    
                        cmd.ExecuteNonQuery()
                    End Using
                    con.Close()
                End Using
    and the error is "Must declare the scalar variable @uId"


    I just don't seem to understand. I've added all the parameters to the command but it's still showing the same problem.
    Friday, March 5, 2010 3:14 AM

Answers

  • Sorry to disturb you all, but the issue has been resolved.  The mistake was on my part. I had mistyped in the second Parameter dpName as dpID which was causing the previous dpID to be overwritten with "@Name" and the corresponding value.

    Here see for your self.

                        Dim dpName As SqlParameter = cmd.CreateParameter
    >                   dpId.ParameterName = "Name"
    >                   dpId.Value = e.Name
                        cmd.Parameters.Add(dpName)

    Correction was very simple

                        Dim dpName As SqlParameter = cmd.CreateParameter
                        dpName.ParameterName = "Name"
                        dpName.Value = e.Name
                        cmd.Parameters.Add(dpName)


    Sorry once again.
    Friday, March 5, 2010 4:30 AM

All replies

  • Sorry to disturb you all, but the issue has been resolved.  The mistake was on my part. I had mistyped in the second Parameter dpName as dpID which was causing the previous dpID to be overwritten with "@Name" and the corresponding value.

    Here see for your self.

                        Dim dpName As SqlParameter = cmd.CreateParameter
    >                   dpId.ParameterName = "Name"
    >                   dpId.Value = e.Name
                        cmd.Parameters.Add(dpName)

    Correction was very simple

                        Dim dpName As SqlParameter = cmd.CreateParameter
                        dpName.ParameterName = "Name"
                        dpName.Value = e.Name
                        cmd.Parameters.Add(dpName)


    Sorry once again.
    Friday, March 5, 2010 4:30 AM
  • Ditto!

    Thanks for posting answer.

    Friday, May 3, 2019 3:21 AM