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 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

Related Questions

Using SQL 2000, SQL 2005 Old Database Name is – Sysdatabase New Database Name
Using Sql Server 2005 Table1 ID Name Value 001 Rajesh 90 002 Suresh 100
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
Are there good efficiency savings using Sql Server 2005 over Sql Server 2000? Or
Using SQL 2005: Taking too much time to execute I want to filter the
I've got a query that returns a proper result set, using SQL 2005. It
I'm using MS SQL 2005 and I have created a CTE query to return
The excel spreadsheet is connecting to SQL server 2005 using the connection string below:
So I'm trying to update an object in my MS SQL 2005 database using

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.