user4627339
user4627339

Reputation:

Storing And Displaying Images On ASP.NET Web Forms Website

The website I am building holds 571 houses with information about each house. Each house has a image associated with it. The information is coming from a SQL database table called houses. I am using a custom pager to filter the 571 houses with 5 results per page. I am using a repeater control to do this so I don't have multiple aspx pages. My question is I want to store the folder with the 571 images in a location (stored on a network drive) and set a path to the specific image in that folder for each house in the database table column called image and display the images for each house while using the repeater control. I have looked at a lot of tutorials but nothing is helping me. Please don't post any links to tutorials because I have looked at them all. If you have done anything like this before please post your experience because this is new to me. Source code below.

Database Structure

[Id]          NCHAR (10)     NOT NULL,
[Name]        NVARCHAR (MAX) NULL,
[Townland]    NVARCHAR (MAX) NULL,
[Near]        NVARCHAR (MAX) NULL,
[Status]      NVARCHAR (MAX) NULL,
[Built]       NVARCHAR (MAX) NULL,
[Description] NVARCHAR (MAX) NULL,
[Families]    NVARCHAR (MAX) NULL,
[Image]       VARCHAR (200)  NULL,
CONSTRAINT [PK_Houses] PRIMARY KEY CLUSTERED ([Id] ASC)

Example Table Data

enter image description here

Houses.aspx

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#expanderHead").click(function () {
                $("#expanderContent").slideToggle();
                if ($("#expanderSign").text() == "+") {
                    $("#expanderSign").html("-")
                }
                else {
                    $("#expanderSign").text("+")
                }
            });
        });
    </script>
    <script>
        $(function () { setCurrentTab('tab2'); });
    </script>
    <div class="box">
        <div>
            <div class="body">
                <h1>Search Houses</h1>

                <p>
                    Welcome to Houses of Mayo search page. Enter details below to search for a specific house. Additionally you can use advanced search or search by map.
                </p>
                <div>
                    <form style="margin-left: 32%">
                        Name of House:
                        <input type="search" placeholder="Search" style="margin-left: 7%">
                    </form>
                    <br />
                    <form style="margin-left: 32%">
                        Townland:
                        <input type="search" placeholder="Search" style="margin-left: 14%">
                    </form>
                    <br />
                    <form style="margin-left: 32%">
                        Near:
                        <input type="search" placeholder="Search" style="margin-left: 20%">
                    </form>
                    <br />
                    <form style="margin-left: 32%"><a id="expanderHead" style="cursor: pointer;">Advanced Search</a><input type="button" value="Search" class="button" style="margin-left: 35%" /></form>
                    <div id="expanderContent" style="display: none">
                        <br />
                        <form style="margin-left: 32%">
                            Associated Families:
                            <input type="search" placeholder="Search" style="margin-left: 2%">
                        </form>
                        <br />
                        <form style="margin-left: 32%">
                            Keyword:
                            <input type="search" placeholder="Search" style="margin-left: 15%">
                        </form>
                        <br />
                    </div>
                </div>
                <br />
                <br />
                <br />

                <h1>Houses By Alphabetical Order</h1>

                <ul id="rooms">
                    <asp:Repeater ID="rptData" runat="server">
                        <ItemTemplate>
                            <li>
                                <a href='<%# "HouseInfo.aspx?HouseId=" + Eval("Id").ToString() %>'>
                                    <img src="" alt="img" width="398" height="287"/></a>
                                <h2>
                                    <a href='<%# "HouseInfo.aspx?HouseId=" + Eval("Id").ToString() %>'>
                                        <asp:Label runat="server" Text='<%# Eval("Name") %>'></asp:Label></a></h2>
                                <p>
                                    <b>ID: </b>
                                    <asp:Label runat="server" Text='<%# Eval("Id") %>'></asp:Label>
                                    <br />
                                    <b>Name of House: </b>
                                    <asp:Label runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                                    <br />
                                    <b>Townland: </b>
                                    <asp:Label runat="server" Text='<%# Eval("Townland") %>'></asp:Label>
                                    <br />
                                    <b>Near: </b>
                                    <asp:Label runat="server" Text='<%# Eval("Near") %>'></asp:Label>
                                    <br />
                                    <b>Status/Public Access: </b>
                                    <asp:Label runat="server" Text='<%# Eval("Status") %>'></asp:Label>
                                    <br />

                                    <b>Date Built: </b>
                                    <asp:Label runat="server" Text='<%# Eval("Built") %>'></asp:Label>
                                </p>
                            </li>
                        </ItemTemplate>
                    </asp:Repeater>
                </ul>
                <asp:Repeater ID="rptPager" runat="server">
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkPage" runat="server" Text='<%#Eval("Text") %>' CommandArgument='<%# Eval("Value") %>'
                            Style="padding: 8px; margin: 2px; background: #ac9e94; border: solid 1px #666; font: 8pt; color: #594334; display: inline-block;"
                            CssClass='<%# Convert.ToBoolean(Eval("Enabled")) ? "page_enabled" : "page_disabled" %>'
                            OnClick="Page_Changed" OnClientClick='<%# !Convert.ToBoolean(Eval("Enabled")) ? "return false;" : "" %>'></asp:LinkButton>
                    </ItemTemplate>
                </asp:Repeater>
            </div>
        </div>
    </div>
