2014年7月13日 星期日

[RESOLVED] internal search engine


Hi 


I am still biggner in C# and i would like some one to help me please, In my master page i have added textbox called: Srchtxt, and search button called: SearchBtn and i have use listview in page called SearchResult.aspx NOT in index.aspx which is the main
page, thats mean when the user enter the domain name the index.aspx will appear and if he write something in Srchtxt and click on SearchBtn  it should take him to the SearchResult.aspx page and show the results. I have create a session in master page to collect
the text written in Srchtxt like this: 


Session["SearchValue"] = Srchtxt.Text;
Response.Redirect("SearchResult.aspx");

and in the SearchResult.aspx page i have used this code but sure its not working as there is no Srchtxt  in the page so i need some one to help me to fix this code correctly, the code in SearchResult.aspx look like this:



 public partial class SearchResult : System.Web.UI.Page
{

string sc = ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString.ToString();

protected void Page_Load(object sender, EventArgs e)
{

SqlConnection srchsql = new SqlConnection(ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString);

string strser = " select [AdsID],[AdsTit], SUBSTRING([AdsDesc],1,155) as AdsDesc, [Section], [Category], [Wtags], [Country], [State], [City] from ads where (Wtags like '%' + @search + '%')";
//SELECT [AdsID],[AdsTit], SUBSTRING([AdsDesc],1,155) as AdsDesc, [Section], [Category], [Wtags], [Country], [State], [City] FROM [ads] WHERE (([Country] = @Country) AND ([Wtags] = @Wtags))
SqlCommand serch = new SqlCommand(strser, srchsql);
serch.Parameters.Add("@Search", SqlDbType.NVarChar).Value = Session["SearchValue"];


srchsql.Open();
serch.ExecuteNonQuery();
SqlDataAdapter srcds = new SqlDataAdapter();
srcds.SelectCommand = serch;
DataSet srcdset = new DataSet();
srcds.Fill(srcdset, "Wtags");
srchrsultlistview.DataSource = srcdset;
srchrsultlistview.DataBind();
srchsql.Close();

Session["SearchValue"] = Srchtxt.Text;
Response.Redirect("SearchResult.aspx");

}

protected void Adstitlinkbtn_Click(object sender, EventArgs e)
{

}
}

Note: that the SQL statment down i have make it as comment out of selected lines as i didnt know how i can added to the code above


WHERE (([Country] = @Country) AND ([Wtags] = @Wtags)

Many thanks for all :-)



Hello,


Using your normal code :


protected void Page_Load(object sender, EventArgs e)
{

SqlConnection srchsql = new SqlConnection(ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString);

string strser = " select [AdsID],[AdsTit], SUBSTRING([AdsDesc],1,155) as AdsDesc, [Section], [Category], [Wtags], [Country], [State], [City] from ads where Wtags like '% @search %')";

SqlCommand serch = new SqlCommand(strser, srchsql);
serch.Parameters.Add("@Search", SqlDbType.NVarChar).Value = Session["SearchValue"];
srchsql.Open();
DataTable sercdt=new DataTable();
sercdt.Load(serch.ExecuteReader());
srchsql.Close();
srchrsultlistview.DataSource = sercdt;
srchrsultlistview.DataBind();
Session["SearchValue"] = Srchtxt.Text;
Response.Redirect("SearchResult.aspx");

}

Using the query you commented out , check below:


protected void Page_Load(object sender, EventArgs e)
{

SqlConnection srchsql = new SqlConnection(ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString);

string strser = "SELECT [AdsID],[AdsTit], SUBSTRING([AdsDesc],1,155) as AdsDesc, [Section], [Category], [Wtags], [Country], [State], [City] FROM [ads] WHERE [Country] = @Country AND [Wtags] = @Wtags";

SqlCommand serch = new SqlCommand(strser, srchsql);
serch.Parameters.Add("@Country", SqlDbType.NVarChar).Value = "Some Country";
serch.Parameters.Add("@Wtags", SqlDbType.NVarChar).Value = "Some Tags";

srchsql.Open();
DataTable sercdt=new DataTable();
sercdt.Load(serch.ExecuteReader());
srchsql.Close();
srchrsultlistview.DataSource = sercdt;
srchrsultlistview.DataBind();
Session["SearchValue"] = Srchtxt.Text;
Response.Redirect("SearchResult.aspx");

}



Hope This Helps.


Feel free to ask any question(s).


Have a great day.



thanks for your reply but as i mention that the srchtxt is in the master page and the srchrsultlistview in another page so its will gives me an error message the one of the tools are not contain in the page, and this code where it suppose to be in the master
page or in SearchResult.aspx 


Currently i am using the SqlDataSource but i dont want to use it , i want to use code behind 


        ConnectionString="<%$ ConnectionStrings:BeravaConnectionString %>" 

SelectCommand="SELECT [AdsID],[AdsTit], SUBSTRING([AdsDesc],1,155) as AdsDesc, [Section], [Category], [Wtags], [Country], [State], [City] FROM [ads] WHERE (([Country] = @Country) AND ([Wtags] = @Wtags))">







First things first msimo, You dont need to store the value entered in search text box in a session. Rather using querty string is more advisible.  On your search button click event you can redirect the user to search page along with the values using query
string. In the search page load  you can query the data base and display results in list view.



Okay please could you help by writing this query string 



Hi msimo,


User some thing like this


http://www.aspdotnet-suresh.com/2012/10/query-string-in-aspnet-example-c-vbnet.html


The link above is the most simplest form of how to pass values from one page to another using query string. Session is one of them but costly as sessions are maintained on server and hits the performance.



On search button click


response.redirect("~/Search.aspx?searchvalue="+txtserach.text+"&country="+txtcountry.text);



In the search page in the page load fetch these values from the query string


protected void Page_Load(object sender, EventArgs e)


if(!IsPostBack)


{


{


   SearchValue = Request.QueryString["searchvalue"];


  Country = Request.QueryString["country"];


}


}


Now once you retrieve the values here in the seach page from the qyery string now pass the values to stored procedure... fetch the results and bind it to the list view. Another important thing to note is not to use inline sql statements... its always better
to use a stored procedure. 

Hope this helps you :)


Regards,


N1ZAM


沒有留言:

張貼留言