tags:

views:

76

answers:

2
SqlConnection con = new SqlConnection(str);
int ID;
ID = Int32.Parse(Combo_Stu.SelectedValue.ToString());
double a = double.Parse(Txt_Ins.Text);
string date = dtp_Stu.Value.ToString();
SqlCommand sqlcmd1 = new SqlCommand("INSERT INTO Instalment_Billing(ID,Pay_Date, Fees) VALUES (" + ID + "," + **date** + "," + a + ")", con);
con.Open();
sqlcmd1.ExecuteNonQuery();

Incorrect syntax near '10' I am getting this error in date guess pls help me

+3  A: 

The problem is probably that your date is unquoted, but the real solution to this problem is to use a parameterized query.

recursive
+3  A: 

Whenever you get an error in a line like:

SqlCommand sqlcmd1 = new SqlCommand("INSERT INTO Instalment_Billing(" +
    "ID,Pay_Date, Fees) VALUES (" + ID + "," + date + "," + a + ")", con);

you should immediately insert some code to print it out and check whether it's valid SQL:

Console.WriteLine ("INSERT INTO Instalment_Billing(" +
    "ID,Pay_Date, Fees) VALUES (" + ID + "," + date + "," + a + ")");

or:

System.Windows.Forms.MessageBox.Show("INSERT INTO Instalment_Billing" +
    "(ID,Pay_Date, Fees) VALUES (" + ID + "," + date + "," + a + ")");

This should show you exactly where your problem lies. Most likely insertions of dates should use strings of the format 'yyyy.mm.dd' or something similar so you should, at a bare minimum:

  • quote the date string (... + ",'" + date + "'," + ...).
  • ensure that its format is okay (I'm not sure what ToString() will give you off the top of my head but I would think, given that you posted this today and it's not the tenth month or tenth day of the month, that it's probably something like yy-mm-dd and the unquoted 10 from 2010 is causing you grief).
paxdiablo
SqlConnection con = new SqlConnection(str); int ID; ID = Int32.Parse(Combo_Stu.SelectedValue.ToString()); double a = double.Parse(Txt_Ins.Text); string date = dtp_Stu.Value.ToString("yyyy-MM-dd"); SqlCommand sqlcmd1 = new SqlCommand("INSERT INTO Instalment_Billing (ID, Pay_date, Fees) VALUES (1, CONVERT(DATETIME, '"+ date +"', 102), 100)", con); con.Open(); sqlcmd1.ExecuteNonQuery();Thanks a loti changed my input string to yyyy-MM-dd and it worked