</asp:Content>

Houses.aspx.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Houses_of_Mayo.images
{
    public partial class Houses : System.Web.UI.Page
    {
        private int PageSize = 5;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.GetHousesPageWise(1);
            }
        }

        private void GetHousesPageWise(int pageIndex)
        {
            string constring = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constring))
            {
                using (SqlCommand cmd = new SqlCommand("GetHousesPageWise", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
                    cmd.Parameters.AddWithValue("@PageSize", PageSize);
                    cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4);
                    cmd.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
                    con.Open();
                    IDataReader idr = cmd.ExecuteReader();
                    rptData.DataSource = idr;
                    rptData.DataBind();
                    idr.Close();
                    con.Close();
                    int recordCount = Convert.ToInt32(cmd.Parameters["@RecordCount"].Value);
                    this.PopulatePager(recordCount, pageIndex);
                }
            }
        }

        private void PopulatePager(int recordCount, int currentPage)
        {
            double dblPageCount = (double)((decimal)recordCount / Convert.ToDecimal(PageSize));
            int pageCount = (int)Math.Ceiling(dblPageCount);
            List<ListItem> pages = new List<ListItem>();
            if (pageCount > 0)
            {
                for (int i = 1; i <= pageCount; i++)
                {
                    pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
                }
            }
            rptPager.DataSource = pages;
            rptPager.DataBind();
        }

        protected void Page_Changed(object sender, EventArgs e)
        {
            int pageIndex = int.Parse((sender as LinkButton).CommandArgument);
            this.GetHousesPageWise(pageIndex);
        }

        public override void VerifyRenderingInServerForm(Control control)
        {
            return;
        }
    }
}

Store Procedure

CREATE PROCEDURE GetHousesPageWise
      @PageIndex INT = 1
      ,@PageSize INT = 5
      ,@RecordCount INT OUTPUT
AS
BEGIN
      SET NOCOUNT ON;
      SELECT ROW_NUMBER() OVER
      (
            ORDER BY [Name] ASC
      )AS RowNumber
      ,[Id]
      ,[Name]
      ,[Townland]
      ,[Near]
      ,[Status]
      ,[Built]
     INTO #Results
      FROM [Houses]

      SELECT @RecordCount = COUNT(*)
      FROM #Results

      SELECT * FROM #Results
      WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1

      DROP TABLE #Results
end

Houses.aspx

enter image description here

Upvotes: 4

Views: 1690

Answers (2)

Arun Raj
Arun Raj

Reputation: 969

Change stored procedure's select query

SELECT ROW_NUMBER() OVER
      (
            ORDER BY [Name] ASC
      )AS RowNumber
      ,[Id]
      ,[Name]
      ,[Townland]
      ,[Near]
      ,[Status]
      ,[Built]
,[Image] 
     INTO #Results
      FROM [Houses]

and use

 <img src='<%# Eval("Image") %>' alt="img" width="398" height="287"/>

Upvotes: 1

Yasien Dwieb
Yasien Dwieb

Reputation: 35

First of all I advice to bind to datasource by using NetTiers model like this

and then you use Eval as this "> />

Upvotes: 0

Related Questions