Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Sir,

This is code for Search Query on multiple criteria filled by user. I am firing this query and storing result in datatable and storing that datatable in session and retreving this session on another page to display search result in listview.

My problem is that, I want to count the number of records return by this query and display them on search Result page on top for
eg: 25 Records found or if no record found display no record found..
C#
SqlConnection con = new SqlConnection(str);
            string strQ = ("select s.Property_ID,s.Property_For,s.Property_Type,s.Property_Name,C.City_Name,L.Locality_Name,s.Price,s.Bedroom,s.Area,s.ImageName,s.ImagePath From tbl_Post_Property 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 Property_For='"+ rbtnsearch.SelectedItem.Text +"' and 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 + "");
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(strQ, con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            Session["Search"] = dt;
            con.Close();
            Response.Redirect("SearchResult.aspx", false);

Searchresult Page
C#
DataTable dt = new DataTable();
        dt = Session["Search"] as DataTable;         
        listview1.DataSource = dt;
        listview1.DataBind();
Posted
v2

C#
int rowcount = dt.rows.count();

will give you the number of records fetched.
 
Share this answer
 
v2
Comments
Raj.Rautela 18-Oct-12 7:56am    
thanks sir
you can put a label on the top of your flied and set the the value like this..

C#
Label1.Text = Covert.ToString(dt.Rows.Count)+"Records found ";


you can use conditional staement if you need to set message for zero rows
 
Share this answer
 
Comments
Raj.Rautela 18-Oct-12 7:56am    
thanks sir
Use the code as below.
C#
if(dt.Rows.Count > 0)
{
     lblSearchResultText.Text = Covert.ToString(dt.Rows.Count) + " Records found.";
}
else
{
     lblSearchResultText.Text = "No records found.";
}
 
Share this answer
 
Comments
Raj.Rautela 18-Oct-12 7:24am    
thanks sir..
Hi Raj,

Thanks for accepting the answer.

Regards,
Tadit
Hi Raj,

Anytime, my pleasure...
Please accept this answer, if it has helped you in any way.
This will help others to find the answer in one go and you will also be awarded with some points for this action...

Thanks,
Tadit

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