-5

I'm getting this error when I'm trying to open a form in my C# App and I don't know what might be the problem. The error is in this line of code DataTable dt = new DataTable(); dt.Load(cmd.ExecuteReader());

Error:

{"Incorrect syntax near ','."}

 public void popTraseu()
    {
        string cs = "Data Source=CODRINMA\\CODRINMA;Initial Catalog=TrafficManager;Integrated Security=True";
        string select = "ats.NrOrdine, a.IDAutogara, a.Denumire as Autogara, o.Oras as Oras, o.Judet FROM Curse c INNER JOIN Trasee t on c.IDTraseu=t.IDTraseu INNER JOIN AutogariTrasee ats on t.IDTraseu=ats.IDTraseu INNER JOIN Autogari a on ats.IDAutogara=a.IDAutogara INNER JOIN Orase o on a.IDOras=o.IDOras where IDCursa=@IDCursa ORDER BY ats.NrOrdine";

        try
        {
            using (SqlConnection con = new SqlConnection (cs))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand(select, con);
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("IDCursa", int.Parse(grdCurse.CurrentRow.Cells[0].FormattedValue.ToString()));
                DataTable dt = new DataTable();
                dt.Load(cmd.ExecuteReader());
                con.Close();
                grdDetalii.DataSource = dt;
            }
        }
       catch (Exception er) { MessageBox.Show(er.Message); }
Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
cdrrr
  • 147
  • 1
  • 2
  • 10

2 Answers2

6

It looks like you forgot to put select at the beginning of your query.

string select = "select ats.NrOrdine, a.IDAutogara, a.Denumire as Autogara, o.Oras as Oras, o.Judet FROM Curse c INNER JOIN Trasee t on c.IDTraseu=t.IDTraseu INNER JOIN AutogariTrasee ats on t.IDTraseu=ats.IDTraseu INNER JOIN Autogari a on ats.IDAutogara=a.IDAutogara INNER JOIN Orase o on a.IDOras=o.IDOras where IDCursa=@IDCursa ORDER BY ats.NrOrdine";
user707727
  • 1,287
  • 7
  • 16
2

Your select does not start with a SELECT. Hence the syntax of your SQL statement is invalid.

Also note this line:

cmd.Parameters.Clear();

is completely superfluous. There are no parameters in a new instance.

Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90