Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
i am facing oledb exception error the source code is below
C#
 string query = "Insert into RealData(WantTo,PropertyType,Names,Owner,Area,
CateType,Address,Price,Desc)values('" + cmbWantTO.SelectedItem.ToString() + "',
'" + protery + "','" + txtName.Text + "','" + cmbType.SelectedItem.ToString() + "',
'" + cmbArea.SelectedItem.ToString() + "','" + cmbCategoeryType.SelectedItem.ToString() + "','" + address + "',
'" + txtPrice.Text + "','" + txtPDesc.Text + "')";
 cmd = new OleDbCommand(query, cnn);
int xy = cmd.ExecuteNonQuery();



this code is giving exception insert into statement syntax error plz help me.
Posted
Comments
David_Wimbley 13-Jan-13 3:00am    
What specifically is the error in your sql? Im going to submit a solution but for all i know every last column in your insert is not in your table

You have not given spaces appropriately to make correct query.
Issues:
RealData(WantTo,
Desc)values('

Try:
C#
string query = "INSERT INTO RealData (WantTo, PropertyType, Names, Owner, Area, CateType, Address, Price, Desc) VALUES ('" + cmbWantTO.SelectedItem.ToString() + "',
'" + protery + "','" + txtName.Text + "','" + cmbType.SelectedItem.ToString() + "',
'" + cmbArea.SelectedItem.ToString() + "','" + cmbCategoeryType.SelectedItem.ToString() + "','" + address + "',
'" + txtPrice.Text + "','" + txtPDesc.Text + "')";
 cmd = new OleDbCommand(query, cnn);
int xy = cmd.ExecuteNonQuery();


BTW, your implementation is open for SQL Injection, Not suggestible at all. You should use parametrized query.
Read about protecting from SQL Injection here: SQL Injection Mitigation: Using Parameterized Queries[^]
 
Share this answer
 
Comments
Dharmendra-18 13-Jan-13 2:55am    
still having same problem
Sandeep Mewara 13-Jan-13 3:29am    
1. Use parametrized query, it will resolve your issue of quotes at places.
2. While you DEBUG, copy the query formed and share here. Alternatively, you can paste the same in your SQL and see if you can find the issue and resolve. It's a simple looking query, you might have missed something some place.
For the love of your sanity and your coworkers who have to look at this, your killing maintainability by doing inline sql...at the bare minimum do string.format

I re-arranged your sql using string.format. Give it a shot i dont see why this wouldnt work unless your column names in the sql are not match what is in your table. In which case you didnt specify that in your question so who knows what the problem is at that point...it could be the aliens took your table.

C#
string query = string.Format(@"INSERT INTO RealData (WantTo,PropertyType,Names,Owner,Area,CateType,Address,Price,Desc) VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}')", cmbWantTO.SelectedItem.ToString(),
                                                                                                                                                                                         protery,
                                                                                                                                                                                         txtName.Text,
                                                                                                                                                                                         cmbType.SelectedItem.ToString(),
                                                                                                                                                                                         cmbArea.SelectedItem.ToString(),
                                                                                                                                                                                         cmbCategoeryType.SelectedItem.ToString(),
                                                                                                                                                                                         address,
                                                                                                                                                                                         txtPrice.Text,
                                                                                                                                                                                         txtPDesc.Text);
 
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