Displaying File Name From Upload Without Fakepath

In this post, Nosotros discuss how to save image in folder and path in the database table using FileUpload Control and  and so display uploaded images in a gridview.

In this post, we will cover the following points.

  • Display Images in GridView Control using the path stored in SQL Server database
  • How to testify images from database in gridview in asp internet
  • How to brandish paradigm in gridview from folder in asp.internet c#

I take created a tabular array in the database that will shop the paradigm path and name of the epitome.

                  CREATE Tabular array [dbo].[Imagetable](     [Id] [int] IDENTITY(1,i) NOT Aught,     [Filename] [nvarchar](150) NULL,     [Filepath] [nvarchar](250) Cypher )                                  
                                  
  1. Open up visual studio add new empty website
  2. File>>New>>Spider web Site then select "C# "Empty Web Site"

3.Now add together a folder in your website name Images or anything else every bit  you want.

Now I have added a Fileupload control, push, and a gridview and want to show all images in grid view.Nosotros want to display the images in GridView control. I have added ii Bound Fields which displays ID and File Proper name and an Image Field.

                  <%@ Page Language="VB"                    AutoEventWireup="simulated"                    CodeFile="Aimage.aspx.vb"                    Inherits="Aimage"                    %>  <!DOCTYPE html>  <html xmlns="http://world wide web.w3.org/1999/xhtml"> <head runat="server">     <title></title>     <style type="text/css">         .auto-style1 {             width: 100px;         }     </style> </head> <body>     <form id="form1"                    runat="server">     <div>              <table                    course="automobile-style1">             <tr>                 <td>                     <asp:FileUpload ID="FileUpload1"                    runat="server"                    />                 </td>                 <td>                     <asp:Button ID="Button1"                    runat="server"                    Text="Upload"                    />                 </td>             </tr>             <tr>                 <td colspan="2">                     <Columns>                       <asp:BoundField DataField =                    "ID"                    HeaderText =                    "ID"                    />                       <asp:BoundField DataField =                    "FileName"                    HeaderText =                    "Epitome Name"                    />                       <asp:ImageField DataImageUrlField="FilePath"                    ControlStyle Width="100"                    ControlStyle-Height =                    "100"                    HeaderText =                    "Preview Image"/>                    </Columns>                 </td>             </tr>         </table>          </div>     </form> </trunk> </html>                
                                  
  • Double click on upload push and write code on button click event.
  • On button click we save the image in website folder and path in the database.Now i have created a method bindgridview() which bind gridview from database.
                                      using                    System;                    using                    Organisation.Collections.Generic;                    using                    Organization.Linq;                    using                    Arrangement.Spider web;                    using                    Organisation.Spider web.UI;                    using                    System.Spider web.UI.WebControls;                    using                    System.Information.SqlClient;                    using                    System.Information;                    using                    System.Configuration;                    using                    System.IO;                    public                    partial                    form                    Aimage : System.Web.UI.Page {     SqlConnection con =                    new                    SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());                    protected                    void                    Page_Load(object                    sender, EventArgs eastward)     {      }                    protected                    void                    Button1_Click(object                    sender, EventArgs e)     {                    if                    (FileUpload1.PostedFile !=                    zippo)         {                    string                    FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);             FileUpload1.SaveAs(Server.MapPath("Images/"                    + FileName));                    //save picture into Images folder in the website                    string                    imagepath=                    "Images/"                    + FileName;             SqlCommand cmd =                    new                    SqlCommand("insert into Imagetable (Filename,Filepath) values('"                    + FileName +                    "','"+imagepath+"')",con);             con.Open();                    int                    i= cmd.ExecuteNonQuery();             con.Close();                    if                    (i > 0)             {                 Response.Write("<script>alert('image uploaded successfully!')</script>");                 bindgridview();             }                    else                    {                 Response.Write("<script>alert('something went wrong try afterwards!')</script>");                 bindgridview();             }         }     }                    public                    void                    bindgridview()     {         SqlCommand cmd =                    new                    SqlCommand("select * from Imagetable", con);         SqlDataAdapter adp =                    new                    SqlDataAdapter(cmd);         DataSet ds =                    new                    DataSet();         adp.Fill(ds);                    if                    (ds.Tables[0].Rows.Count > 0)         {             GridView1.DataSource = ds;             GridView1.DataBind();         }                       } }                

Download Source Code

Now run the application and upload a image.

Display image from database folder path

As nosotros know that GridView is a very useful  Asp .internet command for displaying data from a database. Using GridView nosotros can display items in the two-dimensional grid and provide more flexibility for displaying and working with information compared to other controls.Diverse features of GridView Control can be customized as per the requirement.

When we fix the AutoGenerateColumns property of GridView Control to true, GridView Control examine our Data Object using Reflection and find all the properties of a Custom Object or all the Fields of a Tape. Then create a Separate Column for each Field or Property in the aforementioned order in which they are found.

In this way, a GridView Command automatically creates columns based on the Field / Properties of its Record / Custom Object, which causes u.s. to write any Actress Code to render a Custom Object or Tape's information to Output. Nonetheless, many times we do not get the flexibility nosotros need past GridView Control due to Automatically Create Columns.

For example, if nosotros want to hide a column in our GridView Command, or change the gild in which information technology is rendered, or change the heading text of those columns, then we demand set AutoGenerateColumns Property to false to achieve all these requirements.

Subsequently setting this property to Imitation, we have  to manually specify the <Columns> section in the <GridView> Element.

Ashok Patel

I'm a software engineer, having good experience in software programming web designing with neat control on ASP.NET, React JS, Angular JS,.NET Core HTML5, JavaScript, T-SQL, JQuery.
As well take keen experience in Electronics and electrical engineers design.
I like to practice RND and Inquiry.

carricowayin1997.blogspot.com

Source: https://www.appsloveworld.com/uplaod-and-display-an-image-in-gridview-from-image-path-in-database-c/

0 Response to "Displaying File Name From Upload Without Fakepath"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel