Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello,

I want to insert photo into database...i have used datatype image for the photo column
this is the code i used to insert to databse..i

C#
protected void Btn_Save_Click(object sender, EventArgs e)
      {
        objSqlData.SetSQLCommandParameterAddWithValue("@Photo", SqlDbType.Image);
      // procedure
      }

here is my stored procedure
SQL
ALTER procedure Insert
as 
begin 
@FirstName nvarchar(50),
@LastName nvarchar(50),
@Photo image

INSERT INTO Employee 
(FirstName, LastName,photo)
VALUES (@FirstName , @LastName,@Photo)
end   


Thank you
Posted
Updated 31-May-15 21:12pm
v8
Comments
Tomas Takac 1-Jun-15 2:53am    
The code is clearly not complete. Show the stored proc too.

C#
//getting length of uploaded file
int length = fileuploadImage.PostedFile.ContentLength;
//create a byte array to store the binary image data
byte[] imgbyte = new byte[length];
//store the currently selected file in memeory
HttpPostedFile img = fileuploadImage.PostedFile;
//fileuploadImage is the name of my file uploder
//set the binary data
img.InputStream.Read(imgbyte, 0, length);

//please convert your image to byte array


cmd.Parameters.Add("@Photo", SqlDbType.Image).Value = imgbyte;
//cmd is a object of sql command class

int count = cmd.ExecuteNonQuery();

//This code will work for asp.net
//if u r working in windows application then let me know 
 
Share this answer
 
Comments
Member 11148509 1-Jun-15 4:03am    
Im using Radasyncupload...can u tell me how to do in that ....i just want to store to database... im able to select image and also see preview of that ..
Repace fileuploadImage with your fileuploder id in code;

If it is not working for u
then post all the code inside the button click.
 
Share this answer
 
Comments
Member 11148509 1-Jun-15 4:22am    
can u tell me why this error occurs????error is coming near sp ....
sasanka sekhar panda 1-Jun-15 6:26am    
please share your full code so that i can help you
Member 11148509 1-Jun-15 6:38am    
this is the code for preview of the image and the code which i have shared in the starting for inserting into database ..im using radaschyncupload and radbinary image..





protected void Page_Load(object sender, EventArgs e)
{
RadAsyncUpload.FileUploaded += new Telerik.Web.UI.FileUploadedEventHandler(RadAsyncUpload_FileUploaded);

Radbtn_Upload.Click += new EventHandler(RadBtn_Upload_Click);
}

protected void RadAsyncUpload_FileUploaded(object sender, FileUploadedEventArgs e)
{
Bitmap bitmapImage = ResizeImage(RadAsyncUpload.UploadedFiles[0].InputStream);
System.IO.MemoryStream stream = new System.IO.MemoryStream();
bitmapImage.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
RadBinaryImage.DataValue = stream.ToArray();

}
public Bitmap ResizeImage(Stream stream)
{
System.Drawing.Image originalImage = Bitmap.FromStream(stream);

int height = 100;
int width = 100;

Bitmap scaledImage = new Bitmap(width, height);

using (Graphics g = Graphics.FromImage(scaledImage))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(originalImage, 0, 0, width, height);
//g.DrawString("My photo from " + RadComboBox2.SelectedItem.Text + ", " + RadComboBox1.SelectedItem.Text, new Font("Tahoma", 18), Brushes.White, new PointF(0, 0));
return scaledImage;
}
is this a window application??
 
Share this answer
 
Comments
Member 11148509 1-Jun-15 6:51am    
its a Web application...Asp is used
sasanka sekhar panda 1-Jun-15 6:54am    
which database u r using??
Member 11148509 1-Jun-15 6:57am    
sql
please read this article..it may help u..

Click me[^]
 
Share this answer
 
Comments
Member 11148509 1-Jun-15 7:34am    
This is onl for fileupload...im using ajax controls ...fileupload does not work in ajax controls....let me know why this errors comes so that i can proceed
sasanka sekhar panda 1-Jun-15 7:42am    
objSqlData.SetSQLCommandParameterAddWithValue("@Photo", SqlDbType.Image);

here you are passing the sp parameter name and type...show me the code where you are assigning value to this @Photo parameter.. @Photo parameter you have to assign a byte[]...
Member 11148509 1-Jun-15 7:46am    
i have pasted the code when u asked thats the code i have used...
Member 11148509 1-Jun-15 7:48am    
radasyncupload doesnot have hasfile property

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