Must declare the scalar variable "@n"

SuperCoder 176 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()

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,066 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,369 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,768 questions
{count} votes

Accepted answer
  1. Naomi Nosonovsky 8,126 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.