Click here to Skip to main content
11,410,118 members (61,332 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi!


I'm getting an error at da.Fill(dt) when i run the application, it says "Incorrect syntax near '='." and it's because WHERE clause because when i comment it out it runs with no errors.
can anyone please help me?

here's some code:
private void Kombi_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'kombiDBDataSet.KombiStateTbl' table. You can move, or remove it, as needed.
            this.kombiStateTblTableAdapter.Fill(this.kombiDBDataSet.KombiStateTbl);
            numberPlateComboBox.Text = oldIndexText;
            oldIndexText = numberPlateComboBox.Text;  
   
 
            conn.ConnectionString =(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Projects\RentalPlus\RentalPlus\KombiDB.mdf;Integrated Security=True;User Instance=True");
            conn.Open();
 
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            ds.Tables.Add(dt);
            SqlDataAdapter da = new SqlDataAdapter();
            String Param1 = this.Text;
 
            da = new SqlDataAdapter("SELECT RentalTbl.NumberPlate, RentalTbl.PricePD, RentalTbl.FromDate,"
            + "RentalTbl.ToDate, ClientTbl.CompanyName, ClientTbl.ContactName, ClientTbl.Mobile,"
            + "ClientTbl.Email FROM RentalTbl INNER JOIN ClientTbl ON RentalTbl.ClientID = ClientTbl.ClientID"
            + "WHERE(RentalTbl.NumberPlate = @Param1)", conn);
            
            da.Fill(dt);
 
            singleHistoryDataGridView.DataSource = dt.DefaultView;
 
            conn.Close();
                  
        }
Posted 4-Dec-12 5:38am
tyska377
Comments
richcb at 4-Dec-12 10:40am
   
Where is "@Param1" coming from? If you are meaning for it to be the string Param1 as in the code above, your WHERE clause should look like this:

Where (RentalTbl.NumberPlate = " + Param1 + ")",
tyska at 4-Dec-12 10:47am
   
i tried it still doesn't work.
DinoRondelly at 4-Dec-12 10:40am
   
Did you try putting @Param one in single quotes?
DinoRondelly at 4-Dec-12 10:42am
   
Where (RentalTbl.NumberPlate = '" + Param1 + "')", conn);
tyska at 4-Dec-12 10:48am
   
both ways, still same error
DinoRondelly at 4-Dec-12 10:52am
   
Did you try adding the spaces like in the solution below?
tyska at 4-Dec-12 10:55am
   
well your comment was helpfull, and wiht the accepted solution it all works now! thank you!
DinoRondelly at 4-Dec-12 11:01am
   
Your welcome,

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Spaces, my friend, spaces. As in you need one between
ClientTbl.ClientID
and
WHERE(RentalTbl
            da = new SqlDataAdapter("SELECT RentalTbl.NumberPlate, RentalTbl.PricePD, RentalTbl.FromDate,"
            + "RentalTbl.ToDate, ClientTbl.CompanyName, ClientTbl.ContactName, ClientTbl.Mobile,"
            + "ClientTbl.Email FROM RentalTbl INNER JOIN ClientTbl ON RentalTbl.ClientID = ClientTbl.ClientID "
            + "WHERE(RentalTbl.NumberPlate = @Param1)", conn);
  Permalink  
Comments
tyska at 4-Dec-12 10:57am
   
Thank you!!
OriginalGriff at 4-Dec-12 11:11am
   
You're welcome!
Marcus Kramer at 4-Dec-12 11:03am
   
+5. This one has bitten pretty much everyone who writes sql at some point.
OriginalGriff at 4-Dec-12 11:11am
   
Oh yes! And it's a PITA to see, because you end up reading what you meant to write! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 169
1 OriginalGriff 110
2 Maciej Los 68
3 BillWoodruff 60
4 Philippe Mori 60
0 Sergey Alexandrovich Kryukov 8,755
1 OriginalGriff 6,915
2 Maciej Los 3,390
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web04 | 2.8.150414.5 | Last Updated 4 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100