.Net Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - More.Net Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 28th, 2012, 01:13 PM
cecilia_2004 cecilia_2004 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 305 cecilia_2004 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 5 Days 18 h 57 m 35 sec
Reputation Power: 0
Simple search problem

Hi everyone,

How to build a search function (using asp.net 3.5 with C#) to search the texts/keywords on page1.aspx, page2.aspx, page3.aspx, ..., etc.

For example,

page1.aspx contains "cars" and page2.aspx contains "car" and page3.aspx contains "computers".

Now I search "car" and the search reasults shows: page1.aspx and page2.aspx becuase both page1.aspx and page2.aspx contain "car".

Now I search "computer" and the search results shows: page3.aspx because page3.aspx contains "computers".

Thanks,

May

Reply With Quote
  #2  
Old March 29th, 2012, 08:58 PM
naynay447 naynay447 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 38 naynay447 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 4 m 3 sec
Reputation Power: 2
PLENTY of ways. Maybe go with a streamreader

http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx

Reply With Quote
  #3  
Old April 11th, 2012, 08:12 AM
Darknite Darknite is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: London
Posts: 40 Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 10 h 51 m 51 sec
Reputation Power: 14
Hello,

This question is a bit confusing.

Are you trying to search the text (markup) on each physical .aspx page?

Where does your content come from?

Does this content data not reside in a database?

Reply With Quote
  #4  
Old April 11th, 2012, 10:53 AM
cecilia_2004 cecilia_2004 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 305 cecilia_2004 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 5 Days 18 h 57 m 35 sec
Reputation Power: 0
Quote:
Originally Posted by Darknite
Hello,

This question is a bit confusing.

Are you trying to search the text (markup) on each physical .aspx page?

Where does your content come from?

Does this content data not reside in a database?


Hi,

I got the search engine working. And this search engine search the content of all *.aspx files. The problem I am having now is my code is unable to return the first n characters/words of the *.aspx file when the search engine finds the *.aspx file.

Thanks,

May

Reply With Quote
  #5  
Old April 11th, 2012, 11:21 AM
Darknite Darknite is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: London
Posts: 40 Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 10 h 51 m 51 sec
Reputation Power: 14
Hello May,

Quote:
Ok, without seeing any code, its like asking to perform brain surgery in the dark, without any tools


If you could post your code, then it does indeed make it easier for all

Reply With Quote
  #6  
Old April 11th, 2012, 12:35 PM
prog007 prog007 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Posts: 38 prog007 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 24 m 50 sec
Reputation Power: 6
Search engines first index the pages in database and then using proper query they find the results. You can also index the contents in database first and then can use full text search feature to get the results.

Reply With Quote
  #7  
Old April 11th, 2012, 05:03 PM
cecilia_2004 cecilia_2004 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 305 cecilia_2004 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 5 Days 18 h 57 m 35 sec
Reputation Power: 0
Quote:
Originally Posted by Darknite
Hello May,



If you could post your code, then it does indeed make it easier for all


No problem, I will post my code tonight.

Thanks,

May

Reply With Quote
  #8  
Old April 11th, 2012, 06:03 PM
cecilia_2004 cecilia_2004 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 305 cecilia_2004 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 5 Days 18 h 57 m 35 sec
Reputation Power: 0
Quote:
Originally Posted by cecilia_2004
No problem, I will post my code tonight.

Thanks,

May


Below is my code:

Site_Search_Results.aspx

Code:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Site_Search_Results.aspx.cs" Inherits="Site_Search_Results" %>



<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">

<br />

<asp:Panel ID="panel_Found" runat="server">
&nbsp;&nbsp;&nbsp;<b>Search results for :</b> <span style="color:Red;"><%= Request.Params.Get("Search_Keyword").Trim() %></span>
<br /><br />
</asp:Panel>

<asp:Panel ID="panel_Not_Found" runat="server">

<table>
<tr>
<td>
&nbsp;&nbsp;&nbsp;
</td>
<td>
Your search - <%= Request.Params.Get("Search_Keyword").Trim() %> - did not match any documents.
<br /><br />
Suggestions:
<br /><br />
&nbsp;&nbsp;&nbsp;• Make sure all words are spelled correctly.<br />
&nbsp;&nbsp;&nbsp;• Try different keywords.<br />
&nbsp;&nbsp;&nbsp;• Try more general keywords.
<br /><br />
If you search for chemicals and property information please visit page <a href="chemical_catalog.aspx?page=chemical_catalog">Chemical Catalog</a>.

<br />
</td>
</tr>
</table>

</asp:Panel>





<asp:DataGrid id="dgrdPages" CssClass="Grid" HorizontalAlign="Center" Width="98%" CellSpacing="0"
								BorderWidth="0" ShowHeader="False" AllowPaging="True" PageSize="5" PagerStyle-Mode="NumericPages"
								AutoGenerateColumns="False" AllowSorting="True" HeaderStyle-CssClass="GridHeader"
								PagerStyle-CssClass="GridPager" FooterStyle-CssClass="GridFooter" SelectedItemStyle-CssClass="GridSelectedItem"
								AlternatingItemStyle-CssClass="GridAlternatingItem" ItemStyle-CssClass="GridItem" CellPadding="0"
								Runat="Server"
                                OnPageIndexChanged="Grid_Change">
                                
								<SelectedItemStyle CssClass="GridSelectedItem"></SelectedItemStyle>
								<AlternatingItemStyle CssClass="GridAlternatingItem"></AlternatingItemStyle>
								<ItemStyle CssClass="GridItem"></ItemStyle>
								<HeaderStyle CssClass="GridHeader"></HeaderStyle>
								<FooterStyle CssClass="GridFooter"></FooterStyle>
								<Columns>
									<asp:TemplateColumn>
										<ItemTemplate>
                                                <table>
                                                  <tr>
                                                    <td><%# Eval("Title") %></td>
                                                  </tr>
                                                  <tr>
                                                    <td><%# Eval("Description") %></td>
                                                  </tr>                                                  
                                                  <tr>
                                                  <td>
                                                     <span style="color:Green;"><%# Eval("Url") %></span>
                                                  </td>
                                                  </tr>
                                                </table>
                                        </ItemTemplate>
                                     </asp:TemplateColumn>

                               </Columns>

                           </asp:DataGrid>
</asp:Content>



Site_Search_Results.aspx.cs

The following line in the code below does not work. What I try to do is to get the first 50 characters from the *.aspx file once the keywords found in this *.aspx file.

dr["Description"] = file.Substring(0, 50); //<== This line does not work.

// OR dr["Description"] = source.Substring(0, 50); //<== This line does not work.

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.IO;
using System.Net;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.OleDb;
using System.Net.Mail;

public partial class Site_Search_Results : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            dgrdPages.DataSource = CreateDataSource();
            dgrdPages.DataBind();
        }
    }


    private ICollection CreateDataSource()
    {
             

        string search = Request.Params.Get("Search_Keyword").Trim();

        string[] files = Directory.GetFiles(Server.MapPath("./"), "*.aspx", SearchOption.AllDirectories);

        string dir = Server.MapPath("./");

        string[] files1;
        int numFiles;
        files1 = Directory.GetFiles(dir);
        numFiles = files.Length;

        DataTable dtg = new DataTable();
        DataColumn dc = null;

        dc = new DataColumn("Title", typeof(string));
        dtg.Columns.Add(dc);

        dc = new DataColumn("Description", typeof(string));
        dtg.Columns.Add(dc);

        dc = new DataColumn("Url", typeof(string));
        dtg.Columns.Add(dc);


        // begin - search static files
        for (int i = 0; i < numFiles; i++)
        {
            string file = files[i].ToString();
            Search_Static_Files(file, search, dtg, dc);
        }
        // end - search static files


        DataView dv = new DataView(dtg);

        if (dv.Count <= 0)
        {
            panel_Found.Visible = false;
            panel_Not_Found.Visible = true;
            dgrdPages.Visible = false;

        }
        else
        {
            panel_Found.Visible = true;
            panel_Not_Found.Visible = false;
            dgrdPages.Visible = true;
        }

        return dv;

    }


    public void Grid_Change(Object sender, DataGridPageChangedEventArgs e)
    {

        // Set CurrentPageIndex to the page the user clicked.
        dgrdPages.CurrentPageIndex = e.NewPageIndex;

        // Rebind the data to refresh the DataGrid control. 
        dgrdPages.DataSource = CreateDataSource();
        dgrdPages.DataBind();
    }

    private void Search_Static_Files(String file, String searchText, DataTable dtg, DataColumn dc)
    {

        //Declare reader as a new StreamReader with file as the file to use

        System.IO.StreamReader reader = new System.IO.StreamReader(file);

        //Declare text as the reader reading to the end

        String text = reader.ReadToEnd();

        int FoundText;

        int intSearchLoopCounter;
        string[] sarySearchWord;
        
        char[] del = { ' ' };
        
        sarySearchWord = searchText.Trim().Split(del);        

        for (intSearchLoopCounter = 0; intSearchLoopCounter < sarySearchWord.Length; intSearchLoopCounter++)
        {
            FoundText = text.IndexOf(sarySearchWord[intSearchLoopCounter], StringComparison.CurrentCultureIgnoreCase);

            if (FoundText > -1)
            {


                WebClient x = new WebClient();
                string source = x.DownloadString(file);
                string title = Regex.Match(source, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;
            
                string s = Server.HtmlEncode(file);
            
                Uri uri = new Uri(file);
                string filename = Path.GetFileName(uri.LocalPath);

                string full_url = HttpContext.Current.Request.Url.AbsoluteUri;
                string url_without_filename = Path.GetDirectoryName(full_url);

                DataRow dr = dtg.NewRow();
                dr["Title"] = "<a href='" + filename.Trim() + "' style='Color:#1e4d7b'>" + title.Trim() + "</a>"; //url_without_filename.Trim() + "\\" + filename.Trim();

               dr["Description"] = file.Substring(0, 50); //<== This line does not work.
 // OR dr["Description"] = source.Substring(0, 50); //<== This line does not work.
                                
                dr["Url"] = url_without_filename.Trim() + "\\" + filename.Trim() + "<br /><br />";
                dtg.Rows.Add(dr);
                                             
                break;

            }

            else
            {

                //Show that it was not a match
            }

        }


    }

}

