Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
hi
i am facing oledb exception error the source code is below
 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 12-Jan-13 20:38pm
Comments
Plyswthsqurles at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You have not given spaces appropriately to make correct query.
Issues:
RealData(WantTo,
Desc)values('
Try:
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[^]
  Permalink  
Comments
Dharmendra-18 at 13-Jan-13 2:55am
   
still having same problem
Sandeep Mewara at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
 
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);
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 840
1 OriginalGriff 437
2 Abhinav S 303
3 thatraja 215
4 Emre Ataseven 200
0 Sergey Alexandrovich Kryukov 8,122
1 OriginalGriff 4,520
2 Peter Leow 3,744
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 13 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid