5 Replies - 409 Views - Last Post: 07 November 2017 - 09:07 PM Rate Topic: -----

#1 Booney440  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-November 16

c# Searching in Access database

Posted 05 November 2017 - 07:35 AM

I have a Local Access database that I want to search by date my code returns nothing, what am I missing?

Error Thrown

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: Syntax error (missing operator) in query expression 'ToDay = like ('*'), con'.


private void btn_Search_Click(object sender, RoutedEventArgs e)
        {

            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
            con.Open();
            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandText = "Select * from Sheet1 WHERE ToDay like ('" + txt_Date.Text + "*')";
            cmd.Connection = con;
            OleDbDataReader rd = cmd.ExecuteReader();
            dataGrid.ItemsSource = rd;

            loadgrid();

        }


This post has been edited by Booney440: 05 November 2017 - 07:37 AM


Is This A Good Question/Topic? 0
  • +

Replies To: c# Searching in Access database

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5899
  • View blog
  • Posts: 20,142
  • Joined: 05-May 12

Re: c# Searching in Access database

Posted 05 November 2017 - 08:06 AM

I'm not a SQL person, but your LIKE expression looks wrong. I think you need double quotes only around the pattern you are trying to match, not parenthesis and sinqle quotes like you are currently doing.

As an aside, your code construction above is ripe for SQL injection.
Was This Post Helpful? 0
  • +
  • -

#3 Booney440  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-November 16

Re: c# Searching in Access database

Posted 05 November 2017 - 11:15 AM

I just need to Query on the database I don't have SQL running this program / app. just trying to fig it out.

Thanks
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5899
  • View blog
  • Posts: 20,142
  • Joined: 05-May 12

Re: c# Searching in Access database

Posted 05 November 2017 - 11:39 AM

The term 'SQL injection' is the generic term for a vulnerability where somebody takes advantage of somebody just inserting raw text from a user straight into a SQL database query -- just like you are doing above.
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13495
  • View blog
  • Posts: 53,911
  • Joined: 12-June 08

Re: c# Searching in Access database

Posted 05 November 2017 - 11:49 AM

You should have your WHERE clause be an equal.

WHERE ToDay = '01/01/2017'


No () or *.
Was This Post Helpful? 0
  • +
  • -

#6 JapanDave  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 366
  • Joined: 01-February 16

Re: c# Searching in Access database

Posted 07 November 2017 - 09:07 PM

Access syntax is different to SQL server, if that is a date type in the Database you select statement should be like this.

cmd.CommandText = "Select * from Sheet1 WHERE ToDay = #" + txt_Date.Text + "#";


And just too add to what @SkyDiver mentioned about SQL injection, this probably should be the correct way to do things.

DateTime textBoxDate = DateTime.Parse(txt_Date.Text);
cmd.Parameters.AddWithValue("@txtDate", textBoxDate);
cmd.CommandText = @"Select * from Sheet1 WHERE ToDay = @txtDate";

This post has been edited by JapanDave: 07 November 2017 - 09:54 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1