Last edited by cecilia_2004 : April 11th, 2012 at 06:13 PM.

Reply With Quote
  #9  
Old April 12th, 2012, 04:19 AM
Darknite Darknite is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: London
Posts: 40 Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 10 h 51 m 51 sec
Reputation Power: 14
If I've read your code correct,

the variable "file" is just the url correct?


If that is the case then this line will not work:

c# Code:
Original - c# Code
  1.  
  2. dr["Description"] = file.Substring(0, 50)


however this line should work assuming there are actually 50 characters or more:

c# Code:
Original - c# Code
  1.  
  2. dr["Description"] = source.Substring(0, 50);


have you debugged this, have you stepped through to that point and had a look at the variable "source" at that point?

What errors messages do you get?

Reply With Quote
  #10  
Old April 12th, 2012, 05:37 AM
cecilia_2004 cecilia_2004 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 305 cecilia_2004 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 5 Days 18 h 57 m 35 sec
Reputation Power: 0
Quote:
Originally Posted by Darknite
If I've read your code correct,

the variable "file" is just the url correct?


If that is the case then this line will not work:

c# Code:
Original - c# Code
  1.  
  2. dr["Description"] = file.Substring(0, 50)


however this line should work assuming there are actually 50 characters or more:

c# Code:
Original - c# Code
  1.  
  2. dr["Description"] = source.Substring(0, 50);


