Click here to Skip to main content
12,357,409 members (70,110 online)
Click here to Skip to main content
Articles » Web Development » ASP.NET » Samples » Downloads

Stats

60.9K views
2.4K downloads
45 bookmarked
Posted

GridViewImages from DB in ASP.NET using C#

, 26 Jun 2009 CPOL
GridViewImages from DB in ASP.NET using C#
DataGridImages
App_Data
Database.mdf
Database_log.LDF
DataGridImages in C# using Handler
App_Data
Database.mdf
Database_log.LDF
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)

Share

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.160621.1 | Last Updated 26 Jun 2009
Article Copyright 2008 by Naga Sridhar Madiraju
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid