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 » Images »

Image uploading to database


Posted Date: 22 Aug 2008    Resource Type: Code Snippets    Category: Images
Author: amit chaudharyMember Level: Gold    
Rating: 1 out of 5Points: 7



This is the method for uploading images into database.
on design page add: a fileupload control, a label and a button.

on code page:

declare min. and max size:
protected int MinSizeAllowed = 500;
protected int MaxSizeAllowed = 5242880; // image size up to 5 MB

on button click:-

Int32 imageSize;
string imageType;
string imgname;
Stream imageStream;
imageSize = fileImgUpload.PostedFile.ContentLength;
imageType = fileImgUpload.PostedFile.ContentType;
imageStream = fileImgUpload.PostedFile.InputStream;
imgname = fileImgUpload.PostedFile.FileName;
byte[] imageContent = new byte[imageSize];
if (imageContent.Length < MaxSizeAllowed && imageContent.Length > MinSizeAllowed)
{
int intStatus;
intStatus = imageStream.Read(imageContent, 0, imageSize);
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
SqlCommand myCommand = new SqlCommand("insert into photo(img_title,img_stream,img_type,img_name) values(@img_title,@img_stream,@img_type,@img_name)", myConnection);
myCommand.CommandType = CommandType.Text;
SqlParameter img_title = new SqlParameter("@img_title", SqlDbType.VarChar, 50);
img_title.Value = TextBox9.Text;
myCommand.Parameters.Add(img_title);
SqlParameter img_stream = new SqlParameter("@img_stream", SqlDbType.Image);
img_stream.Value = imageContent;
myCommand.Parameters.Add(img_stream);
SqlParameter img_Type = new SqlParameter("@img_type", SqlDbType.VarChar, 50);
img_Type.Value = imageType;
myCommand.Parameters.Add(img_Type);
SqlParameter img_name = new SqlParameter("@img_name", SqlDbType.VarChar, 50);
img_name.Value = imgname;
myCommand.Parameters.Add(img_name);
Label1.Text = "Thanks for joining Photo Contest.";
try
{
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
}
catch (SqlException SQLexc)
{
Response.Write("Insert Failure. Error Details : " + SQLexc.ToString());
}
}
else
{
Label1.Text = "Image should be less than 5 MB";
}
}




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.
Images into database  .  Image upload to database  .  

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: Image Comparison in C#
Previous Resource: Images upload to server
Return to Discussion Resource Index
Post New Resource
Category: Images


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use