have you debugged this, have you stepped through to that point and had a look at the variable "source" at that point?

What errors messages do you get?


> the variable "file" is just the url correct?

Correct.

>dr["Description"] = source.Substring(0, 50);

I looked at the variable "source" which contains

"<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="about.aspx.cs" Inherit..."

and the code display 

Thanks,

May

Reply With Quote
  #11  
Old April 12th, 2012, 05:41 AM
cecilia_2004 cecilia_2004 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 305 cecilia_2004 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 5 Days 18 h 57 m 35 sec
Reputation Power: 0
Quote:
Originally Posted by cecilia_2004
Below is my code:

Site_Search_Results.aspx

Code:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Site_Search_Results.aspx.cs" Inherits="Site_Search_Results" %>



<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">

<br />

<asp:Panel ID="panel_Found" runat="server">
&nbsp;&nbsp;&nbsp;<b>Search results for :</b> <span style="color:Red;"><%= Request.Params.Get("Search_Keyword").Trim() %></span>
<br /><br />
</asp:Panel>

<asp:Panel ID="panel_Not_Found" runat="server">

<table>
<tr>
<td>
&nbsp;&nbsp;&nbsp;
</td>
<td>
Your search - <%= Request.Params.Get("Search_Keyword").Trim() %> - did not match any documents.
<br /><br />
Suggestions:
<br /><br />
&nbsp;&nbsp;&nbsp;• Make sure all words are spelled correctly.<br />
&nbsp;&nbsp;&nbsp;• Try different keywords.<br />
&nbsp;&nbsp;&nbsp;• Try more general keywords.
<br /><br />
If you search for chemicals and property information please visit page <a href="chemical_catalog.aspx?page=chemical_catalog">Chemical Catalog</a>.

