473,411 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

SQL Insert Error Through Runtime Control In FLP

maheshwag
am using C#, VS-2005, and SQL-2000

am trying to insert into sql database via following.

Expand|Select|Wrap|Line Numbers
  1.  
  2. StringBuilder sb = null;
  3. sb = new StringBuilder();
  4. sb.Append("insert into dummy(name,amount)values");
  5.  
  6. foreach (Control ctl in this.flowLayoutPanel1.Controls) 
  7. {
  8.    if (ctl.Name.Contains("tb") && ctl is TextBox) 
  9.    {
  10.       sb.Append(ctl.Text);
  11.    }
  12. }
  13.  
  14. foreach(Control bbl in this.flowLayoutPanel1.Controls)
  15. {
  16.    if(bbl.Name.Contains("bb") && bbl is TextBox)
  17.    {
  18.      sb.Append(bbl.Text);
  19.    }
  20. }
  21.  
  22.   SqlCommand cmd1 = new SqlCommand(sb.ToString(), con);
  23.   cmd1.CommandType = CommandType.Text;
  24.   cmd1.ExecuteNonQuery();
  25.  
but it's gives "incorrect syntex error near values"

I don't know what's wrong with the code please help me.
Sep 2 '10 #1

✓ answered by MartijnHoekstra

Try something like this:

Expand|Select|Wrap|Line Numbers
  1. sb.Append("insert into dummy(name,amount)values"); 
  2.  
  3. bool first=true;
  4. string values = "";
  5. foreach (Control ctl in this.flowLayoutPanel1.Controls)  
  6.    if (ctl.Name.Contains("tb") && ctl is TextBox) 
  7.    {
  8.       if (!first) sb.Append(","); first=false;
  9.       values = "('" + ctl.Text + "'";
  10.    }
  11.    if(ctl.Name.Contains("bb") && ctl is TextBox) 
  12.    {
  13.       values += ",'" + ctl.Text + "')";
  14.       sb.Append(values); 
  15.    } 
This will most likely NOT work, but the SQL syntax would be correct.
Since I am not sure what refers to 'Name' or 'Amount', you need to make the adjustments properly.

6 1615
syntax should be:

insert into dummy (name,amount) values ( 'text','amount' );

meaning you are missing the bold parts: as in: ( ' ' , ' ' )
Sep 2 '10 #2
sir, how can i do it in foreachloop when my controls like two textboxes as above code in FLP and they are increment may be it's two or three records so how can i perform it. please guide me proper.

thx for ur feedback.
Sep 2 '10 #3
uncertain to me is what you exactly want, and what the controls exactly refer to.

You want to add a 'Name' and 'Amount' - pair in database.
Am I correct that the controls containing 'tb' contain names, and 'bb' contain amounts ?
Sep 2 '10 #4
as per my above code how to i modify it as per ur suggestion i don't know how to it with foreach loop where as am not familiar of stringbuilder so how to i quot it with it. how to append sql query in correct format with stingbuilder that's what i am demanding.
Sep 2 '10 #5
Try something like this:

Expand|Select|Wrap|Line Numbers
  1. sb.Append("insert into dummy(name,amount)values"); 
  2.  
  3. bool first=true;
  4. string values = "";
  5. foreach (Control ctl in this.flowLayoutPanel1.Controls)  
  6.    if (ctl.Name.Contains("tb") && ctl is TextBox) 
  7.    {
  8.       if (!first) sb.Append(","); first=false;
  9.       values = "('" + ctl.Text + "'";
  10.    }
  11.    if(ctl.Name.Contains("bb") && ctl is TextBox) 
  12.    {
  13.       values += ",'" + ctl.Text + "')";
  14.       sb.Append(values); 
  15.    } 
This will most likely NOT work, but the SQL syntax would be correct.
Since I am not sure what refers to 'Name' or 'Amount', you need to make the adjustments properly.
Sep 3 '10 #6
very true sir here am accept ur answer. it's my mistake that i not tell u in details. exactly sir the name tb and bb in control collection is textbox u know. but it's increment by user it may two or three whatever it's upto user. in short it's in multiple that's why am demanding this way. Anyway i have done it in parameterized way. once again thx.
Sep 3 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: owen | last post by:
Generally speaking, what does it mean when I see a "button" with red text showing this message instead of the control I've dragged onto the web form in Design View.? (But the page works fine at...
0
by: Ravi Ambros Wallau | last post by:
Hi: I've created a custom control - a grid that uses Infragistics to display some filters, the grid itself, and some buttons. Well, when using this control directly on WebForm, everything works...
1
by: INSERT ERROR | last post by:
I am trying to INSERT record in MYSQL database, but each time the code tries to execute the command: MySQL_cmdGLAccount.ExecuteNonQuery(), it reports an error with the message "COLUMN COUNT...
3
by: INSERT ERROR | last post by:
I am trying to INSERT record in MYSQL database, but each time the code tries to execute the command: MySQL_cmdGLAccount.ExecuteNonQuery(), it reports an error with the message "COLUMN COUNT...
8
by: erin.sebastian | last post by:
Hi all, I have a really silly problem that i can't find the answer too. I am working with VB.NET and i am trying to insert a new record into my access database (pretty easy right?) well i am...
3
by: mpg | last post by:
using vb express 2005 trying to insert a spreadsheet control onto a form. i have added the reference for excel object 11.0 i cant figure out where to FIND the object to paste the control on my...
4
by: AG | last post by:
Using VS2005. When I add a server control to a page in design view, it displays the following; Error Rendering Control = <control name> An unhandled exception has occurred. 'HelpVerb' could...
3
by: Joseph Gruber | last post by:
Hi all -- I have two questions. First, I'm adding a control to my form at runtime and the control/form seems to ignore the anchor property of the runtime control. Any idea how I can get a...
3
by: Greg Corradini | last post by:
Hello, I'm trying to perform a simple insert statement into a table called Parcel_Test (see code below). Yet, I get an error message that I've never seen before (see traceback below). I've tried...
2
by: Greg Corradini | last post by:
Hello, I've never gotten this traceback error before using mx.ODBC. Any ideas about resolving this issue? The statement and the error it generates are listed below. curse.execute("Insert into...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.