Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how i can store an image and text data in sqlserver database and retrive it and display the output in a gridview in asp using c#....plz help me to find out the solution
Posted

1.Convert the image from file strean to byte array.

2.Declare the field in Database of type image

3. write the byte array to database

4. To display image from database, convert the byte array from database to stream and keep it in a file and give the path of the file.
 
Share this answer
 
You need to decide how do you want to save image. I mean whether you want to save the path of image? or want to save the whole image in SQL in binary format.

I personally feel to save the image in filesystem and save the path only in DB buit it depends on your requirement. If you have less amount of data then you can go for DB as well. Please refer the link below

Click here
 
Share this answer
 
Ref:
GridViewImages from DB in ASP.NET using C#[^]

Just go through it, u'll get solution.
Don't forget to mark as answer if it helps. :)
 
Share this answer
 
SQL
USE [Souvik]
GO

/****** Object:  Table [dbo].[Test]    Script Date: 09/04/2012 14:37:35 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Test](
    [Sln] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NULL,
    [Address] [varchar](50) NULL,
    [Photo] [image] NULL,
 CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED
(
    [Sln] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


//////////////////////////////////////////////////////////////////////////
Store Procedure
----------------
USE [Souvik]
GO
/****** Object: StoredProcedure [dbo].[sp_Insert_Into_Test] Script Date: 09/04/2012 14:38:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <author,,name>
-- Create date: <create>
-- Description: <description,,>
-- =============================================
ALTER PROCEDURE [dbo].[sp_Insert_Into_Test]
-- Add the parameters for the stored procedure here
@Name varchar(50),
@Address varchar(50),
@Photo image
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
insert into Test(Name,Address,Photo) values(@Name,@address,@Photo)
END
///////////////////////////////////////////////////////////////////////////////
Asp.net
-------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;

public partial class Test : System.Web.UI.Page
{
string str = "Data Source=SOUVIK-PC;Initial Catalog=Souvik;User ID=sa;Password=123456";



protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnUploadData_Click(object sender, EventArgs e)
{
try
{

SqlConnection con = new SqlConnection(str);
SqlCommand com = new SqlCommand("sp_Insert_Into_Test", con);

com.CommandType = CommandType.StoredProcedure;
com.Parameters.Add("@Name", SqlDbType.VarChar).Value = txtName.Text;
com.Parameters.Add("@Address", SqlDbType.VarChar).Value = txtAddress.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);
SqlParameter UploadedImage = new SqlParameter("@Photo", SqlDbType.Image, imageSize.Length);
UploadedImage.Value = imageSize;
com.Parameters.Add(UploadedImage);
}
}


con.Open();
com.ExecuteNonQuery();
Label4.Text = "Inserted";

con.Close();
}
catch (Exception ex)
{
}
}

}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900