Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

REREIVING IMAGE IN GRIDVIEW

Posted on 2013-11-26
2
Medium Priority
?
272 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 25

Accepted Solution

by:
chaau earned 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

772 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