Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The error is that "Incorrect syntax near Order"



C#
private void Window_Loaded(object sender, RoutedEventArgs e)
        {
         
 string ID = textBox1.text;
            string Contact = textBox2.Text;
            string Nme = textBox3.Text;
            string Cab = textBox3.Text;

            SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\BingMap\WpfApplication1\WpfApplication1\Database1.mdf;Integrated Security=True;User Instance=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("Select u.Phone,u.Name,o.CabType from Customer u join Order o on u.ID=o.OrderID where ID='" + lbid + "'", con);
            cmd.CommandType = CommandType.Text;
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                textBox2.Text = dr[1].ToString();
                textBox3.Text = dr[2].ToString();
                textBox3.Text = dr[1].ToString();

            }
            else
            {
                MessageBox.Show("Data not found");
            }
            con.Close();
        }
Posted
Updated 1-Aug-13 20:18pm
v4

I did one mistake,i have named my table name as Order and Order is a system variable in SQL.
So what i have to do is just change the table name.
 
Share this answer
 
Comments
[no name] 7-Aug-13 6:51am    
+5. From me.
Hi,
You are not getting any connection error right? Then check below code
Try this,
SQL
SqlCommand cmd = new SqlCommand("Select u.Phone,u.Name,o.CabType from Customer u join Order o on u.ID=o.OrderID where ID=@ID", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@ID",lbid);
//code to fetch 
 
Share this answer
 
Comments
Adarsh chauhan 2-Aug-13 1:44am    
I agree... as such nothing seems wrong in Apoorva's code. so s/he can try using parametrized query. it will also protect application from sql injection..
+5 Harshil
Apoorva from Baroda 7-Aug-13 6:38am    
Thanks Harshil,I found my mistake.My table name was Order and that is system variable in SQL.I have to just change the table name.

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