Click here to Skip to main content
15,886,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:-
C#
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
Updated 23-Dec-12 19:54pm
v3

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
 
Share this answer
 
Comments
Dharmendra-18 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 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 24-Dec-12 2:14am    
Ya I know autocomplete extender but we don't use it.
_Amy 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 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
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:

SQL
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:
SQL
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.
 
Share this answer
 
First, create the table structure of mainTable and then add rows like this:

C#
foreach (DataRow rw in dt.Rows)
{
   mainTable.Rows.Add(rw.ItemArray);
}
 
Share this answer
 
v2
Comments
Dharmendra-18 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 25-Dec-12 0:56am    
pls post some example
Dharmendra-18 25-Dec-12 5:31am    
on google search engine, indiamart.com etc
gagan sawaliya 25-Dec-12 7:01am    
this type of work is done by SEO and ranking basis
Dharmendra-18 25-Dec-12 23:03pm    
but i have seen that type of project is in php. so i know every thing is possible .net

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900