<br />
</td>
</tr>
</table>

</asp:Panel>





<asp:DataGrid id="dgrdPages" CssClass="Grid" HorizontalAlign="Center" Width="98%" CellSpacing="0"
								BorderWidth="0" ShowHeader="False" AllowPaging="True" PageSize="5" PagerStyle-Mode="NumericPages"
								AutoGenerateColumns="False" AllowSorting="True" HeaderStyle-CssClass="GridHeader"
								PagerStyle-CssClass="GridPager" FooterStyle-CssClass="GridFooter" SelectedItemStyle-CssClass="GridSelectedItem"
								AlternatingItemStyle-CssClass="GridAlternatingItem" ItemStyle-CssClass="GridItem" CellPadding="0"
								Runat="Server"
                                OnPageIndexChanged="Grid_Change">
                                
								<SelectedItemStyle CssClass="GridSelectedItem"></SelectedItemStyle>
								<AlternatingItemStyle CssClass="GridAlternatingItem"></AlternatingItemStyle>
								<ItemStyle CssClass="GridItem"></ItemStyle>
								<HeaderStyle CssClass="GridHeader"></HeaderStyle>
								<FooterStyle CssClass="GridFooter"></FooterStyle>
								<Columns>
									<asp:TemplateColumn>
										<ItemTemplate>
                                                <table>
                                                  <tr>
                                                    <td><%# Eval("Title") %></td>
                                                  </tr>
                                                  <tr>
                                                    <td><%# Eval("Description") %></td>
                                                  </tr>                                                  
                                                  <tr>
                                                  <td>
                                                     <span style="color:Green;"><%# Eval("Url") %></span>
                                                  </td>
                                                  </tr>
                                                </table>
                                        </ItemTemplate>
                                     </asp:TemplateColumn>

                               </Columns>

                           </asp:DataGrid>
</asp:Content>



Site_Search_Results.aspx.cs

The following line in the code below does not work. What I try to do is to get the first 50 characters from the *.aspx file once the keywords found in this *.aspx file.

dr["Description"] = file.Substring(0, 50); //<== This line does not work.

// OR dr["Description"] = source.Substring(0, 50); //<== This line does not work.

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.IO;
using System.Net;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.OleDb;
using System.Net.Mail;

