Must declare the scalar variable "@n"

SuperCoder 236 Reputation points
2025-01-07T16:18:18.84+00:00

Receive the above error generated by the code below.

 Dim objCommand As SqlCommand = New SqlCommand
 Dim Connection1 As New SqlConnection(ConnectionString)

 objCommand.CommandText = "UPDATE names " &
     "SET n.cc_date = @n.cc_date, n.cpr_date = @n.cpr_date, n.fa_date = @n.fa_date " &
     "FROM names n JOIN names2 n2 " &
     "WHERE account_no = " & strAccount_No

 objCommand.Parameters.AddWithValue("@n.cc_date", datCC_Date.EditValue)
 objCommand.Parameters.AddWithValue("@n.cpr_date", datCPR_Date.EditValue)
 objCommand.Parameters.AddWithValue("@n.fa_date", datFA_Date.EditValue)

 'Open Connection
 If Connection1.State = ConnectionState.Closed Then Connection1.Open()
 
Try
     objCommand.ExecuteNonQuery()
 Catch SqlExceptionErr As SqlException
     MessageBox.Show(SqlExceptionErr.Message)
 End Try

 'Close connection
 If Connection1.State = ConnectionState.Open Then Connection1.Close()

Developer technologies .NET Other
Developer technologies VB
SQL Server Other
{count} votes

Accepted answer
  1. Naomi Nosonovsky 8,431 Reputation points
    2025-01-07T16:23:11.9+00:00

    You cannot have . inside the identifier (see rules for SQL variables). You need to rename your parameters to remove . from their names.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.