By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,515 Members | 1,042 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,515 IT Pros & Developers. It's quick & easy.

Search text box for first name AND last name

100+
P: 294
I have a text box where users will enter a first and last name, and then search by those parameters. I can find plenty of examples and have done search functions where first name and last name are separate text boxes, however I am not sure how to go about this.

I understand it would be some kind of concatenation, yet I'm not sure how to go about it (Parameters, etc.). I am also curious on how to handle whether or not the user enters "FirstName LastName" or "LastName FirstName" in the text box. I suppose it would be the same logic just reversed.

Anyone ever done something like this who could provide insight? Here is what I have so far...

Expand|Select|Wrap|Line Numbers
  1. Private Sub Searchbtn_Click(sender As System.Object, e As System.EventArgs) Handles Searchbtn.Click
  2.         Dim SQLConnection As New SqlClient.SqlConnection("Data Source=SQLTEST_HR,4000\SQLEXPRESS;Integrated Security=True")
  3.         SQLConnection.Open()
  4.         Dim SqlCommand As New SqlClient.SqlCommand("Select * FROM PARTICIPANT", SQLConnection)
  5.  
  6.  
  7.         Dim SQLReader As SqlClient.SqlDataReader
  8.         SQLReader = SqlCommand.ExecuteReader
  9.         'Need Parameter
  10.  
  11.         If SQLReader.Read Then
  12.  
  13.         End If
Jan 10 '14 #1

✓ answered by Rabbit

I would split the input by a space delimiter and look for the individual values in the first and last name fields.

Share this Question
Share on Google+
12 Replies


Rabbit
Expert Mod 10K+
P: 12,382
Why not separate the inputs?
Jan 10 '14 #2

100+
P: 294
Rabbit, thanks for replying. Honestly, I wanted to just have knowledge of how this could be performed and have a better understanding of searching for items in a database (somewhat like Google's search). In no way is this method mandatory, however it was just a preference to be able to do it.
Jan 10 '14 #3

Rabbit
Expert Mod 10K+
P: 12,382
I would split the input by a space delimiter and look for the individual values in the first and last name fields.
Jan 10 '14 #4

100+
P: 294
Hmm. So I would create two variables, say First and Last.
Assign them to the two values that would be delimited.
and in the search button handler I would use those variables as the parameters?
Jan 10 '14 #5

Rabbit
Expert Mod 10K+
P: 12,382
Pretty much. It would be something like:
Expand|Select|Wrap|Line Numbers
  1. string1 = string before space
  2. string2 = string after space
  3.  
  4. sql = where (first name = string1 and last name = string2) or (first name = string2 and last name = string1)
Jan 10 '14 #6

100+
P: 294
Do you know how to declare the text in the textbox to a variable to delimit? I am getting an error "'Variable of type 1-dimensional array of String' cannot be converted to string.

Expand|Select|Wrap|Line Numbers
  1. Dim Search = Searchtxt.Text
  2.         Dim searchArray As String = Split(Searchtxt.Text)
  3.  
  4.         Dim NonEmpty As Integer = -1
  5.         For i As Integer = 0 To searchArray.Length - 1
  6.             If searchArray(i) <> "" Then
  7.                 NonEmpty += 1
  8.                 searchArray(NonEmpty) = searchArray(i)
  9.             End If
  10.         Next
  11.         ReDim Preserve searchArray(NonEmpty)
Jan 10 '14 #7

Rabbit
Expert Mod 10K+
P: 12,382
I don't understand what you are trying to do. You are basically setting the array equal to itself, I don't understand the purpose of trying to do that.
Jan 10 '14 #8

100+
P: 294
I've given up for now, haha. Thanks for your assistance, Rabbit.
Jan 10 '14 #9

Rabbit
Expert Mod 10K+
P: 12,382
Don't give up just yet. You don't need any of the code from lines 4 through 11. None of that was in my pseudo code. You just need to go straight to the filter now.
Jan 11 '14 #10

100+
P: 294
I managed to get it to work. Here's my code for the OnClick Event for a button:

Expand|Select|Wrap|Line Numbers
  1.         Dim Search As String() = DelimitTxt.Text.Split(" ")
  2.         Dim first As String = Search(0)
  3.         Dim last As String = Search(1)
  4.  
  5.         eFirsttxt.Text = first
  6.         eLasttxt.Text = last
Jan 17 '14 #11

Rabbit
Expert Mod 10K+
P: 12,382
Glad you got it working.
Jan 17 '14 #12

100+
P: 294
Thanks for the encouragement, Rabbit.
Jan 20 '14 #13

Post your reply

Sign in to post your reply or Sign up for a free account.