364,088 Members | 5440 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Storing of image in DB using open file dialog box

rajasekar s
P: 3
I want to store an image from PC to ms sql db , I want the details of the coding from the first step. From getting the image to converting to bytes and then storing, Can anyone please provide me the coding part of it..
Feb 9 '12 #1

✓ answered by Newface

RAJASEKAR S,I too came across this issue and got it resolved in the following way
1.datatype of the image column-image
2.in aspx.cs page
HttpPostedFile uploadedImage=null;
byte[] imageData=null;
if (cmdBrowsePhoto.PostedFile != null)
{
imageData = new byte[cmdBrowsePhoto.PostedFile.ContentLength];
uploadedImage = cmdBrowsePhoto.PostedFile;
uploadedImage.InputStream.Read(imageData, 0, (int)cmdBrowsePhoto.PostedFile.ContentLength);
}
and in parameter:
cmd.Parameters.Add(new SqlParameter("@photo", imageData));
Please do let me know if it is workin for you or not!
Share this Question
Share on Google+
5 Replies


PsychoCoder
Expert Mod 100+
P: 208
While I personally don't like the practice of storing actual images in the database (way too much overhead, and if SQL goes down then you cannot display your images) but that's your decision.

Given that I think this may help
Feb 9 '12 #2

rajasekar s
P: 3
Hi , thanks. But can i get the c # coding..
And also If storing in Db is overload , Then which is the best way to do it.
Feb 9 '12 #3

PsychoCoder
Expert Mod 100+
P: 208
First, I'm not going to convert the code to C# for you, you will learn nothing.

Best way, if this is an ASP.NET application then store the images in a folder and the path to the image in your database.
Feb 9 '12 #4

rajasekar s
P: 3
K sir, Thanks for your reply, I ll learn the C # coding part on my own. Thanks a lot.
Feb 9 '12 #5

Newface
P: 16
RAJASEKAR S,I too came across this issue and got it resolved in the following way
1.datatype of the image column-image
2.in aspx.cs page
HttpPostedFile uploadedImage=null;
byte[] imageData=null;
if (cmdBrowsePhoto.PostedFile != null)
{
imageData = new byte[cmdBrowsePhoto.PostedFile.ContentLength];
uploadedImage = cmdBrowsePhoto.PostedFile;
uploadedImage.InputStream.Read(imageData, 0, (int)cmdBrowsePhoto.PostedFile.ContentLength);
}
and in parameter:
cmd.Parameters.Add(new SqlParameter("@photo", imageData));
Please do let me know if it is workin for you or not!
Feb 10 '12 #6

Post your reply

Help answer this question



Didn't find the answer to your .NET Framework question?

You can also browse similar questions: .NET Framework