Click here to Skip to main content
15,885,773 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Sir M having a search form in my real estate site for searching properties there are many criterias like propertytype,city, locality,price,area i want to display the search result on diff page not on the same page...i'hv written a query which searchers the database properly but i dont know how to display the search result on another page and bind them in listview...on page load...

m doing like this to bind listview on searchresult page by (datatable)dt stored in session
C#
SqlConnection con = new SqlConnection(str);
 SqlDataAdapter da = new SqlDataAdapter();
 DataTable dt = new DataTable();
 dt = Session["Search"] as DataTable;
 da.Fill(dt);
 listview1.DataSource = dt;
 listview1.DataBind();

but it is showing error..
Fill: SelectCommand.Connection property has not been initialized.

Code Behind
C#
if (rbtnfs.Checked == true)
        {
            try
            {
                SqlConnection con = new SqlConnection(str);
                string strQ = ("select S.Property_Type,C.City_Name,L.Locality_Name,S.Price,S.Bedrooms,S.Area From tbl_post_property_sale as S inner join tbl_City as C on C.City_Id=S.City_id inner join tbl_Locality as L on L.Locality_Id=S.Locality_Id where S.Property_type='" + ddlpropertytype.SelectedItem.Text + "'and C.City_Id=" + ddlcity.SelectedValue + " and L.Locality_Id=" + ddllocality.SelectedValue + " and S.price between " + ddlminprice.SelectedValue + " and " + ddlmaxprice.SelectedValue + " and S.Bedrooms=" + ddlbedrooms.SelectedItem.Text + " and S.Area between '" + (txtminarea.Text).Trim() + " " + ddlarea.SelectedItem.Text + "'and '" + (txtmaxarea.Text).Trim() + " " + ddlarea.SelectedItem.Text + "'");
                SqlCommand cmdsearch = new SqlCommand(strQ, con);
                con.Open();
                cmdsearch.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {

            }

        }
        else
        {
            try
            {
                SqlConnection con = new SqlConnection(str);
                string strQ = ("select R.Property_Type,C.City_Name,L.Locality_Name,R.Price,R.Bedrooms,R.Area From tbl_post_property_rent as R inner join tbl_City as C on C.City_Id=R.City_id inner join tbl_Locality as L on L.Locality_Id=R.Locality_Id where R.Property_type='" + ddlpropertytype.SelectedItem.Text + "'and C.City_Id=" + ddlcity.SelectedValue + " and L.Locality_Id=" + ddllocality.SelectedValue + " and R.price between " + ddlminprice.SelectedValue + " and " + ddlmaxprice.SelectedValue + " and R.Bedrooms=" + ddlbedrooms.SelectedItem.Text + " and R.Area between '" + (txtminarea.Text).Trim() + " " + ddlarea.SelectedItem.Text + "'and '" + (txtmaxarea.Text).Trim() + " " + ddlarea.SelectedItem.Text + "'");
                SqlCommand cmdsearch = new SqlCommand(strQ, con);
                con.Open();
                cmdsearch.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {

            }

        }
Posted
Updated 4-Oct-12 8:56am
v4

C#
SqlConnection con = new SqlConnection(str);
 SqlDataAdapter da = new SqlDataAdapter();
 DataTable dt = new DataTable();
 dt = Session["Search"] as DataTable;
 da.Fill(dt);
 listview1.DataSource = dt;
 listview1.DataBind();

Incorrect implementation.

I see you already have data in Session. Since you have it, you don't need to hit DB and get back data. Simply do:
C#
if(Session["Search"] != null)
{
  listview1.DataSource = (DataTable) Session["Search"];
  listview1.DataBind();
}



Just to add on, the reason why you were getting an error was: You defined a dataadapter without a command and tried to fill the dataset. If you want to hit database and get data, first define the adapter command (probably a select statement) and then fill a dataset from it.
 
Share this answer
 
Comments
Raj.Rautela 4-Oct-12 23:41pm    
thanks u sir....
I your are retrieving your results from a database, you can just pass the parameters of the select method to the query string dictionary and get them back in the page you want to show data in.

it will look something like this: domain/folder/showResults.aspx?id1=this&id2=that
Then in the showResults page all you need to do is:
get the differents ids from the query string by doing this:
var id1 = Request.QueryString[id1]; and so on.
I hope this to help you.
 
Share this answer
 
v2
store your result session and get result from session on other page .
 
Share this answer
 
Comments
solanki.net 4-Oct-12 8:52am    
suppose u have table that contain your all search result.
then session["Record"] =dt;// for storing table in session.
// if u want to get this session on other page then

DataTable dt=new DataTable();
dt=session["Record"] as DataTable;


Ambesha 4-Oct-12 9:42am    
Yes ! you can :)

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