Click here to Skip to main content
15,880,905 members
Articles / Web Development / HTML

GridViewImages from DB in ASP.NET using C#

Rate me:
Please Sign up or sign in to vote.
4.80/5 (12 votes)
26 Jun 2009CPOL4 min read 82K   2.5K   45  
GridViewImages from DB in ASP.NET using C#
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;

            myConnection.Open();
            storeimage.ExecuteNonQuery();
            myConnection.Close();
        }

    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        GridView1.DataSource = FetchAllImagesInfo();
        GridView1.DataBind();
    }
    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();
        da.Fill(dt);
        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.

License

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


Written By
Web Developer Pasca Software Solutions
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions