Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

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:
C#
MAcmd.CommandText = "Update registeraccount Set Balance =Balance-'" + c + "'where username='" + textBox3.Text + "'";
MAcmd.ExecuteNonQuery();
MAcmd.CommandText = "insert into transaction (username, withdrawamount, date) VALUES ("+textBox3.Text+", '"+textBox4.Text+"','"+textBox1.Text+"');
MAcmd.ExecuteNonQuery();
Posted
Updated 1-Mar-11 8:30am
v2
Comments
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

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):
C#
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.

Now,
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.

UPDATE 1:
Put a quote just before semicolon too. Query does not need a semicolon at the end.
 
Share this answer
 
v3
Comments
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: http://www.google.co.in/search?q=parameterized+query+access&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-GB:official&client=firefox-a

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