0

I want to use my textBox to search on my datagridViewn. I´m done this right now but just with the "Name" on database. I need the same textBox to search for "name" or "DataCad" (datacad is DateTime type) or "city". I have all this fields on my table.

if the user type "John", all users with this name show on grid. (alread work with the name). If user tipe "12/08/2017" it show all data with this day time. And same with "city".

Is this possible? Here is my code working with name:

if (!string.IsNullOrEmpty(txtSearch.Text))
            {
                pacientesBindingSource.Filter = string.Format("Nome LIKE '*{0}*'", txtSearch.Text);

            }
Brugo
  • 101
  • 1
  • 9
  • 1
    I'm not familiar with this approach, but it seems like changing `Nome LIKE '*{0}*'` to `Nome LIKE '*{0}*' OR City LIKE '*{0}*'` should work. DateTime is a little trickier because you are comparing a string to a DateTime - the answer would depend on whether all of your DateTimes have a Time of 00:00:00.000, etc, but try linking together all of your fields with ORs – Dave Smash Aug 17 '17 at 20:18
  • 1
    I can't close this browser window without warning you about a little thing called **SQL Injection**. Please, check this link before continuing with your code: [https://stackoverflow.com/questions/35163361/how-can-i-add-user-supplied-input-to-an-sql-statement](https://stackoverflow.com/questions/35163361/how-can-i-add-user-supplied-input-to-an-sql-statement) – andiblas Aug 17 '17 at 20:33
  • tnk you friend. Gona look into this, – Brugo Aug 17 '17 at 20:43
  • string.Format("Nome LIKE '*{0}*' OR DataCad = '*{1}*' OR City LIKE '*{2}*'", txtSearch.Text, txtSearch.Text, txtSearch.Text); i think this is it? you can try it – Muj Aug 19 '17 at 09:09

1 Answers1

1

I'm not sure how the rest of your code is operating but it looks like the Filter property is a where clause. I believe you can just add an OR for the DataCad field like so:

string.Format("Nome LIKE '*{0}*' OR Cast(DataCad as date) = Cast('{0}' as date) OR City LIKE '*{0}*'", txtSearch.Text);

Pete
  • 769
  • 7
  • 20
  • 1
    Beat me to it, but I don't think you can use the LIKE operator with a DateTime (at least in the SQL Server world...) – Dave Smash Aug 17 '17 at 20:20
  • Ah yikes, I missed the part where DataCad is a date field. I'll amend the answer but I don't like it :) – Pete Aug 17 '17 at 20:26
  • Yep. I got a error saying 'LIKE' cannot be used with dateTime. – Brugo Aug 17 '17 at 20:27
  • Is it the astrisks? Try removing them from the Cast call:string.Format("Nome LIKE '*{0}*' OR Cast(DataCad as date) = Cast('{0}' as date) OR City LIKE '*{0}*'", txtSearch.Text); – Pete Aug 17 '17 at 20:33
  • Same error without asterisks. undefined Cast() on expression. – Brugo Aug 17 '17 at 20:39
  • What database engine are you using? The * makes me think of MS Access? – Pete Aug 17 '17 at 21:08
  • The local database of visual studio. Called service-based database. its a local .mdf file. – Brugo Aug 17 '17 at 21:11
  • `string.Format("Nome LIKE '*{0}*' OR DataCad = '*{0}*' OR City LIKE '*{0}*'", txtSearch.Text);` i think this is it? you can try it – Muj Aug 18 '17 at 03:16