Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 20-Nov-11 20:37pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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. Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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:
-- 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)
{
}
}

}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 751
1 George Jonsson 330
2 Garth J Lancaster 290
3 Sergey Alexandrovich Kryukov 274
4 BillWoodruff 252
0 Sergey Alexandrovich Kryukov 10,879
1 OriginalGriff 5,555
2 Maciej Los 5,534
3 CPallini 3,819
4 BillWoodruff 3,594


Advertise | Privacy | Mobile
Web02 | 2.8.141030.1 | Last Updated 4 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100