Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
C#
string str1="insert into Employee values("+txtempid.Text+",'"+txtEmpName.Text+"',"+txtsal.Text+",'"+txtdob.Text+"','"+txtdoj.Text+"',"+txtorderid.Text+ ")";
Posted
Updated 28-Jan-14 1:57am
v2
Comments
Gautam Raithatha 28-Jan-14 7:32am    
you should mention the error atleast, so proper help can be provided.
Parth Dotnet1 28-Jan-14 7:36am    
THE ERROR IS : Incorrect syntax near ')'.
Dinesh.V.Kumar 28-Jan-14 7:36am    
Query seems correct...it will be helpful if you can provide the error information...so that we can help you out...
Valeh_CP 28-Jan-14 7:40am    
Use try catch and look Exception error
EduChapow 28-Jan-14 7:46am    
see my solution man, i hope help u

Building a string by concatenating text boxes is a bad idea for a couple of reasons ...

Firstly, you are leaving yourself vulnerable to SQL Injection attacks - see http://bobby-tables.com/[^]

So use parameterised queries to avoid this.

Secondly, if you do use parameterised queries, you will gain certain other advantages - simpler query strings - which make it easier to spot any errors. For example you won't have to worry about the single quotes around string parameters. Your command string would become
C#
string str1="insert into Employee values (@empid,@EmpName,@sal,@dob,@doj,@orderid)"

Looking at the query now, I can tell that your problem was the result of the contents of one (or more) of your text boxes.

When creating the parameters from your text boxes do use the TryParse methods on int, string etc to ensure that the contents of the text boxes really do contain the type of data you expect.

Finally, in the debugger grab the final content of str1 and paste it into query analyser or similar ... you might get more detailed error reporting in context.

[edit - example of sql parameters] http://csharp-station.com/Tutorial/AdoDotNet/Lesson06[^]
 
Share this answer
 
v2
Hello man, is missing a columns and char '.

see this example:

SQL
INSERT INTO Employee (< columns1 >,< columns2 >, < column3 >) VALUES (< 'valueColumn1' >, < 'valueColumn2' >, < 'valueColumn3' >)


try this.

bye
 
Share this answer
 
Comments
Gautam Raithatha 28-Jan-14 7:56am    
column names can be omitted from insert query. the values clause will insert the values in columns starting from first in database table.
Parth Dotnet1 28-Jan-14 8:09am    
i am NOT MISSING ANY COLUMN , it would have given date type related error if date was the problem
EduChapow 28-Jan-14 8:10am    
hmmm, nice... living and learning! lol
EduChapow 28-Jan-14 8:13am    
so, first convert your string to datetime?
I think the error is datatype mismatch..
So, change your query :
string str1="insert into Employee values("+txtempid.Text+",'"+txtEmpName.Text+"',"+txtsal.Text+",'"+txtdob.Text+"','"+txtdoj.Text+"',"+txtorderid.Text+ ")";



To


string str1="insert into Employee values("+txtempid.Text+",'"+txtEmpName.Text+"',"+txtsal.Text+",#"+txtdob.Text+"#,#"+txtdoj.Text+"#,"+txtorderid.Text+ ")";


# sign represent the date value .
I hope it will help you....
Happy Coding .. :-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900