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 1037427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:51:03+00:00 2026-05-16T14:51:03+00:00

I am using C#, VS 2005 and SQL 2000 My code is: StringBuilder sb

  • 0

I am using C#, VS 2005 and SQL 2000

My code is:

StringBuilder sb = new StringBuilder();
sb.Append("insert into dummy(name,amount)values");
foreach (Control ctl in this.flowLayoutPanel1.Controls) 
{
  if (ctl.Name.Contains("tb") && ctl is TextBox) 
  {
     sb.Append(ctl.Text);
  }
} 

foreach(Control bbl in this.flowLayoutPanel1.Controls)
{
   if(bbl.Name.Contains("bb") && bbl is TextBox)
   {
     sb.Append(bbl.Text);
   }
}

SqlCommand cmd = new SqlCommand(sb.ToString(), con);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();

It throws an error like this:

Incorrect syntax near values

Please help me.

  • 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-05-16T14:51:04+00:00Added an answer on May 16, 2026 at 2:51 pm

    The values themselves need to be in brackets and separated by commas as well.

    I think your code produces this:

    insert into dummy(name,amount)valuesthisname100
    

    But you need to change it to produce this:

    INSERT INTO dummy (name, amount) VALUES ('thisname', 100)
    

    some example code that will do this is:

    StringBuilder sb = null;
    sb = new StringBuilder();
    sb.Append("insert into dummy(name,amount)values (");
    foreach (Control ctl in this.flowLayoutPanel1.Controls) 
    {
       if (ctl.Name.Contains("tb") && ctl is TextBox) 
       {
          sb.Append("'" + ctl.Text + "'");
       }
    } 
    sb.Append(", ");
    foreach(Control bbl in this.flowLayoutPanel1.Controls)
    {
       if(bbl.Name.Contains("bb") && bbl is TextBox)
       {
           sb.Append(bbl.Text);
       }
    }
    sb.Append(")");
    SqlCommand cmd1 = new SqlCommand(sb.ToString(), con);
    cmd1.CommandType = CommandType.Text;
    cmd1.ExecuteNonQuery();
    

    This code is far from ideal, but it should fix your SQL syntax error. Some other enhancements you should think about are:

    • Make sure only one text box is ever found in each of the foreach loops. If more than one then the field count won’t match.
    • Put validation or fix-up code in to ensure that no single quote characters appear in text thats entered by the user, or change the SQL to use parameters (thanks Jon Skeet).
    • Put validation to ensure that your second text box is parseable as a number (see Int.TryParse()), assuming that your Amount field is a numeric.

    However, a MUCH better way would be to do this (EDITED to help mahesh with his coding, now includes multiple inserts):

    string sName = null;
    double? nAmount = null;
    
    foreach (Control ctl in this.flowLayoutPanel1.Controls) 
    {
       if (ctl.Name.Contains("tb") && ctl is TextBox) sName = ctl.Text;
       if (ctl.Name.Contains("bb") && ctl is TextBox) 
       {
           double nTmp = 0;
           if (double.TryParse(ctl.Text, out nTmp)) nAmount = nTmp;
       }
    
       if (sName != null && iAmount != null) 
       {
          SqlCommand cmd1 = new SqlCommand("INSERT INTO dummy (name, amount) VALUES (@name, @amount)", con);
          cmd1.Parameters.Add("@name", SqlDbType.VarChar).Value = sName;
          cmd1.Parameters.Add("@amount", SqlDbType.Decimal).Value = nAmount;
          cmd1.ExecuteNonQuery();
          sName = null;
          nAmount = null;
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 528k
  • Answers 528k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is the shortest: echo '<pre>',print_r($arr,1),'</pre>'; The closing tag can… May 16, 2026 at 11:03 pm
  • Editorial Team
    Editorial Team added an answer Many UNIXes now have pgrep which does exactly what you… May 16, 2026 at 11:03 pm
  • Editorial Team
    Editorial Team added an answer Here is a detailed tutorial on how to achieve that:… May 16, 2026 at 11:03 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Using SQL 2000, SQL 2005 Old Database Name is – Sysdatabase New Database Name
Occasionally I run into this limitation using SQL Server 2000 that a row size
am using C#, VS 2005 and SQL 2000 I have date conversion problem my
I am using C#, Visual Studio 2005, and SQL Server 2000. My problem is
if i try to run following code in sql server 2005 i get error
Sproc in SQL Server 2005 parses, compiles and runs successfully via SQL Management Studio.
I know you can execute a Job in SQL Server 2005/2008 by sending a
We've just separated our SQL Server database adapters to allow for differences between SQL
Following on from my last question : I've written some code to upgrade a
I want to know what are pros and cons while using varchar(500) vs varchar(max)

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.