Reputation: 10775
I have a list of 100 hotels in which i have to show the 10 images per hotel . I used the DataList control for this . As my images are stored in different table so i have to query each time for list of images using HotelId in ItemDataBound Event . but every time that stored procedure is called its taking around 6 seconds to complete . so for 100 rows its require 100*6 seconds and here is my code
protected void dlSearchResult_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (ListItemType.Item == e.Item.ItemType || ListItemType.AlternatingItem == e.Item.ItemType)
{
HtmlGenericControl ul = (HtmlGenericControl)e.Item.FindControl("ulImages");
DataSet dsImages = new DataSet();
string HotelId=dlSearchResult.DataKeys[e.Item.ItemIndex].ToString();
dsImages = SqlHelper.ExecuteDataset(Connection.ConnectionString, CommandType.StoredProcedure, "GetImage_ByHotelID", new SqlParameter("@HotelId", HotelId));
StringBuilder listtext=new StringBuilder ();
if (dsImages.Tables.Count > 0)
{
for (int i = 0; i < dsImages.Tables[0].Rows.Count; i++)
{
listtext.Append("<li class='PX_sri_photos_0'><a href='#'> <img src='" + dsImages.Tables[0].Rows[i]["URL"].ToString() + "' alt='" + dsImages.Tables[0].Rows[i]["Caption"].ToString() + "' /></a></li>");
}
}
ul.InnerHtml = listtext.ToString();
}
}
Please give me direction to optimize my code
Upvotes: 0
Views: 588
Reputation: 610
Why don't you utilize the base result of a datalist control, which is a table. This would avoid building a dataset from scratch. Looping is replaced by using a sql data adapter to fill the dataset. Create a table that holds the relationship of the hotel id and the id values of the 10 images for each of the 100 hotels. Join the 2 tables in your query and the resulting dataset should improve the wait time for the images to load in the webpage. I can't take the time to code this up, I am on my Android phone. Hope the suggestions help. ADO.Net works as fast as any other .Net data protocols, a lot time it is faster.
Upvotes: 0