C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ASP.NET WebForms »

Save image in Sql Server


Posted Date: 04 Jan 2009    Resource Type: Code Snippets    Category: ASP.NET WebForms
Author: DharmarajMember Level: Diamond    
Rating: 1 out of 5Points: 12



//create table as shown

USE [MyDB]
GO
/****** Object: Table [dbo].[Person] Script Date: 01/05/2009 01:34:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Person](
[PersonID] [int] IDENTITY(1,1) NOT NULL,
[PersonEmail] [varchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[PersonName] [varchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[PersonSex] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[PersonDOB] [datetime] NULL,
[PersonImage] [image] NULL,
[PersonImageType] [varchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO
SET ANSI_PADDING OFF

//create stored procedure with parameters

CREATE PROC sp_person_isp
@PersonEmail varchar(255),
@PersonName varchar(255),
@PersonSex char(1),
@PersonDOB datetime,
@PersonImage image,
@PersonImageType varchar(255)
AS
BEGIN
Insert into person
(PersonEmail,PersonName,PersonSex,
PersonDOB,PersonImage,PersonImageType)
Values
(@PersonEmail,@PersonName,@PersonSex,
@PersonDOB,@PersonImage,@PersonImageType)
END


//add this code in the button click

public void AddPerson(object sender, EventArgs e)
{
int imageSize = 0;
string imageType = string.Empty;
Stream ImgStream;

//get the size of the image
imageSize = PersonImage.PostedFile.ContentLength;
//get the image type
imageType = PersonImage.PostedFile.ContentType;
//read the image
ImgStream = PersonImage.PostedFile.InputStream;
byte[] ImageContent = new byte[imageSize + 1];
int intStatus = 0;
intStatus = ImgStream.Read(ImageContent, 0, imageSize);
//create an instance of the connection
SqlConnection mycon = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand myCommand = new SqlCommand("sp_person_isp", mycon);
//set the command type to stored procedure
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add("@PersonEmail", SqlDbType.VarChar, 255);
myCommand.Parameters["@PersonEmail"].Value = txtPersonMail.Text;
myCommand.Parameters.Add("@PersonName", SqlDbType.VarChar, 255);
myCommand.Parameters["@PersonName"].Value = txtPersonName.Text;
myCommand.Parameters.Add("@PersonSex", SqlDbType.Char, 1);
if (sexMale.Checked)
{
myCommand.Parameters["@PersonSex"].Value ="M";
}
else
{
myCommand.Parameters["@PersonSex"].Value = "F";
}
myCommand.Parameters.Add("@PersonDOB", SqlDbType.DateTime);
myCommand.Parameters["@PersonDOB"].Value = Convert.ToDateTime(txPersonDOB.Text);
myCommand.Parameters.Add("@PersonImage", SqlDbType.Image);
myCommand.Parameters["@PersonImage"].Value = ImageContent;
myCommand.Parameters.Add("@PersonImageType", SqlDbType.VarChar,255);
myCommand.Parameters["@PersonImageType"].Value = imageType;
try
{
mycon.Open();
myCommand.ExecuteNonQuery();
mycon.Close();
Response.Write("Person added successfully");
}
catch (Exception ex)
{
Response.Write("Error Details"+ ex.Message.ToString());
}
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Maintaining scroll position on post back
Previous Resource: SMTP Server
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET WebForms


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use