Solved

REREIVING IMAGE IN GRIDVIEW

Posted on 2013-11-26
2
266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 25

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

632 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