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"; } }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|