ARTICLE

How to Upload Images to Databases or How to Store Images to Database

Posted by Mahesh Babu Articles | ADO.NET in C# February 26, 2011
How to upload images to databases or how to store images to database
Reader Level:
Download Files:
 

I want to write this article using Layer Architecture.

database1.gif

This is the screen design. I want to store a product type image into the database.

I want to enter Product Type Name and that Image.

database2.gif

Presentation Layer

You have to write code for the Button Click event:

protected void Button2_Click(object sender, EventArgs e)
    {
        lblMessage.Visible = true;
        p.ProductType = txtName.Text;
        if (FileUpload1.PostedFile != null &&
            FileUpload1.PostedFile.FileName != "")
        {
            byte[] imageSize = new byte
                          [FileUpload1.PostedFile.ContentLength];
            HttpPostedFile uploadedImage = FileUpload1.PostedFile;
            uploadedImage.InputStream.Read
               (imageSize, 0, (int)FileUpload1.PostedFile.ContentLength);
            p.Image = imageSize;
        }

        p.InsertProducts();
        lblMessage.Text = "File Uploaded";

    }

Business Access Layer

public void InsertProducts()
    {
        SqlParameter[] p=new SqlParameter[2];
        p[0]=new SqlParameter("@ProductType",producttype);
        p[0].DbType=DbType.String;
        p[1]=new SqlParameter("Image",image);

        SqlHelper.ExecuteNonQuery(clsConnection.Connection,
CommandType.StoredProcedure, "sp_InsertProducts", p);
    }

// Here "sp_InsertProducts" is the stored procedure Name.

Data Access Layer

public class clsConnection
{
      public clsConnection()
      {
            //
            // TODO: Add constructor logic here
            //
      }
    public static string Connection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
}


Under Web.Config

<connectionStrings>
<
add name="ConnectionString" connectionString="user Id=sa;Password=123;DataBase=RentalShopping;Data Source=server2" providerName="System.Data.SqlClient"/>

</connectionStrings>

//Here write your database name,userid,password,datasource.

Under DataBase:

Create one database i.e RentalShopping.Under Database create one table.I have created one table i.e Products.

CREATE TABLE [dbo].[Products]
(
      [ProductId] [int] IDENTITY(101,1) NOT NULL,
      [ProductType] [varchar](50) NULL,
      [Image] [image] NULL
}

This is the Stored Procedure
USE [RentalShopping]
GO
-SET
ANSI_NULLS ON
GO
SET
QUOTED_IDENTIFIER ON
GO
ALTER
PROCEDURE [dbo].[sp_InsertProducts](@ProductType varchar(50),@Image image)
as
begin
  insert into Products(ProductType,Image)values(@ProductType,@Image)
  end

Login to add your contents and source code to this article
post comment
     

Thanks a lot Mahesh...

Posted by Anand Navalagatti Dec 17, 2012

Hi Anand, You can follow the below link to find How to retrieve images from database... http://www.c-sharpcorner.com/UploadFile/225bcd/how-to-retrieve-images-from-database-in-layer-architecture/

Posted by Mahesh Babu Dec 14, 2012

HI Thanks a lot Can you please share how to retrieve and display the Uploaded image back to Asp Page....?

Posted by Anand Navalagatti Dec 14, 2012

Thanks Rohatash......

Posted by Mahesh Babu Dec 06, 2012

Nice article Mahesh. Keep it up...

Posted by Rohatash Kumar Dec 05, 2012
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter