Save image to database using asp net

preview_player
Показать описание
Text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

Slides

All ASP .NET Text Articles

All ASP .NET Slides

ASP.NET Playlist

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic

Tags
how to upload image in database table
sql server save image to database c#
how to save image to sql server database using c#

In this video we will discuss how to upload the image to the database. In our next video we will discuss how to download the image from the database and display it on the web page.
Create table tblImages. This table stores the uploaded images along with it's name and size in bytes.
Create table tblImages
(
Id int primary key identity,
Name nvarchar(255),
Size int,
ImageData varbinary(max)
)
Go

public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblMessage.Visible = false;
hyperlink.Visible = false;
}
}

protected void btnUpload_Click(object sender, EventArgs e)
{
HttpPostedFile postedFile = FileUpload1.PostedFile;
string filename = Path.GetFileName(postedFile.FileName);
string fileExtension = Path.GetExtension(filename);
int fileSize = postedFile.ContentLength;

if (fileExtension.ToLower() == ".jpg" || fileExtension.ToLower() == ".gif"
|| fileExtension.ToLower() == ".png" || fileExtension.ToLower() == ".bmp")
{
Stream stream = postedFile.InputStream;
BinaryReader binaryReader = new BinaryReader(stream);
Byte[] bytes = binaryReader.ReadBytes((int)stream.Length);

string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spUploadImage", con);
cmd.CommandType = CommandType.StoredProcedure;

SqlParameter paramName = new SqlParameter()
{
ParameterName = @"Name",
Value = filename
};
cmd.Parameters.Add(paramName);

SqlParameter paramSize = new SqlParameter()
{
Value = fileSize
};
cmd.Parameters.Add(paramSize);

SqlParameter paramImageData = new SqlParameter()
{
Value = bytes
};
cmd.Parameters.Add(paramImageData);

SqlParameter paramNewId = new SqlParameter()
{
Value = -1,
Direction = ParameterDirection.Output
};
cmd.Parameters.Add(paramNewId);

con.Open();
cmd.ExecuteNonQuery();
con.Close();

lblMessage.Visible = true;
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Text = "Upload Successful";
hyperlink.Visible = true;
}
}
else
{
lblMessage.Visible = true;
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "Only images (.jpg, .png, .gif and .bmp) can be uploaded";
hyperlink.Visible = false;
}
}
}

In our next video we will discuss how to download an image from the database and display it on the web page.
Рекомендации по теме
Комментарии
Автор

Thank you sir, this video was perfect!

jezzyfizzo
Автор

Hi Kudvenkat I followed the series (169, 170 and 171) of insert image in database render the images on image label as well as in grid view
you did an outstanding job. By any chance have you did a similar video about how to edit/update the same varbinary images
from the database? Again thanks for your help

guycamu
Автор

Dude you're AWESOME! Your videos are always perfectly explained and I always get what I need from them. Keep up the good work.

osamaissa
Автор

short and precise, with all the necessary information in it. thank you so much !

Eve
Автор

Sir, I am a great fan of you. All  of your tutorials are very nice and easy to learn. Sir can you please upload full series video tutorial of 3- tier architecture in asp, net

binoyroy
Автор

Very nice tutorial. It gave me a lot of confidence to move ahead

optimistms
Автор

I can't thank you enough Sir, thanks a lot & may God bless you!

johnsakala
Автор

Thanks, i have learnt a lot from your tutorials.

Mlegendaryone
Автор

Thanks alot sir! I have learned so much from you. Just one question, why use "Path.GetFileName" when u can just use "postedFile.Name" as the fileName?

TheYTviewer
Автор

thank you very much venkat sir, & god bless you !

love u sir...

muhammadrehbarsheikh
Автор

Thank you sir, that is that what I need to finish my school project

orlandoromo
Автор

Very helpful Video. I appreciate your work. Thankyou

ghayurhaider
Автор

Hello sir,
Can you please upload a video on how to play video files in asp.net webforms application.
I have tried many ways.
But always loved the way what u teach.
So please can u upload a video on this topic.

nasirahmed
Автор

thank you kind sir you just saved my ass from failing college

grin
Автор

GRACIAS POR


Era lo que buscaba :)

Dacelirb
Автор

really amazing job well done and thank you very much

shahzadhafeez
Автор

thank you sir..
so are we coming back to forms ? and dose the jquery tutorial finished "it was grate i dont know what left coz you covered all the nice stuff there " ? what about the mvc ?
Thanks again your amazing sir

davidespada
Автор

this is my error canyou help me

"Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query."}

norzazamak
Автор

would you do one on PDF's / Word documents that would be great. Either way thank for for the videos you do

troybryantIII
Автор

Thanks for the great explanation! Would this still work with storing image to filestreams in MSSQL?

GG_Booboo