Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am getting the below error.
System.Data.SqlClient.SqlException: Incorrect syntax near '='. What could cause this Error?
Please, can anyone help me?
My code is below.
 protected void btnLocate_Click(object sender, EventArgs e)
        {
            SqlConnection conDatabase = null;
            try
            {
                 conDatabase = new SqlConnection("server=.;Initial Catalog=harika;Integrated Security=True");
                conDatabase.Open();
                SqlCommand command = new SqlCommand("SELECT * FROM dbo.cars" +
                    "WHERE TagNumber = @TagNbr;", conDatabase);
                command.Parameters.Add("@TagNbr", SqlDbType.VarChar);
                command.Parameters["@TagNbr"].Value = txttag.Text;

                DataSet dsCars = new DataSet("CarsSet");
                SqlDataAdapter sdaCars = new SqlDataAdapter();
               sdaCars.SelectCommand = command;
                //command.CommandText = sdaCars.SelectCommand;

                sdaCars.Fill(dsCars);

                try
                {
                    DataRow recCar = dsCars.Tables[0].Rows[0];
                    if (recCar.IsNull("CarID"))
                        throw new IndexOutOfRangeException("Invalid Tag Number");
                    txtmake.Text = (string)recCar["Make"];
                    txtmodel.Text = (string)recCar["Model"];
                    txtyear.Text = (string)recCar["CarYear"];
                    chkk7.Checked = (bool)recCar["HasK7Player"];
                    chkcd.Checked = (bool)recCar["HasCDPlayer"];
                    chkdvd.Checked = (bool)recCar["HasDVDPlayer"];
                    chkavail.Checked = (bool)recCar["Available"];
                }
                catch (IndexOutOfRangeException)
                {
                    txttag.Text = "";
                    txtmake.Text = "";
                    txtmodel.Text = "";
                    txtyear.Text = "";
                    chkk7.Checked = false;
                    chkcd.Checked = false;
                    chkdvd.Checked = false;
                    chkavail.Checked = false;
                }
            }
            finally
            {
                conDatabase.Close();
            }
        }
        public SqlCommand Command { get; set; }
    }
}
Posted
Updated 17-Jan-11 20:44pm
v2
Comments
justinonday 18-Jan-11 3:28am    
are you got correct answer ???

Try putting a space after dbo.cars:
C#
SqlCommand command = new SqlCommand("SELECT * FROM dbo.cars "
+ "WHERE TagNumber = @TagNbr", conDatabase);
 
Share this answer
 
Comments
neha427 18-Jan-11 3:11am    
Hi ,
I tried this.
It works now.
Thanks a lot.
JOAT-MON 18-Jan-11 3:19am    
You're welcome.
SQL
SELECT * FROM dbo.cars" +
"WHERE TagNumber = @TagNbr;



Remove the ; and try.
 
Share this answer
 
v2
Comments
neha427 18-Jan-11 2:49am    
hi Abhinav,
I tried this.
But,still the same error.
Thanks
Abhinav S 18-Jan-11 2:57am    
If your question is not solved, dont mark this answer as answered. Other posters may miss this question.
Abhinav S 18-Jan-11 2:59am    
Make sure the text box value is not null.

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