Searching within datagridview
-
Hey everyone, I've got a datagridview linked to an access database with a seach function. It's working well, but with one problem. It only displays the row(s) when you search for the complete User Name. It would be a big time saver if the search would locate any row(s) which contains the txtSearchID, so you would not need to type out the whole user name every time. At the moment I am using:
Command.CommandText = "SELECT * FROM Users WHERE [User ID] = '" + txtSearchID.Text + "'"; OleDbDataAdapter Adapter = new OleDbDataAdapter(Command); Adapter.Fill(ds);
I'm sure theres a simple answer, just can't quite figure it out. Any help greatly appreciated Martin -
Hey everyone, I've got a datagridview linked to an access database with a seach function. It's working well, but with one problem. It only displays the row(s) when you search for the complete User Name. It would be a big time saver if the search would locate any row(s) which contains the txtSearchID, so you would not need to type out the whole user name every time. At the moment I am using:
Command.CommandText = "SELECT * FROM Users WHERE [User ID] = '" + txtSearchID.Text + "'"; OleDbDataAdapter Adapter = new OleDbDataAdapter(Command); Adapter.Fill(ds);
I'm sure theres a simple answer, just can't quite figure it out. Any help greatly appreciated MartinIn access try Command.CommandText = "SELECT * FROM Users WHERE [User ID] = '*" + txtSearchID.Text + "*'";
-
Hey everyone, I've got a datagridview linked to an access database with a seach function. It's working well, but with one problem. It only displays the row(s) when you search for the complete User Name. It would be a big time saver if the search would locate any row(s) which contains the txtSearchID, so you would not need to type out the whole user name every time. At the moment I am using:
Command.CommandText = "SELECT * FROM Users WHERE [User ID] = '" + txtSearchID.Text + "'"; OleDbDataAdapter Adapter = new OleDbDataAdapter(Command); Adapter.Fill(ds);
I'm sure theres a simple answer, just can't quite figure it out. Any help greatly appreciated Martinyou will want to use 'LIKE' instead of '=' example...
Command.CommandText = "SELECT * FROM Users WHERE [User ID] LIKE '%" + txtSearchID.Text + "%'";
of course what you really want to do is...
Command.CommandText = "SELECT * FROM Users WHERE [User ID] LIKE '%@1'";
Command.Parameters.Add("@1", OleDbType.Char).Value = txtSearchID.Text;Life goes very fast. Tomorrow, today is already yesterday.
-
you will want to use 'LIKE' instead of '=' example...
Command.CommandText = "SELECT * FROM Users WHERE [User ID] LIKE '%" + txtSearchID.Text + "%'";
of course what you really want to do is...
Command.CommandText = "SELECT * FROM Users WHERE [User ID] LIKE '%@1'";
Command.Parameters.Add("@1", OleDbType.Char).Value = txtSearchID.Text;Life goes very fast. Tomorrow, today is already yesterday.
I think he is using an access database, not Sql Server. Better than %, he must use *
Command.CommandText = "SELECT * FROM Users WHERE [User ID] LIKE '*" + txtSearchID.Text + "*'";
but use parameters it´s a good choice.
-
I think he is using an access database, not Sql Server. Better than %, he must use *
Command.CommandText = "SELECT * FROM Users WHERE [User ID] LIKE '*" + txtSearchID.Text + "*'";
but use parameters it´s a good choice.
-
I use % with access and it works fine. What does * do that is better?
Life goes very fast. Tomorrow, today is already yesterday.
Nothing into access it's better, but the "Jet sql" suggest using like with * as wild pattern. If you use %, the OleDb Provider make the translation for you. If you try
select * from table where field1 like '%something%'
in the query panel of access you get an empty result.
-
Nothing into access it's better, but the "Jet sql" suggest using like with * as wild pattern. If you use %, the OleDb Provider make the translation for you. If you try
select * from table where field1 like '%something%'
in the query panel of access you get an empty result.
-
OK, well thanks for the info, is good to know :) But either way in the context it is being used then it does not matter if use * or %
Life goes very fast. Tomorrow, today is already yesterday.