Solved

REREIVING IMAGE IN GRIDVIEW

Posted on 2013-11-26
2
257 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Any reason why this might be bad? 7 40
Using Simpleject with Class Library 11 25
c# code 19 59
Check my code out(2) 1 33
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now