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

  • Home
  • SEARCH
  • 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 8978063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:31:09+00:00 2026-06-15T19:31:09+00:00

I am inserting multiple items into table based on a selection from drop down

  • 0

I am inserting multiple items into table based on a selection from drop down list. When I select one item from the drop down then everything works fine but when I select multiple items then I get this error

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

what am i doing wrong? thanks
here is my code

protected void DV_Test_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        foreach (ListItem listItem in cblCustomerList.Items)
        {
            if (listItem.Selected)
            {
                string Name= listItem.Value;
                sqlcon.Open();
                string CompName= ((TextBox)DV_Test.FindControl("txtCompName")).Text.ToString();
                string Num = ((TextBox)DV_Test.FindControl("txtNum")).Text.ToString();
                SqlCommand cmd = new SqlCommand("select CompNamefrom MyTable where CompName= '" + CompName+ "' and Num = '" + Num + "' and Name= '" + Name+ "'    ", sqlcon);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    lblmsg.Text = "Not Valid";
                }
                else
                {
                    dr.Close();

                    sqlcmd.CommandText = "INSERT INTO MyTable(CompName, Num, Name) VALUES(@CompName, @Num, @Name)";
                    sqlcmd.Parameters.Add("@CompName", SqlDbType.VarChar).Value = CompName;
                    sqlcmd.Parameters.Add("@Num", SqlDbType.VarChar).Value = Num;
                    sqlcmd.Connection = sqlcon;                 
                    sqlcmd.ExecuteNonQuery();
                    DV_Test.ChangeMode(DetailsViewMode.Insert);
                    sqlcon.Close();
                }
                sqlcon.Close();               
            }

         }
 }
  • 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-15T19:31:10+00:00Added an answer on June 15, 2026 at 7:31 pm

    You are adding the paramters @CompName and @Num to your query every time through the loop.

    You need to move the call to Add outside the loop and use the following to update them:

    sqlcmd.Parameters["@CompName"].Value = CompName;
    sqlcmd.Parameters["@Num"].Value = Num;
    

    So your code becomes:

    sqlcmd.CommandText = "INSERT INTO MyTable(CompName, Num, Name) VALUES(@CompName, @Num, @Name)";
    sqlcmd.Parameters.Add("@CompName", SqlDbType.VarChar);
    sqlcmd.Parameters.Add("@Num", SqlDbType.VarChar);
    
    foreach (ListItem listItem in cblCustomerList.Items)
    {
        if (....)
        {
            ....
        }
        else
        {
            dr.Close();
    
            sqlcmd.Parameters["@CompName"].Value = CompName;
            sqlcmd.Parameters["@Num"].Value = Num;
            sqlcmd.Connection = sqlcon;                 
            sqlcmd.ExecuteNonQuery();
            DV_Test.ChangeMode(DetailsViewMode.Insert);
            sqlcon.Close();
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi i am having problem inserting data into multiple table from one view. First
I'm inserting multiple records into a table A from another table B. Is there
if i run command from Pl-sql command prompt for inserting multiple rows one by
I want to insert multiple email into SQL Server table cell and before inserting
i need some help with inserting multiple rows from different arrays into my database.
In a single operation, I am inserting multiple rows into a table. It is
i am dynamically creating sql query for inserting multiple records in one hit.Suppose no
Inserting rows one by one from a 600 record data-set is taking almost an
When inserting copy into an HTML document I get from sources such as word
When inserting a row into a table with an auto increment primary key column,

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.