Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET SQL-Server
Hii i m new in asp and trying to upload image in folder(in website)and show in gridview but image is going in folder and path also going in database bt image is not showing image
How can i get image in gridview..plzzz any body help me...Thnxx
---------------------------
I m showing code here...
 
.aspx page code is..
 <%@ 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>Save Images In Folder and Display Images in Gridview from folder</title>
 <style type="text/css">
.Gridview
{
font-family:Verdana;
font-size:10pt;
font-weight:normal;
color:black; 
}
</style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:FileUpload ID="fileuploadimages" runat="server" />
    <br />
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
    </div>
    <div>
    <asp:GridView runat="server" ID="gvImages" AutoGenerateColumns="False" 
            DataSourceID="SqlDataSource1" CssClass="Gridview" 
            HeaderStyle-BackColor="#61A6F8" >
    <Columns>
    <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" 
            ReadOnly="True" SortExpression="Id" />
    <asp:BoundField DataField="ImageName" HeaderText="ImageName" 
            SortExpression="ImageName" />
        <asp:BoundField DataField="ImagePath" HeaderText="ImagePath" 
            SortExpression="ImagePath" />
    </Columns>
 
<HeaderStyle BackColor="#61A6F8"></HeaderStyle>
    </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ImageConnectionString %>" 
            SelectCommand="SELECT * FROM [ImagesPath]"></asp:SqlDataSource>
    </div>
    </form>
</body>
</html>
.cs code is
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //Get Filename from fileupload control
        string filename = Path.GetFileName(fileuploadimages.PostedFile.FileName);
        //Save images into Images folder
        fileuploadimages.SaveAs(Server.MapPath("Images/" + filename));
        //Getting dbconnection from web.config connectionstring
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ImageConnectionString"].ToString());
        //Open the database connection
        con.Open();
        //Query to insert images path and name into database
        SqlCommand cmd = new SqlCommand("Insert into ImagesPath(ImageName,ImagePath) values(@ImageName,@ImagePath)", con);
        //Passing parameters to query
        cmd.Parameters.AddWithValue("@ImageName", filename);
        cmd.Parameters.AddWithValue("@ImagePath", "Images/" + filename);
        cmd.ExecuteNonQuery();
        //Close dbconnection
        con.Close();
        Response.Redirect("~/Default.aspx");
    }
}
Posted 9-Jan-14 1:07am
Edited 9-Jan-14 1:08am
v2
Comments
KARTHIK Bangalore at 9-Jan-14 7:42am
   
there is no code to display images...
er.rakesh at 13-Jan-14 7:19am
   
plz show the content of your database i.e.
SELECT * FROM [ImagesPath]

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
In your code there is not any display image code.
You need to try Display image gallery in ASP.NET GridView Control[^].
You also can try this for step by step tutorial:
GridView Examples for ASP.NET 2.0: Displaying Images in a GridView Column[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 13 Jan 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100