Hey guys, I am really new at this and I am trying to do the following:
I need to split keywords entered into search box eg. if someone enters 'Research Design' in, it needs to split the words and then build an Sql query that will give me the following output :
SELECT [ProjectName], [ProjectID]
FROM [ProjectList]
WHERE ([ProjectName] LIKE '%' + 'Research' + '%') AND ([ProjectName] LIKE '%' + 'Design' + '%'
I figured I would use this method as a basic database search engine.
Thanks guys.
6 3731
Hi Scorpion, you don't say but I presume that you have already written the code to open the database connection and display your records etc? If not then there are lots of tutorials on the web and you can always come back here to ask any questions.
You can split the search string using the following bit of code as an example. It will create an array from the search string and then build the sql string from it.
<%
'Define your variables
Dim sSearchString
Dim sSQL
Dim i
Dim sSearchArray
'Get the value of the search box (I've called it sInput)
sSearchString = Request("sInput")
'Split the search string into separate words (The split function splits the string at the specified character - in this case " ")
sSearchArray= Split(sSearchString , " ")
'Build the first part of the sql string
sSQL = " SELECT [ProjectName], [ProjectID] FROM [ProjectList] "
'Add the first search word to the sql string
If UBound(sSearchArray) > 0 Then
sSQL = sSQL & " WHERE ([ProjectName] LIKE %" & sSearchArray(0) & "%) "
End If
'Add the rest of the words
If UBound(sSearchArray) > 1 Then
For i = 1 To UBound(sSearchArray)
sSQL = sSQL & " AND ([ProjectName] LIKE %" & sSearchArray(i) & "%) "
Next
End If
%>
Let me know how it goes or if you need any more help,
Dr B
Thanks a lot Dr. B for your help. Now when I try to display the data into a gridview, my guess was that I would need to create a dataset and fill it with the results and later bind it to the gridview. I tried the following but it did not work:
Dim sSearchString
Dim myConn As New Data.SqlClient.SqlConnection
myConn.ConnectionString = "Data Source=servername;Initial Catalog=databasename;Integrated Security=True"
myConn.Open()
Dim sSQL
Dim i
Dim sSearchArray
Dim ds As New DataSet
'Get the value of the search box (I've called it sInput)
sSearchString = Request.Cookies("search").Value
'Split the search string into separate words (The split function splits the string at the specified character - in this case " ")
sSearchArray = Split(sSearchString, " ")
'Build the first part of the sql string
sSQL = " SELECT [ProjectName], [ProjectID] FROM [ProjectList] "
'Add the first search word to the sql string
If UBound(sSearchArray) > 0 Then
sSQL = sSQL & " WHERE ([ProjectName] LIKE %" & sSearchArray(0) & "%) "
End If
'Add the rest of the words
If UBound(sSearchArray) > 1 Then
For i = 1 To UBound(sSearchArray)
sSQL = sSQL & " AND ([ProjectName] LIKE %" & sSearchArray(i) & "%) "
Next
End If
Dim ad As New Data.SqlClient.SqlDataAdapter(sSQL, myConn)
ad.Fill(ds)
GridView1.DataSource = ds
GridView1.DataBind()
myConn.Close()
I'm obviously doing something wrong there but I can't seem to figure out what.
There was some " ' " missing before the % that's why it was giving me an error.
Now For some reason, it worked but with some minor mistakes. Here is what it does: When the search box has exactly 1 word:
sSQL = " SELECT [ProjectName], [ProjectID] FROM [ProjectList] " instead of:
sSQL = sSQL & " WHERE ([ProjectName] LIKE '%" & sSearchArray(0) & "%') " When the search box has exaclty 2 words:
sSQL = sSQL & " WHERE ([ProjectName] LIKE '%" & sSearchArray(0) & "%') " instead of:
sSQL = sSQL & " AND ([ProjectName] LIKE '%" & sSearchArray(i) & "%') "
For 3 or more words, it work fine.
I dont seem to see what's wrong ??
This is the code:
<%
'Define your variables
Dim myConn As New Data.SqlClient.SqlConnection
Dim ds As New DataSet
Dim sSearchString
Dim sSQL
Dim i
Dim sSearchArray
'Get the value of the search box (I've called it sInput)
sSearchString = Request.Cookies("search").Value
'Split the search string into separate words (The split function splits the string at the specified character - in this case " ")
sSearchArray = Split(sSearchString, " ")
'Build the first part of the sql string
sSQL = " SELECT [ProjectName], [ProjectID] FROM [ProjectList] "
'Add the first search word to the sql string
If UBound(sSearchArray) > 0 Then
sSQL = sSQL & " WHERE ([ProjectName] LIKE '%" & sSearchArray(0) & "%') "
End If
'Add the rest of the words
If UBound(sSearchArray) > 1 Then
For i = 1 To UBound(sSearchArray)
sSQL = sSQL & " AND ([ProjectName] LIKE '%" & sSearchArray(i) & "%') "
Next
End If
myConn.ConnectionString = "Data Source=servername;Initial Catalog=databasename;Integrated Security=True"
myConn.Open()
Dim ad As New Data.SqlClient.SqlDataAdapter(sSQL, myConn)
ad.Fill(ds)
results.DataSource = ds
results.DataBind()
myConn.Close()
End Sub
%>
Scorpion, are you using classic ASP or ASP.NET ? This is a classic ASP forum and my initial answer to you was on that basis - if you're using .NET then we should change the code to reflect that.
Dr B
You are correct. I am using ASP.NET. I think I found a trick that makes it work just fine.
If Ubound(sSearchArray) + 1 > 0 then
....
End If.
That works for now. If there is a better way, please let me know. Otherwise, Thanks a whole lot.
Scorpion, are you using classic ASP or ASP.NET ? This is a classic ASP forum and my initial answer to you was on that basis - if you're using .NET then we should change the code to reflect that.
Dr B
Glad you got it sorted. If you have any more questions then post them in the .NET forum and the experts in there will be able to give you better advice.
As the script I gave you above was written for classic ASP there are a few things you can change to update it to ASP.NET.
Things like changing sSQL = sSQL & to sSQL +=
And sSearchArray = Split(sSearchString, " ") to sSearchArray = sSearchString.Split(" ")
Amongst other little things. Good luck with the project.
Dr B
Sign in to post your reply or Sign up for a free account.
Similar topics
by: middletree |
last post by:
Sorry for starting a new thread, but the original was 24 hours ago, and I am
afraid it won't get seen.
In the original, Ray advised me to change
if len(strPeopleID)>0 then
arrPeople =...
|
by: Anna Smith |
last post by:
I have an extremely long and complex select statement as
shown below and I need to split it onto two lines but not
quite sure how to do it because I've never needed to
before. I need to split it...
|
by: pmarisole |
last post by:
I am using the following code to split/join values in a multi-select
field. It is combining all the values in All the records into one long
string in each record in recordset.
Example: I have a...
|
by: MrHelpMe |
last post by:
Hello again experts,
I have successfully pulled data from an LDAP server and now what I want
to do is drop the data into a database table. The following is my code
that will insert the data but...
|
by: clayalphonso |
last post by:
Here is the code:
<%
dim testArray, testArray2
dim Conn, rs
dim sSQL, sConnString
'response.write request.form("sel1")
'testArray = split(request.form("sel1"),",")
'for each gidstuff In...
|
by: loken0673 |
last post by:
Hello All
I want to Split multirow resultset to single row, multicolumn resultset
my table is
ProfileDetail(ProfileId bigint, PropertyId bigint PropertyValue nvarchar(400))
Table data :
...
|
by: =?ISO-8859-15?Q?C=E9dric?= |
last post by:
Hi all,
I want to import a SQL script (SQLite) executing each queries separately.
- I read the SQL file
- I split the read string with the separator ";"
- I execute each query
string query...
|
by: chifm |
last post by:
Hi ALL,
I dont know much about php, but have got a long form that i need to split into two pages, i want to have the inputs from the page 1 past to page 2 and all submitted via email at the end. I...
|
by: Hvid Hat |
last post by:
Hi
At first, I thought I could only solve my problem with a C# method inside
my XSLT but I'm beginning to think it might be possible with XSLT only. So
I'm trying, but I need help :-) How can I...
|
by: CSharper |
last post by:
I have a string which is seprated by . and I want to split it and
convert it in a single line of code (using lambda may be), is it
possible? I have already verified all the characters in the string...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |