Click here to Skip to main content
16,012,223 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I've a windows form containing Id,GroupName,MemberName,Date,InstallmentAmount.
when I'm adding data to database it shows "Incorrect Syntax near '1000'."(1000 is the amount of installment which I inserted in textbox.)
In database type of this field is numeric(8,2).
I'm not understanding why I'm getting this error. Please Help.

Here is my code:

C#
private void btnSAdd_Click(object sender, EventArgs e)
       {
           cmbSGID = Convert.ToInt16(cmbSGrpName.SelectedValue);
           cmbSMId=Convert.ToInt32(cmbSMName.SelectedValue);
           SavingsDetails sd = new SavingsDetails(lblSTId.Text,cmbSGID,cmbSMId,dtpInstallmentDate.Value.Date,Convert.ToInt32(txtInstallmentAmt.Text));
           sd.Add();
           setSavingsGridView();
           resetSavingsDetails();
       }



C#
public SavingsDetails(string _STId, int _GId, int _MId, DateTime _SDate, int _Amt)
        {
            con = new Connection();
            STId = Convert.ToInt16(_STId);
            GId = _GId;
            MId = _MId;
            SDate = _SDate;
            Amt = _Amt;
        }

        public void Add()
        {
            con.Execute("insert into tblSavingsDetails(STId,STDate,GId,MId,SavingInstallment) values("+ STId +","+ SDate.ToShortDateString() +","+ GId +","+ MId +","+ Amt +"");
            
        }
Posted
Comments
Kornfeld Eliyahu Peter 13-Jul-14 5:08am    
1. Never use string concatenation to create SQL query from code, use parameterized query...
2. What the actual SQL query you got? All the values are escaped as needed (enclosed in quotes)?

1 solution

I think you missed final ")"
C#
con.Execute("insert into tblSavingsDetails(STId,STDate,GId,MId,SavingInstallment) values("+ STId +","+ SDate.ToShortDateString() +","+ GId +","+ MId +","+ Amt +")");

as a best practice, don't concatenate and build the sql statement, use Parameters [^]
 
Share this answer
 
v2
Comments
Sneha_10 13-Jul-14 5:48am    
It was so obvious....Silly me.....Thank you for the correction.

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