Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9070479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:41:47+00:00 2026-06-16T17:41:47+00:00

Possible Duplicate: The variable name ‘@VarName’ has already been declared Issue using (SqlCommand command

  • 0

Possible Duplicate:
The variable name ‘@VarName’ has already been declared Issue

using (SqlCommand command = connection.CreateCommand())
{
    int j = 1;
    for (int i = 0; i < temp4.Count; i++)
    {
        #region idsetter

        int prevId = 0;

        command.CommandText = "SELECT MAX(id) FROM Configuration";
        connection.Open();
        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                if (reader[0].ToString() != "")
                {
                    prevId = Int32.Parse(reader[0].ToString());
                }
                else
                {
                    prevId = 0;
                }
            }
        }

        connection.Close();
        int currId = prevId + 1;
        #endregion
        command.CommandText = "INSERT INTO Configuration (id,A, B, C, D, E, F,G) " + 
                              "VALUES (@id, @a, @b , @c, @d, @e, @f, @g)";

        command.Parameters.AddWithValue("@id", currId);
        command.Parameters.AddWithValue("@a", myL);
        command.Parameters.AddWithValue("@b", pipi);
        command.Parameters.AddWithValue("@c", temp4[i].ad);
        command.Parameters.AddWithValue("@d", temp4[i].asa);
        command.Parameters.AddWithValue("@e", temp4[i].Tasa);
        command.Parameters.AddWithValue("@f", j);
        command.Parameters.AddWithValue("@g", 1);
        connection.Open();
        command.ExecuteNonQuery();
        connection.Close();
        j++;
        if (j > 7)
        {
            j = 1;
        }
    }
}

I am trying to write in Configuration Table first reading id and increment it and then writing everything else. But when second entry is being added it gives exception

The variable name “@id” has already been declared. Variable names
must be unique within a query batch or stored procedure.

How to avoid this exception any work around ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:41:48+00:00Added an answer on June 16, 2026 at 5:41 pm

    You need to clear your Command parameters in every iteration of your loop.

    command.CommandText = "INSERT INTO Configuration (id,A, B, C, D, E, F,G) VALUES (@id, @a, @b , @c, @d, @e, @f, @g)";
    
    command.Parameters.Clear();
    command.Parameters.AddWithValue("@id", currId);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why can't a duplicate variable name be declared in a nested local
Possible Duplicate: Get variable name. javascript “reflection” Is there a way to know the
Possible Duplicate: Finding the Variable Name passed to a Function in C# In C#,
Possible Duplicate: How to get a variable name as a string in PHP? Example:
Possible Duplicate: javascript object, access variable property name? I'm trying to call a method
Possible Duplicate: Determine original name of variable after its passed to a function. I
Possible Duplicate: Max name length of variable or method in Java I was reading
Possible Duplicate: How to concatenate a number to a variable name in MATLAB? Hi
Possible Duplicate: R: How to convert string to variable name? In R, I'm writing
Possible Duplicate: Finding the Variable Name passed to a Function in C# public new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.