public partial class Site_Search_Results : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            dgrdPages.DataSource = CreateDataSource();
            dgrdPages.DataBind();
        }
    }


    private ICollection CreateDataSource()
    {
             

        string search = Request.Params.Get("Search_Keyword").Trim();

        string[] files = Directory.GetFiles(Server.MapPath("./"), "*.aspx", SearchOption.AllDirectories);

        string dir = Server.MapPath("./");

        string[] files1;
        int numFiles;
        files1 = Directory.GetFiles(dir);
        numFiles = files.Length;

        DataTable dtg = new DataTable();
        DataColumn dc = null;

        dc = new DataColumn("Title", typeof(string));
        dtg.Columns.Add(dc);

        dc = new DataColumn("Description", typeof(string));
        dtg.Columns.Add(dc);

        dc = new DataColumn("Url", typeof(string));
        dtg.Columns.Add(dc);


        // begin - search static files
        for (int i = 0; i < numFiles; i++)
        {
            string file = files[i].ToString();
            Search_Static_Files(file, search, dtg, dc);
        }
        // end - search static files


        DataView dv = new DataView(dtg);

        if (dv.Count <= 0)
        {
            panel_Found.Visible = false;
            panel_Not_Found.Visible = true;
            dgrdPages.Visible = false;

        }
        else
        {
            panel_Found.Visible = true;
            panel_Not_Found.Visible = false;
            dgrdPages.Visible = true;
        }

        return dv;

    }


    public void Grid_Change(Object sender, DataGridPageChangedEventArgs e)
    {

        // Set CurrentPageIndex to the page the user clicked.
        dgrdPages.CurrentPageIndex = e.NewPageIndex;

        // Rebind the data to refresh the DataGrid control. 
        dgrdPages.DataSource = CreateDataSource();
        dgrdPages.DataBind();
    }

    private void Search_Static_Files(String file, String searchText, DataTable dtg, DataColumn dc)
    {

        //Declare reader as a new StreamReader with file as the file to use

        System.IO.StreamReader reader = new System.IO.StreamReader(file);

        //Declare text as the reader reading to the end

        String text = reader.ReadToEnd();

        int FoundText;

        int intSearchLoopCounter;
        string[] sarySearchWord;
        
        char[] del = { ' ' };
        
        sarySearchWord = searchText.Trim().Split(del);        

        for (intSearchLoopCounter = 0; intSearchLoopCounter < sarySearchWord.Length; intSearchLoopCounter++)
        {
            FoundText = text.IndexOf(sarySearchWord[intSearchLoopCounter], StringComparison.CurrentCultureIgnoreCase);

            if (FoundText > -1)
            {


                WebClient x = new WebClient();
                string source = x.DownloadString(file);
                string title = Regex.Match(source, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;
            
                string s = Server.HtmlEncode(file);
            
                Uri uri = new Uri(file);
                string filename = Path.GetFileName(uri.LocalPath);

                string full_url = HttpContext.Current.Request.Url.AbsoluteUri;
                string url_without_filename = Path.GetDirectoryName(full_url);

                DataRow dr = dtg.NewRow();
                dr["Title"] = "<a href='" + filename.Trim() + "' style='Color:#1e4d7b'>" + title.Trim() + "</a>"; //url_without_filename.Trim() + "\\" + filename.Trim();

               dr["Description"] = file.Substring(0, 50); //<== This line does not work.
 // OR dr["Description"] = source.Substring(0, 50); //<== This line does not work.
                                
                dr["Url"] = url_without_filename.Trim() + "\\" + filename.Trim() + "<br /><br />";
                dtg.Rows.Add(dr);
                                             
                break;

            }

            else
            {

                //Show that it was not a match
            }

        }


    }

}


You can run the code by copying the
Site_Search_Results.aspx
Site_Search_Results.aspx.cs
Default.aspx
Default.aspx.cs

Please note that you need to create a number of *.aspx files so that the search engine can search them.

Default.aspx
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="textbox_SearchKeyword" runat="server" /> <asp:Button ID="button_Search" runat="server" Text="Search" OnClick="button_Search_Click" />
    </div>
    </form>
</body>
</html>


Default.aspx.cs
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }



    protected void button_Search_Click(object sender, EventArgs e)
    {
        Response.Redirect("Site_Search_Results.aspx?Search_Keyword=" + textbox_SearchKeyword.Text.Trim());
    }
}

Last edited by cecilia_2004 : April 12th, 2012 at 05:45 AM.

Reply With Quote
  #12  
Old April 12th, 2012, 07:32 AM
Darknite Darknite is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: London
Posts: 40 Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 10 h 51 m 51 sec
Reputation Power: 14
ok I haven't created a dummy project to try you code out.

However before I do that, have you tried something like this:

c# Code:
Original - c# Code
  1.  
  2.  
  3.  
  4. dr["Description"] = source.toString().Substring(0, 50);   



Also, what exactly is the error message?

Reply With Quote
  #13  
Old April 12th, 2012, 08:31 AM
Darknite Darknite is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: London
Posts: 40 Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level)Darknite User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 10 h 51 m 51 sec
Reputation Power: 14
Soo...

I tried understanding your code, but ended up just writing most of it from scratch:

so instead of using a heavy "datagrid", you can just generate the html yourself (its just a html table anyway).

try the following code, you will need to finish the rendering part your self, and play around with the extractions parts:

and it does not return just the first few characters for the descriptions, it does return the full 50 characters.


