SIGN UP MEMBER LOGIN:    
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 In Layer Architecture.

database1.gif

This is the screen Design. I want to store product Type Image into the database.

I want to enter Product Type Name and that Image.

database2.gif

Presentation Layer

You have to write the code under Button:

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
share this article :
post comment
 

sql helper does not find.......

Posted by ashish sharma Oct 26, 2011

hey im having a problem in the products.cs i have a red line under SQLHELPER what an i supose to do with it i hav tried several thing but i just dont know what to do

Posted by sultan kadanyo Aug 27, 2011

http://www.c-sharpcorner.com/UploadFile/225bcd/5807/ see this link u can find the code for how to retrive images from database using handler class.

Posted by Mahesh Babu Aug 26, 2011

i want to use the 3tier to fetch the image and also use a handler ??

Posted by sultan kadanyo Aug 16, 2011
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor