Declare variable error

SuperCoder 196 Reputation points
2025-01-31T17:59:02.7966667+00:00

Receive this error when running the code below. Don't know how to resolve it.

User's image


    Command1.CommandText = "UPDATE names2 " &

                    "SET trn_date1 = @trn_date1, " &

                    "trn_date2 = @trn_date2, trn_date3 = @trn_date3, trn_date4 = @trn_date4, " &

                    "trn_date5 = @trn_date5, trn_date6 = @trn_date6, trn_date7 = @trn_date7, " &

                    "trn_date8 = @trn_date8, trn_date9 = @trn_date9, trn_date10 = @trn_date10, " &

                    "trn_date11 = @trn_date11, " &

                    "trn_date12 = @trn_date12, trn_date13 = @trn_date13, trn_date14 = @trn_date14, " &

                    "trn_date15 = @trn_date15, trn_date16 = @trn_date16, trn_date17 = @trn_date17, " &

                    "trn_date18 = @trn_date18, trn_date19 = @trn_date19, trn_date20 = @trn_date20, " &

                    "ntrn_date1 = @ntrn_date1, ntrn_date2 = @ntrn_date2, ntrn_date3 = @ntrn_date3," &

                    "ntrn_date4 = @ntrn_date4, ntrn_date5 = @ntrn_date5," &

                    "ntrn_date6 = @ntrn_date6, ntrn_date7 = @ntrn_date7, ntrn_date8 = @ntrn_date8, " &

                    "ntrn_date9 = @ntrn_date9, ntrn_date10 = @ntrn_date10, ntrn_date11 = @ntrn_date11," &

                    "ntrn_date12 = @ntrn_date12, ntrn_date13 = @ntrn_date13, " &

                    "ntrn_date14 = @ntrn_date14, ntrn_date15 = @ntrn_date15, ntrn_date16 = @ntrn_date16, " &

                    "ntrn_date17 = @ntrn_date17, ntrn_date18 = @ntrn_date18, ntrn_date19 = @ntrn_date19, " &

                    "ntrn_date20 = @ntrn_date20 " &

                    "WHERE account_no = " & Account_No

    ' Custom trainings

    Command1.Parameters.AddWithValue("@trn_date1", If(CObj(datTrn_Date1.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@trn_date2", If(CObj(datTrn_Date2.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@trn_date3", If(CObj(datTrn_Date3.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@trn_date4", If(CObj(datTrn_Date4.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@trn_date5", If(CObj(datTrn_Date5.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@trn_date6", If(CObj(datTrn_Date6.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@trn_date7", If(CObj(datTrn_Date7.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@trn_date8", If(CObj(datTrn_Date8.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@trn_date9", If(CObj(datTrn_Date9.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@trn_date10", If(CObj(datTrn_Date10.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@trn_date11", If(CObj(datTrn_Date11.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@trn_date12", If(CObj(datTrn_Date12.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@trn_date13", If(CObj(datTrn_Date13.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@trn_date14", If(CObj(datTrn_Date14.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@trn_date15", If(CObj(datTrn_Date15.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@trn_date16", If(CObj(datTrn_Date16.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@trn_date17", If(CObj(datTrn_Date17.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@trn_date18", If(CObj(datTrn_Date18.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@trn_date19", If(CObj(datTrn_Date19.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@trn_date20", If(CObj(datTrn_Date20.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@ntrn_date1", If(CObj(datNTrn_Date1.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@ntrn_date2", If(CObj(datNTrn_Date2.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@ntrn_date3", If(CObj(datNTrn_Date3.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@ntrn_date4", If(CObj(datNTrn_Date4.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@ntrn_date5", If(CObj(datNTrn_Date5.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@ntrn_date6", If(CObj(datNTrn_Date6.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@ntrn_date7", If(CObj(datNTrn_Date7.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@ntrn_date8", If(CObj(datNTrn_Date8.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@ntrn_date9", If(CObj(datNTrn_Date9.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@ntrn_date10", If(CObj(datNTrn_Date10.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@ntrn_date11", If(CObj(datNTrn_Date11.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@ntrn_date12", If(CObj(datNTrn_Date12.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@ntrn_date13", If(CObj(datNTrn_Date13.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@ntrn_date14", If(CObj(datNTrn_Date14.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@ntrn_date15", If(CObj(datNTrn_Date15.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@ntrn_date16", If(CObj(datNTrn_Date16.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@ntrn_date17", If(CObj(datNTrn_Date17.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@ntrn_date18", If(CObj(datNTrn_Date18.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@ntrn_date19", If(CObj(datNTrn_Date19.EditValue), DBNull.Value))

    Command1.Parameters.AddWithValue("@ntrn_date20", If(CObj(datNTrn_Date20.EditValue), DBNull.Value))
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,400 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,775 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jonathan Pereira Castillo 12,805 Reputation points Microsoft Vendor
    2025-01-31T19:41:23.4866667+00:00

    Thank you SuperCoder

    Let's dive into your issue with the code.

    The error you're encountering when running the provided code could be due to several reasons. Here are some common issues and solutions:

    Parameter Declaration: Ensure that all parameters used in the CommandText are properly declared and added to the Command1.Parameters collection. In your code, you have added parameters for @trn_date1 to @trn_date20 and @ntrn_date1 to @ntrn_date20, but make sure they match exactly with the placeholders in the SQL command.

    Null Values Handling: The use of If(CObj(datTrn_Date1.EditValue), DBNull.Value) is correct for handling null values. However, ensure that datTrn_Date1.EditValue and similar variables are properly initialized and not causing any runtime errors.

    SQL Command Syntax: Verify that the SQL command syntax is correct. Ensure there are no missing commas, spaces, or other syntax errors in the CommandText.

    Account_No Variable: Ensure that the Account_No variable is properly defined and holds a valid value. If Account_No is not properly initialized, it could cause an error.

    Database Connection: Ensure that the database connection is open and valid before executing the command. If the connection is closed or invalid, it will result in an error.

    Here is a revised version of your code with some additional checks:

    'Ensure the database connection is open
    If Command1.Connection.State = ConnectionState.Closed Then
        Command1.Connection.Open()
    End If
    'Define the SQL command
    Command1.CommandText = "UPDATE names2 SET " &
        "trn_date1 = @trn_date1, trn_date2 = @trn_date2, trn_date3 = @trn_date3, trn_date4 = @trn_date4, " &
        "trn_date5 = @trn_date5, trn_date6 = @trn_date6, trn_date7 = @trn_date7, trn_date8 = @trn_date8, " &
        "trn_date9 = @trn_date9, trn_date10 = @trn_date10, trn_date11 = @trn_date11, trn_date12 = @trn_date12, " &
        "trn_date13 = @trn_date13, trn_date14 = @trn_date14, trn_date15 = @trn_date15, trn_date16 = @trn_date16, " &
        "trn_date17 = @trn_date17, trn_date18 = @trn_date18, trn_date19 = @trn_date19, trn_date20 = @trn_date20, " &
        "ntrn_date1 = @ntrn_date1, ntrn_date2 = @ntrn_date2, ntrn_date3 = @ntrn_date3, ntrn_date4 = @ntrn_date4, " &
        "ntrn_date5 = @ntrn_date5, ntrn_date6 = @ntrn_date6, ntrn_date7 = @ntrn_date7, ntrn_date8 = @ntrn_date8, " &
        "ntrn_date9 = @ntrn_date9, ntrn_date10 = @ntrn_date10, ntrn_date11 = @ntrn_date11, ntrn_date12 = @ntrn_date12, " &
        "ntrn_date13 = @ntrn_date13, ntrn_date14 = @ntrn_date14, ntrn_date15 = @ntrn_date15, ntrn_date16 = @ntrn_date16, " &
        "ntrn_date17 = @ntrn_date17, ntrn_date18 = @ntrn_date18, ntrn_date19 = @ntrn_date19, ntrn_date20 = @ntrn_date20 " &
        "WHERE account_no = @Account_No"
    'Add parameters
    Command1.Parameters.AddWithValue("@trn_date1", If(CObj(datTrn_Date1.EditValue), DBNull.Value))
    'Repeat for all other parameters...
    'Add Account_No parameter
    Command1.Parameters.AddWithValue("@Account_No", Account_No)
    'Execute the command
    Command1.ExecuteNonQuery()
    

    If you still encounter issues, please provide more details about the specific error message you're receiving. This will help in diagnosing the problem more accurately.

    Feel free to ask if you have any more questions or need further assistance!

    Joanthan

    0 comments No comments

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.