Link to home
Start Free TrialLog in
Avatar of BOEING39
BOEING39

asked on

REREIVING IMAGE IN GRIDVIEW

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

ASKER CERTIFIED SOLUTION
Avatar of chaau
chaau
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BOEING39
BOEING39

ASKER

Quick accurate response.