Click here to Skip to main content
14,382,515 members
Rate this:
Please Sign up or sign in to vote.
See more:

I am having problem in insert query. The first query works fine, if I comment the insert query.
First query is adding balance and second query is making record of the deposit amount in trasaction table but I am getting an error in insert into statement.

Can anyone please help on why I am getting an error?
Here is the piece of code:
MAcmd.CommandText = "Update registeraccount Set Balance =Balance-'" + c + "'where username='" + textBox3.Text + "'";
MAcmd.CommandText = "insert into transaction (username, withdrawamount, date) VALUES ("+textBox3.Text+", '"+textBox4.Text+"','"+textBox1.Text+"');
Updated 1-Mar-11 9:30am
fjdiewornncalwe 1-Mar-11 14:13pm
What is the error you get? I'm guessing that your date value isn't parsing correctly from textBox1.Text, but unless you give us the error you are seeing, we're only guessing.
codegeekalpha 1-Mar-11 14:29pm
syntax error from insert into statement
Albin Abel 1-Mar-11 14:34pm
What error you are getting? The textbox3 i.e user name is not inside the quotes, the textbox 4 i.e withdrawn amount may not be a string data type, but you are passing in quotes. Not sure the date you are passing in correct format

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

A simple use of DEBUGGER would had told you that what was the final command text formed. All you needed was to look at that text and see if there is any issue. Just to confirm, copy it and paste the raw query formed in SQL server and see if it executes without error. This would tell you the issue with the query, if any.

Try (Looks like quotes were missing for username value):
MAcmd.CommandText = "INSERT INTO transaction (username, withdrawamount, date) VALUES ('"+textBox3.Text+"', '"+textBox4.Text+"','"+textBox1.Text+"')";

P.S.: This is assuming that the datatype for the fields being inserted is of varchar type. In case the database fields are different datatype, then you might need to format the values accordingly before inserting.

1. Always use parameterized query[^]. This would avoid confusion and syntax errors that can come into picture because of quotes. Further, it takes away SQL Injection issue.

2. Always use meaningful names of the control such that you can easily understand what value that control/object hold.

Put a quote just before semicolon too. Query does not need a semicolon at the end.
Albin Abel 1-Mar-11 14:58pm
The two suggestion given are good. my 5
codegeekalpha 1-Mar-11 17:57pm
i am not using sql database...
i am using Microsoft access. and only amount is in of type number all other types are in text including the date... i use textBox3.Text = DateTime.Now.ToShortDateString(); to save sys date on the textbox autmaticall and save it to db as text...
i almost spend 3 hour check all my data tyes .. but getting the same error ( Syntax error in insert into statement... hell
Sandeep Mewara 2-Mar-11 0:20am
Good to know that all is string type. Then the above statement should work. Try out. There was a semicolon too at the end which needs removal. I have updated the answer. Check out.

Further, if this does not work, use debugger and see the query formed and execute it directly (manually) on DB and see what error. I explained you everything on how to troubleshoot and resolve! :doh:

Lastly, it doesn't matter if its Access and not SQL. My last two suggestions still stand.
Look here for Parameterized query in Access:

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100