Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET SQL-Server
hi
I want to search like Google search engine. When I split a string to many word and connecting through foreach statement the result was not bind to gridview.
 
here is my code:-
 var xts = Key.Split(' ');
                string vals;
                foreach (string asd in xts)
                {
                    vals = asd.Trim();
                    cmd = new SqlCommand("SEL_SearchCompany", cnn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@KeyWord", SqlDbType.VarChar).Value = vals;
                    da = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    foreach (DataRow rw in dt.Rows)
                    {
                        DataRow rows1 = mainTable.NewRow();
                        rows1 = rw;
                        mainTable.Rows.Add(rows1);
                        //try
                        //{mainTable.Rows.Add(rw); 
                        //mainTable.ImportRow(rw);
                        //}
                        //catch { };
                    }
                }
                GridView1.DataSource = mainTable;
                GridView1.DataBind();
 
but this code no bind because exception is occurs which is row already in data table.
Posted 23-Dec-12 19:46pm
Edited 23-Dec-12 19:54pm
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The thing which you are looking for is probable an AutoComplete textbox or dropdown.
Refer the links below for the overview and knowledge base:
AutoComplete Textbox[^]
Create Multiple Column Autocomplete/Combobox in ASP.NET MVC 3[^]
 

--Amit
  Permalink  
Comments
Dharmendra-18 at 24-Dec-12 2:06am
   
on Textbox only write a string and split it through spaces and collect string array and then searching if searching complete then whole result is binding to gridview
just like google search engine when no hint provide
_Amy at 24-Dec-12 2:11am
   
I think you just need to put some special conditions in the AutoComplete extender. Do the same which you are doing in auto complete. I am sure it'll work. Because the process you are following is the same as you required, only you need to use AutoComplete with that. This will make it easy. :)
Dharmendra-18 at 24-Dec-12 2:14am
   
Ya I know autocomplete extender but we don't use it.
_Amy at 24-Dec-12 2:21am
   
For this kind of problem generally we used to have autocomplete only. If you are not using that, then I am sorry. I don't have any other solution. All the best. :)
Dharmendra-18 at 25-Dec-12 0:32am
   
i had solve this problem but some more situation occurs that is when you searching any complete string if whole string match then this record will becomes first data and there after second one is matching how to search
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

for 10 words you will call the database query 10 times. Try this alternative method:
 
Pass your text to your stored procedure and split it there. Use the following method for that:
 
CREATE FUNCTION SplitString(@String varchar(8000), @Delimiter char(1))       
returns @temptable TABLE (items varchar(8000))       
as       
begin       
    declare @idx int       
    declare @slice varchar(8000)       
    select @idx = 1       
        if len(@String)<1 or @String is null  return       
    while @idx!= 0       
    begin       
        set @idx = charindex(@Delimiter,@String)       
        if @idx!=0       
            set @slice = left(@String,@idx - 1)       
        else       
            set @slice = @String       
        if(len(@slice)>0)  
            insert into @temptable(Items) values(@slice)       
        set @String = right(@String,len(@String) - @idx)       
        if len(@String) = 0 break       
    end   
return      
end
 
You can get the list of words using the following line:
SELECT i.items FROM dbo.SplitString(@a,' ') AS i
 
Now you have list of words you can query your table for different records using this list of items(for which you have to write too much of code in front-end). A single result set is returned with search result. Check if it works for you.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

First, create the table structure of mainTable and then add rows like this:
 
foreach (DataRow rw in dt.Rows)
{
   mainTable.Rows.Add(rw.ItemArray);
}
  Permalink  
v2
Comments
Dharmendra-18 at 25-Dec-12 0:31am
   
this is working for me but having some other problem that is when you searching any complete string if whole string match then this record will becomes first data and there after second one is matching how to search
gagan sawaliya at 25-Dec-12 0:56am
   
pls post some example
Dharmendra-18 at 25-Dec-12 5:31am
   
on google search engine, indiamart.com etc
gagan sawaliya at 25-Dec-12 7:01am
   
this type of work is done by SEO and ranking basis
Dharmendra-18 at 25-Dec-12 23:03pm
   
but i have seen that type of project is in php. so i know every thing is possible .net
gagan sawaliya at 26-Dec-12 0:41am
   
create your own algo on basis of combination of maximum words match in mainTable table

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 404
1 Nirav Prabtani 273
2 OriginalGriff 199
3 _Amy 165
4 Maciej Los 145
0 OriginalGriff 7,755
1 Sergey Alexandrovich Kryukov 6,951
2 Maciej Los 3,994
3 Peter Leow 3,698
4 CHill60 2,742


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 25 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100