how to display an image to gridview using image path from stored procedure in a class library

Viewed 56

I want to store an image from a database to a gridview using a class library stored procedure. The image path is stored in the database is ~/App_LocalResources/Profile_BrgyCitizen/image.png

My database table is named "TblBrgyCitizen" and the name | data type for the image is ProfilePic | nvarchar(MAX)

Filename of stored procedure => GridviewBrgyCitizen

Stored Procedure SQL to display into gridview is

SELECT ProfilePic, FullName

Class Library code (see below)

public DataSet GridviewBrgyCitizen()
        {
            SqlDataAdapter da = new SqlDataAdapter("GridviewBrgyCitizen", myConn);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataSet ds = new DataSet();
            da.Fill(ds);
            
            return ds;
        }

aspx file code for gridview

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="5" ForeColor="#333333"
                        CssClass="grid-view-style" AllowPaging="True" OnRowCommand="GridView1_RowCommand" Height="20px" ShowHeaderWhenEmpty="True">
                        <AlternatingRowStyle BackColor="White" />
                        <Columns>
                            <asp:ImageField DataImageUrlField="ProfilePic" HeaderText="Profile Pic" >
                                <HeaderStyle Width="60px" />
                                <ItemStyle Height="50px" HorizontalAlign="Center" Width="50px" />
                            </asp:ImageField>

aspx.cs file for where the gridview resides

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                GETGridviewBrgyCitizen();
        }

        DataAccess myData = new DataAccess();

        public void GETGridviewBrgyCitizen()
        {
            GridView1.DataSource = myData.GridviewBrgyCitizen();
            GridView1.DataBind();
        }
<table class="brgy-citizen-table-style">
            <tr>
                <td class="td-topleft-header">
                    <input name="ctl00$ContentPlaceHolder1$txtSearchBox" type="text" id="ContentPlaceHolder1_txtSearchBox" class="search-box" Placeholder="Name keywords" /><input type="submit" name="ctl00$ContentPlaceHolder1$btnSearch" value="Search" id="ContentPlaceHolder1_btnSearch" class="search-button-style" /></td>
                <td class="td-topright-header">
                    <button id="ContentPlaceHolder1_btnAdd" class="add-button-style"><span>ADD</span></button>
                    <button id="ContentPlaceHolder1_btnView" style="display: none;"><span>VIEW</span></button>
                    <button id="ContentPlaceHolder1_btnArchive" style="display: none;"><span>ARCHIVE</span></button>
                </td>
            </tr>
            <tr>
                <td colspan="2" class="table-property">
                    <div>
    <table class="grid-view-style" cellspacing="0" cellpadding="5" rules="all" border="1" id="ContentPlaceHolder1_GridView1" style="color:#333333;height:20px;border-collapse:collapse;">
        <tr align="left" style="color:White;background-color:#1C5E55;font-weight:bold;">
            <th scope="col" style="width:60px;">Profile Pic</th><th scope="col" style="width:300px;">Full Name</th><th scope="col" style="width:120px;">National ID</th><th scope="col" style="width:70px;">Gender</th><th scope="col" style="width:120px;">Civil Status</th><th scope="col" style="width:120px;">Voter Status</th><th scope="col" style="width:120px;">Contact No</th><th scope="col" style="width:70px;">Action</th>
        </tr><tr align="left" valign="middle" style="background-color:#E3EAEB;">
            <td align="center" style="height:50px;width:50px;"><img src="../../App_LocalResources/Profile_BrgyCitizen/Philip_Formal_Attire.png" /></td><td>Jose Ang</td><td>111-1111-111</td><td>Male</td><td>Single</td><td>Yes</td><td>0999999999</td><td align="center" style="width:50px;">
                                    <input type="image" name="ctl00$ContentPlaceHolder1$GridView1$ctl02$ctl02" title="View" src="../../Assets/css/barangay-official-page/images/icon-view.png" style="height:30px;width:30px;" />
                                    <input type="image" name="ctl00$ContentPlaceHolder1$GridView1$ctl02$ctl03" title="Archive" src="../../Assets/css/barangay-official-page/images/icon-archive.png" style="height:30px;width:30px;" />
                                </td>
        </tr>
    </table>
</div>
                </td>
            </tr>
        </table>

Inspect Element: enter image description here

1 Answers

what you have should work. Does the GV load, display other columns and data correctly??

And yes, since this is server side code, then the including the "~/ and your path name" is correct. Keep in mind that for markup, you can NOT use the "~", but you can and should use that format for server side code.

As a quick test?

Drag and drop the image from the solution explore into a blank clean area on the web page. It will and should spit out a image control for you.

so, for example, I have this:

enter image description here

So, lets double check the path name it will generate. But, remember, WHERE is your current page!!!!

My current page is Test5\fun.aspx

So, when I drag + drop in the image (just drag from project explore into the markup), you get this:

enter image description here

Note the path name it generated!!!

however, in my database, I do have this path name:

enter image description here

So, for a plain jane image image control?

You MUST remove the ~ in markup, (but keep the /).

But, for above, say grid view? Or any server side code?

then the "~" is fine.

So, lets send that data table to this grid:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadGrid();
    }

    void LoadGrid()
    {
        using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
        {
            string strSQL = @"SELECT ID, Animal, ImagePath, PictureName FROM tblAnimals";
            using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
            {
                conn.Open();
                DataTable rstData = new DataTable();
                rstData.Load(cmdSQL.ExecuteReader());
                GridView1.DataSource = rstData;
                GridView1.DataBind();
            }
        }
    }

And we now get this:

enter image description here

Also, NOTE VERY close, your size and width settings will not work.

You need to use this:

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

So, use ControlStyle-Width (and height if need be, but width usually does the job).

But, as noted, either your data is not being pulled, but if the other grid columns display, then you do have a path name error.

Related