6 Replies - 1893 Views - Last Post: 15 September 2010 - 02:32 AM Rate Topic: -----

#1 quitac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 28-March 09

searching in an access database

Posted 14 September 2010 - 08:01 AM

Hellomy friends. I want to ask you a question about searching in access database

I have an employee list and this list is consists of ID numbers and name. For ex:

12 Richard
122 Sam


And there is a search box that I can find my employess by typing theri ID number. But when ı type there 12 Sam and rİchard both come. I just want to see Richard there. here is my searching textbox command



command = New OleDb.OleDbCommand("select * from employee_list where ID LIKE '%" & TextBox1.Text & "%'", baglanti)

I try
command = New OleDb.OleDbCommand("select * from employee_list where ID EQV '%" & TextBox1.Text & "%'", baglanti)


this two ways doesn't work. Any ideas?

Is This A Good Question/Topic? 0
  • +

Replies To: searching in an access database

#2 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: searching in an access database

Posted 14 September 2010 - 08:13 AM

dont use LIKE use = and use parameters like so:
command = New OleDb.OleDbCommand("select * from employee_list where ID=@idn", baglanti)
command.Parameters.AddWithValue("@idn", TextBox1.Text)



good luck :)
Was This Post Helpful? 0
  • +
  • -

#3 quitac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 28-March 09

Re: searching in an access database

Posted 14 September 2010 - 08:17 AM

ID=@idn

what is "@idn" meaning?

By the way thank you very much my friend. Thank you for allocating your time for me. This really works.
Was This Post Helpful? 0
  • +
  • -

#4 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,606
  • Joined: 17-April 07

Re: searching in an access database

Posted 14 September 2010 - 08:23 AM

that's a parameter but i think in access this might cause some problem ok this is in your way
[oode]
command = New OleDb.OleDbCommand("select * from employee_list where ID ='" & TextBox1.Text & "'", baglanti)
[/code]

do you know why no brain use a parameter
to find the reason read this link
Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: searching in an access database

Posted 14 September 2010 - 08:46 AM

quitac,
I received your PM but I have to agree with NoBrain on this one.
Even if you don't use Parameters as NoBrain suggested, Thava still answered your question by showing you the SQL statement using the = instead of LIKE operator.

When you use LIKE %something% you will find all records that contains that something

so if you type 12 you would find
122
12
2121
2122
etc...
If you use = something you will limit the number of records to only those fields that contain that actual string.


EDIT:

Here's one example of why using a parameter will benefit you.

In Thava's example he enclosed the value of the ID in (') if this was a numeric field a datatype mismatch could be thrown causing your statement to error. With the parameter, these types of situations are handled.

This post has been edited by CharlieMay: 14 September 2010 - 08:51 AM

Was This Post Helpful? 1
  • +
  • -

#6 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: searching in an access database

Posted 14 September 2010 - 11:35 AM

not to mention the most important reason to use parameters. SQL injections you can read more about it here:
http://www.dreaminco...erized-queries/

EDIT:

Quote

quitac,
I received your PM ...


dont we all :P
Patience is a virtue dude

@thava this is the right way to do it. it will not cause any troubles in access (i do it in any of my queries in .NET). not to mention its easy to read when there is more then 1 parameter to add :)

This post has been edited by NoBrain: 14 September 2010 - 11:44 AM

Was This Post Helpful? 0
  • +
  • -

#7 emresutisna  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 80
  • Joined: 14-May 09

Re: searching in an access database

Posted 15 September 2010 - 02:32 AM

If you use a BindingSource to display your data, you can simply try this to search a data:
YourBindingSourceName.Filter = String.Format("ID = '{0}'", TextBox1.Text)


Hope it give you another way to make a Search Button :smile2:.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1