Click here to Skip to main content
11,795,848 members (78,456 online)
Click here to Skip to main content
Articles » Web Development » ASP.NET » Samples » Downloads
Add your own
alternative version

GridViewImages from DB in ASP.NET using C#

, 26 Jun 2009 CPOL 59K 2.4K 45
GridViewImages from DB in ASP.NET using C#
DataGridImages in C# using Handler
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;

public partial class _Default : System.Web.UI.Page 
    protected void Page_Load(object sender, EventArgs e)

    protected void Button1_Click(object sender, EventArgs e)
        if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")

            byte[] myimage = new byte[FileUpload1.PostedFile.ContentLength];
            HttpPostedFile Image = FileUpload1.PostedFile;
            Image.InputStream.Read(myimage, 0, (int)FileUpload1.PostedFile.ContentLength);

            SqlConnection myConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security=True;User Instance=True");
            SqlCommand storeimage = new SqlCommand("INSERT INTO ImageGallery " + "(Image_Content, Image_Type, Image_Size) " + " values (@image, @imagetype, @imagesize)", myConnection);
            storeimage.Parameters.Add("@image", SqlDbType.Image, myimage.Length).Value = myimage;
            storeimage.Parameters.Add("@imagetype", SqlDbType.VarChar, 100).Value = FileUpload1.PostedFile.ContentType;
            storeimage.Parameters.Add("@imagesize", SqlDbType.BigInt, 99999).Value = FileUpload1.PostedFile.ContentLength;


    protected void LinkButton1_Click(object sender, EventArgs e)
        GridView1.DataSource = FetchAllImagesInfo();
    public DataTable FetchAllImagesInfo()
        string sql = "Select * from ImageGallery";
        SqlDataAdapter da = new SqlDataAdapter(sql, "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security=True;User Instance=True");
        DataTable dt = new DataTable();
        return dt;

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


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


About the Author

Naga Sridhar Madiraju
Web Developer Pasca Software Solutions
India India
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151002.1 | Last Updated 26 Jun 2009
Article Copyright 2008 by Naga Sridhar Madiraju
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid