Solved

REREIVING IMAGE IN GRIDVIEW

Posted on 2013-11-26
2
262 Views
Last Modified: 2013-11-27
Please find the attached code.   I have a Gridview that displays and image in the Image column.   The upload function works correctly as the images are going to the "Image" folder; however, the image does not display in the Gridview.   Only a box with a black X in it.  I need assistance in resolving this issue?  


Markup Code:>>>>>>>>>>>>>>


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>GridView Images Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server"/>
        <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
    </div>
    <hr />
    <div>
    <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false" Font-Names = "Arial" >
            <Columns>
                <asp:BoundField DataField = "ID" HeaderText = "ID" />
                <asp:BoundField DataField = "fname" HeaderText = "Image Name" />
                <asp:ImageField DataImageUrlField = "fpath" ControlStyle-Width = "300" ControlStyle-Height = "300" HeaderText = "Preview Image"/>
            </Columns> 
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Open in new window



Code Behind:>>>>>>>>>>>>>>>

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["DB_67492_mainConnectionString"].ConnectionString;
        string strQuery = "select * from GridUpload order by ID";
        SqlCommand cmd = new SqlCommand(strQuery);
        SqlConnection con = new SqlConnection(strConnString);
        SqlDataAdapter sda = new SqlDataAdapter();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        try
        {
            con.Open();
            sda.SelectCommand = cmd;
            sda.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            con.Close();
            sda.Dispose();
            con.Dispose();
        }
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.PostedFile != null)
        {
            string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);

            //Save files to disk
            FileUpload1.SaveAs(Server.MapPath("~/images/" + FileName));

            //Add Entry to DataBase
            String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["DB_67492_mainConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(strConnString);
            string strQuery = "insert into GridUpload (fname, fpath) values(@fname, @fpath)";
            SqlCommand cmd = new SqlCommand(strQuery);
            cmd.Parameters.AddWithValue("@fname", FileName);
            cmd.Parameters.AddWithValue("@fpath", "Images/" + FileName);
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;

            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
        }
    }
}

Open in new window

0
Comment
Question by:BOEING39
2 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 39679875
Have you checked your GridUpload  table? Is the correct path is in fpath column? I think you need to make the path in this column a relative path starting with "/", like this: "/images/image1.jpg", so this line should be:
cmd.Parameters.AddWithValue("@fpath", "/images/" + FileName);

Open in new window

Try first to modify the data in the database, and then modify the code. Also, use the lower case (some web servers are case sensitive when it comes to path variables)
0
 

Author Closing Comment

by:BOEING39
ID: 39682519
Quick accurate response.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question