Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hello Team,

I am getting error "Incorrect syntax near ')'.".How to fix the issue.

VB
string insertQuery = string.Format(
                   "Insert into CUSTOMERLOANDATA(Firstname,MiddleName,LastName,FullAddress,MobileNo1,EmailID,DOB,Loan1IntrRate,Loan1Amt,Loan2IntrRate,Loan2Amt,DateOfEMI,ReleaseDate,HouseType,Salary,CustomerLoanOCRId,CustomerAddr1,CustomerAddr2,Landmark,State,City,Pincode) values('{0}','{1}','{2}','{3}',{4},'{5}',{6},{7},{8},{9},{10},{11},{12},'{13}',{14},{15},'{16}','{17}','{18}','{19}','{20}',{21})",
                                                     customerLoanRecord.FirstName, ////0
                                                    customerLoanRecord.MiddleName, ////1
                                                    customerLoanRecord.LastName, ////2
                                                    customerLoanRecord.FullAddress, ////3
                                                    customerLoanRecord.MobileNo1, ////4
                                                   customerLoanRecord.EmailID, ////5
                                                   (customerLoanRecord.DOB.HasValue ? "'" + customerLoanRecord.DOB.Value.ObjectToDBDateTime() + "'" : "null"), ////6
                                                   customerLoanRecord.Loan1IntrRate, ////7
                                                   customerLoanRecord.Loan1Amt, ////8
                                                   customerLoanRecord.Loan2IntrRate, ////9
                                                   customerLoanRecord.Loan2Amt, ////10
                                                   (customerLoanRecord.DateOfEMI.HasValue ? "'" + customerLoanRecord.DateOfEMI.Value.ObjectToDBDateTime() + "'" : "null"), ////11
                                                   (customerLoanRecord.ReleaseDate.HasValue ? "'" + customerLoanRecord.ReleaseDate.Value.ObjectToDBDateTime() + "'" : "null"), ////12
                                                   customerLoanRecord.HouseType, ////13
                                                   customerLoanRecord.Salary, ////14
                                                   customerLoanRecord.CustomerLoanOCRId, ////15
                                                   customerLoanRecord.CustomerAddr1, ////16
                                                   customerLoanRecord.CustomerAddr2, ////17
                                                    customerLoanRecord.Landmark, ////18
                                                    customerLoanRecord.State, ////19
                                                    customerLoanRecord.City, ////20
                                                    customerLoanRecord.Pincode);


Thanks Harshal Raut
Posted

Hi Pls dont use Formatted SQL Statements as it is risk of SQL Injection [^],,use SQL Parameterised c#[^]

sample:
C#
string qry = "INSERT INTO TableName (Column1,Column2,Column3) VALUES( @Column1,@Column2,@Column3)";

           SqlCommand cmd = new SqlCommand(qry, con);
           cmd.Parameters.AddWithValue("@Column1",  value1);
           cmd.Parameters.AddWithValue("@Column1",  value2);
           cmd.Parameters.AddWithValue("@Column1",  value3);
 
Share this answer
 
Comments
R Harshal 27-Jan-14 9:43am    
ok friend but in my company the requirement is to be like this which i wrote.So kindly will you able to help me in this situation.(formatted SQL Statement).Please
Karthik_Mahalingam 27-Jan-14 9:45am    
but it is not safe as it leads to sql injection attack...
Rahul VB 27-Jan-14 12:38pm    
Hey Karthik can you please elaborate the term "Sql Injection attack"?
Thanks a ton
Karthik_Mahalingam 27-Jan-14 20:14pm    
hi Rahul
check this links
http://ha.ckers.org/sqlinjection/
PIEBALDconsult 27-Jan-14 12:21pm    
This is your chance to improve things at your company. Parameterized commands are all kinds of better than what you show.

Have a look at this beauty: http://www.codeproject.com/Feature/WeirdAndWonderful.aspx/trackback/?msg=4473385
By using SQL Parameters.

http://www.dotnetperls.com/sqlparameter[^]

Your way is slightly better than string concatenation, but still highly exposed to SQL injection attacks. Using parameterized queries expecially with something this big will really help you form your SQL properly and give errors that you can understand.
 
Share this answer
 
Comments
R Harshal 27-Jan-14 10:20am    
i got the solution .
passing empty in pincode textbox is creating the error.
if we pass pincode it is working.
Thanks Buddy
The best solution which I can say to you is, Remove all the code and start it again from A. And add your query line by line and the same time run and check whether is there any errors or not. In that way you can easily find the missing or additional bracket there.
There is no short cut for find the missing bracket on this query.B'cos its string value.

Good Luck :)
 
Share this answer
 
Comments
R Harshal 27-Jan-14 10:20am    
i got the solution .
passing empty in pincode textbox is creating the error.
if we pass pincode it is working.
Thanks Buddy
Sampath Lokuge 27-Jan-14 10:24am    
Congratulation ! :)
Sampath Lokuge 27-Jan-14 10:28am    
For avoid it'll happen in future, you have to validate either on client side or sever side or may be both.Do that also.
R Harshal 27-Jan-14 10:41am    
Thanks Sir..
The solution, in this case, would be to use a parameterized statement rather than trying to use concatenation to create statement that could be problematic.

And don't store dates as strings.

And I hope you are hashing the pincode.
 
Share this answer
 
Comments
R Harshal 27-Jan-14 9:55am    
if pincode textbox is empty.you mean to say should i do like this for pincode.
'{21}'
PIEBALDconsult 27-Jan-14 9:58am    
No, I mean to store a hash of the value, not the actual value.
R Harshal 27-Jan-14 10:05am    
can you able to write some part ,so that i will get a idea..
R Harshal 27-Jan-14 10:05am    
Please Buddy...
R Harshal 27-Jan-14 10:20am    
i got the solution .
passing empty in pincode textbox is creating the error.
if we pass pincode it is working.
Thanks Buddy

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