Remember THINK FUNCTIONALLY



c# Code:
Original - c# Code
  1.  
  2. using System;
  3. using System.Collections;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Web;
  8. using System.Web.Security;
  9. using System.Web.UI;
  10. using System.Web.UI.HtmlControls;
  11. using System.Web.UI.WebControls;
  12. using System.Web.UI.WebControls.WebParts;
  13. using System.Xml.Linq;
  14. using System.Collections.Generic;
  15. using System.IO;
  16. using System.Net;
  17.  
  18. namespace SearchEngine
  19. {
  20.     public partial class Search_Site_Results : System.Web.UI.Page
  21.     {
  22.         protected void Page_Load(object sender, EventArgs e)
  23.         {
  24.             if (!IsPostBack
  25.                 &&
  26.                 Request.QueryString["Search_Keyword"] != null
  27.                 &&
  28.                 !string.IsNullOrEmpty(Request.QueryString["Search_Keyword"].ToString())
  29.                 )
  30.             {
  31.                 //split search terms
  32.                 List<string> SearchList = new List<string>();
  33.  
  34.                 SearchList = Request.QueryString["Search_Keyword"].ToString().Split(' ').ToList<string>();
  35.  
  36.                 HTML_RESULT.Text = RenderResults(GetMatchedPages(SearchList, GetStaticFiles()));
  37.             }
  38.         }
  39.  
  40.         private List<string> GetStaticFiles()
  41.         {
  42.             //Get List of Static Files:
  43.             return Directory.GetFiles(Server.MapPath("./"), "*.aspx", SearchOption.AllDirectories).ToList<string>();;
  44.         }
  45.         private List<StaticPage> GetMatchedPages(List<string> SearchList, List<string> PageList)
  46.         {
  47.             List<StaticPage> MatchedPages = new List<StaticPage>();
  48.  
  49.             WebClient x = new WebClient();
  50.  
  51.             foreach (string aspx in PageList)
  52.             {
  53.                 string source = x.DownloadString(aspx);
  54.                 foreach (string word in SearchList)
  55.                 {
  56.                     if (source.ToLower().Contains(word.ToLower()))
  57.                     {
  58.                         //this aspx page contains one of the seach term so add it:
  59.                         StaticPage CurrentPage = new StaticPage();
  60.  
  61.                         CurrentPage.PageTitle = "";//extract Page Time
  62.                         CurrentPage.PageDescription = source.ToString().Substring(0, 50);
  63.  
  64.                         Uri Url = new Uri(aspx);
  65.                        
  66.                         CurrentPage.PageUrl = Url.AbsoluteUri.ToString();
  67.  
  68.                         if (!MatchedPages.Contains(CurrentPage))
  69.                         {
  70.                             MatchedPages.Add(CurrentPage);
  71.                         }
  72.                     }
  73.                 }               
  74.             }
  75.  
  76.             return MatchedPages;
  77.         }
  78.         private string RenderResults(List<StaticPage> PageLists)
  79.         {
  80.             string html = "";
  81.  
  82.             foreach (StaticPage aspx in PageLists)
  83.             {
  84.                 //Build your html table here using each attributes:
  85.                 html += aspx.PageUrl +"-->"+ aspx.PageDescription +"<br/><br/";
  86.             }
  87.  
  88.             return html;
  89.         }
  90.         public class StaticPage
  91.         {
  92.             public string PageTitle { get; set; }
  93.             public string PageDescription { get; set; }
  94.             public string PageUrl { get; set; }
  95.         }
  96.     }
  97. }

Reply With Quote
  #14  
Old April 12th, 2012, 09:22 AM
cecilia_2004 cecilia_2004 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 305 cecilia_2004 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 5 Days 18 h 57 m 35 sec
Reputation Power: 0
Quote:
Originally Posted by Darknite
ok I haven't created a dummy project to try you code out.

However before I do that, have you tried something like this:

c# Code:
Original - c# Code
  1.  
  2.  
  3.  
  4. dr["Description"] = source.toString().Substring(0, 50);   



Also, what exactly is the error message?


There is no error message. The code run without any error.
In the past, the code return nothing (or empty) when I made the code to return the first 50 characters.

Now the code return  instead of the first 50 characters.

I had tried source.toString().Substring(0, 50); and I got the same result.

