Uploading and Storing Image Path to Database and Image to Folder - Part 2 (Displaying of Images)

In my previous example, we have learned on how to save the actual image to a folder and image path to the database. In this example, I’m going to show on how to display those images in a GridView and Repeater control.

To get started, let’s create a method for fetching the image information from the database. Here’s the code block below:

    private DataTable GetData

    {

        DataTable dt = new DataTable;

        SqlConnection connection = new SqlConnection(GetConnectionString);

        try

        {

            connection.Open;

            string sqlStatement = "SELECT * FROM ImageInfo";

            SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);

            SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

            sqlDa.Fill(dt);

        }

        catch (System.Data.SqlClient.SqlException ex)

        {

            string msg = "Fetch Error:";

            msg += ex.Message;

            throw new Exception(msg);

        }

        finally

        {

            connection.Close;

        }

        return dt;

    }

As you can see, the code above is very straight forward and self explanatory. Now since we already have a method for fetching the data from database then we can bind the result in a Data Representation control. First let’s use a GridView control for displaying the image.

In Visual Studio, grab a GridView control from the toolbox and place it in the webform. The GridView markup would look something like below:

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">

        <Columns>

            <asp:ImageField DataImageUrlField ="ImagePath" ControlStyle-Width="100px" ControlStyle-Height="100px"></asp:ImageField>

            <asp:BoundField DataField="ImageName" HeaderText="Image Name" />

        </Columns>

        </asp:GridView>

As you can see, we used ImageField column for displaying the image using the path and used a BoundField column for displaying the image infornamtion.

Now, to make it work then let’s bind the GridView control at Page_Load event. See below:

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!Page.IsPostBack)

        {

            DataTable dt = GetData;

            if (dt.Rows.Count > 0)

            {

                //Binding GridView

                GridView1.DataSource = dt;

                GridView1.DataBind;

            }

        }

    }

Running the code above will show something like below in the page:

!.JPG)

Now, let’s go ahead and display the image in the Repeater control. Here’s the markup of Repeater control:

        <asp:Repeater ID="Repeater1" runat="server">

        <ItemTemplate>

            <asp:Image ID="Image1" runat="server" Width="100px" Height="100px" ImageUrl='<%# Bind("ImagePath") %>' />

            <asp:Label ID="Label1" runat="server" Text='<%# Bind("ImageName")%>'></asp:Label>

        </ItemTemplate>

        </asp:Repeater>

Unlike GridView, a Repeater control doesn’t have an ImageField or BoundField columns so we use ItemTemplate so that we can add our own server control for displaying the image and the information as shown above.

At Page_Load event, we can bind the Repeater control as what we did for binding the GridView. See the code block below:

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!Page.IsPostBack)

        {

            DataTable dt = GetData;

            if (dt.Rows.Count > 0)

            {

                Repeater1.DataSource = dt;

                Repeater1.DataBind;

            }

        }

    }

Running the code above will show something like below in the page:

!(https://gwb.blob.core.windows.net/dotnetvinz/Output2.JPG)

That’s it! Hope you will find this example useful.

Technorati Tags: ADO.NET,ASP.NET,C#,GridView,Image Uploading

This article is part of the GWB Archives. Original Author: Vincent Maverick Durano

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.