Click here to Skip to main content
15,910,234 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please can anyone tell me why im gettin this error : Incorrect syntax near 'ListBus' .
Its refering to sda.Fill(ds,"ListBus")
in the code below:

C#
private void bindGrid()
        {
            string lidhja = "SERVER=localhost\\SqlExpress;Database=Projekti2;" + "Integrated Security=true;";
            SqlConnection cnnDB = new SqlConnection(lidhja);
            string query = string.Format("SELECT a.RouteID,a.Departure,a.Price FROM ListBus a INNER JOIN Route b ON a.RouteID=b.RouteID" +
                    "WHERE a.Departure='{0}' AND b.StartPlace='{1}' AND b.DestPlace='{2}'",
                    DepartureDate.Text, DropDownList1.SelectedValue, DropDownList2.SelectedValue);

            
            using (SqlDataAdapter sda = new SqlDataAdapter(query, cnnDB))
            {
                DataSet ds = new DataSet();
                sda.Fill(ds, "ListBus");
                GridView1.DataSource = ds.Tables["ListBus"].DefaultView;
                GridView1.DataBind();
            }
        }
Posted

1 solution

I think it is sqlexception. Give a space before the where clause in your query string.
" WHERE a.Departure='{0}' AND b.StartPlace='{1}' AND b.DestPlace='{2}'",
Be careful when you concatenate strings and don't forget add a space or line break. What you send to SQL server is "..ON a.RouteID=b.RouteIDWHERE a.Depart..".
 
Share this answer
 

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