Thanks,

May

Reply With Quote
  #15  
Old April 12th, 2012, 09:24 AM
cecilia_2004 cecilia_2004 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 305 cecilia_2004 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 5 Days 18 h 57 m 35 sec
Reputation Power: 0
Quote:
Originally Posted by Darknite
Soo...

I tried understanding your code, but ended up just writing most of it from scratch:

so instead of using a heavy "datagrid", you can just generate the html yourself (its just a html table anyway).

try the following code, you will need to finish the rendering part your self, and play around with the extractions parts:

and it does not return just the first few characters for the descriptions, it does return the full 50 characters.


Remember THINK FUNCTIONALLY



c# Code:
Original - c# Code
  1.  
  2. using System;
  3. using System.Collections;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Web;
  8. using System.Web.Security;
  9. using System.Web.UI;
  10. using System.Web.UI.HtmlControls;
  11. using System.Web.UI.WebControls;
  12. using System.Web.UI.WebControls.WebParts;
  13. using System.Xml.Linq;
  14. using System.Collections.Generic;
  15. using System.IO;
  16. using System.Net;
  17.  
  18. namespace SearchEngine
  19. {
  20.     public partial class Search_Site_Results : System.Web.UI.Page
  21.     {
  22.         protected void Page_Load(object sender, EventArgs e)
  23.         {
  24.             if (!IsPostBack
  25.                 &&
  26.                 Request.QueryString["Search_Keyword"] != null
  27.                 &&
  28.                 !string.IsNullOrEmpty(Request.QueryString["Search_Keyword"].ToString())
  29.                 )
  30.             {
  31.                 //split search terms
  32.                 List<string> SearchList = new List<string>();
  33.  
  34.                 SearchList = Request.QueryString["Search_Keyword"].ToString().Split(' ').ToList<string>();
  35.  
  36.                 HTML_RESULT.Text = RenderResults(GetMatchedPages(SearchList, GetStaticFiles()));
  37.             }
  38.         }
  39.  
  40.         private List<string> GetStaticFiles()
  41.         {
  42.             //Get List of Static Files:
  43.             return Directory.GetFiles(Server.MapPath("./"), "*.aspx", SearchOption.AllDirectories).ToList<string>();;
  44.         }
  45.         private List<StaticPage> GetMatchedPages(List<string> SearchList, List<string> PageList)
  46.         {
  47.             List<StaticPage> MatchedPages = new List<StaticPage>();
  48.  
  49.             WebClient x = new WebClient();
  50.  
  51.             foreach (string aspx in PageList)
  52.             {
  53.                 string source = x.DownloadString(aspx);
  54.                 foreach (string word in SearchList)
  55.                 {
  56.                     if (source.ToLower().Contains(word.ToLower()))
  57.                     {
  58.                         //this aspx page contains one of the seach term so add it:
  59.                         StaticPage CurrentPage = new StaticPage();
  60.  
  61.                         CurrentPage.PageTitle = "";//extract Page Time
  62.                         CurrentPage.PageDescription = source.ToString().Substring(0, 50);
  63.  
  64.                         Uri Url = new Uri(aspx);
  65.                        
  66.                         CurrentPage.PageUrl = Url.AbsoluteUri.ToString();
  67.  
  68.                         if (!MatchedPages.Contains(CurrentPage))
  69.                         {
  70.                             MatchedPages.Add(CurrentPage);
  71.                         }
  72.                     }
  73.                 }               
  74.             }
  75.  
  76.             return MatchedPages;
  77.         }
  78.         private string RenderResults(List<StaticPage> PageLists)
  79.         {
  80.             string html = "";
  81.  
  82.             foreach (StaticPage aspx in PageLists)
  83.             {
  84.                 //Build your html table here using each attributes:
  85.                 html += aspx.PageUrl +"-->"+ aspx.PageDescription +"<br/><br/";
  86.             }
  87.  
  88.             return html;
  89.         }
  90.         public class StaticPage
  91.         {
  92.             public string PageTitle { get; set; }
  93.             public string PageDescription { get; set; }
  94.             public string PageUrl { get; set; }
  95.         }
  96.     }
  97. }


Ok.

Thanks for the code!

May

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - More.Net Development > Simple search problem

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap