Save image to database using asp net

  • دعنا نشارك هذا :
ترتيب الدرس : 169
المدة : 00:16:54
التحكم :
Text version of the video
http://csharp-video-tutorials.blogspot.com/2015/08/save-image-to-database-using-aspnet.html

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.
https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation1

Slides
http://csharp-video-tutorials.blogspot.com/2015/08/save-image-to-database-using-aspnet_11.html

All ASP .NET Text Articles
http://csharp-video-tutorials.blogspot.com/p/free-aspnet-video-tutorial.html

All ASP .NET Slides
http://csharp-video-tutorials.blogspot.com/p/aspnet-slides.html

ASP.NET Playlist
https://www.youtube.com/playlist?listPL4cyC4G0M1RQcB4IYS_zwkyBwMyx5AnDM

All Dot Net and SQL Server Tutorials in English
https://www.youtube.com/user/kudvenkat/playlists?view1&sortdd

All Dot Net and SQL Server Tutorials in Arabic
https://www.youtube.com/c/KudvenkatArabic/playlists

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 save images to database table using asp.net and 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

Step 7 : Copy and paste the following code in WebForm1.aspx.cs

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

protected void btnUpload_Click(object sender, EventArgs e)
{
HttpPostedFile postedFileFileUpload1.PostedFile;
string filenamePath.GetFileName(postedFile.FileName);
string fileExtensionPath.GetExtension(filename);
int fileSizepostedFile.ContentLength;

if (fileExtension.ToLower()".jpg"fileExtension.ToLower()".gif"
fileExtension.ToLower()".png"fileExtension.ToLower()".bmp")
{
Stream streampostedFile.InputStream;
BinaryReader binaryReadernew BinaryReader(stream);
Byte[] bytesbinaryReader.ReadBytes((int)stream.Length);


string csConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection connew SqlConnection(cs))
{
SqlCommand cmdnew SqlCommand("spUploadImage", con);
cmd.CommandTypeCommandType.StoredProcedure;

SqlParameter paramNamenew SqlParameter()
{
[email protected]"Name",
Valuefilename
};
cmd.Parameters.Add(paramName);

SqlParameter paramSizenew SqlParameter()
{
ParameterName"@Size",
ValuefileSize
};
cmd.Parameters.Add(paramSize);

SqlParameter paramImageDatanew SqlParameter()
{
ParameterName"@ImageData",
Valuebytes
};
cmd.Parameters.Add(paramImageData);

SqlParameter paramNewIdnew SqlParameter()
{
ParameterName"@NewId",
Value-1,
DirectionParameterDirection.Output
};
cmd.Parameters.Add(paramNewId);

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

lblMessage.Visibletrue;
lblMessage.ForeColorSystem.Drawing.Color.Green;
lblMessage.Text"Upload Successful";
hyperlink.Visibletrue;
hyperlink.NavigateUrl"~/WebForm2.aspx?Id" +
cmd.Parameters["@NewId"].Value.ToString();
}
}
else
{
lblMessage.Visibletrue;
lblMessage.ForeColorSystem.Drawing.Color.Red;
lblMessage.Text"Only images (.jpg, .png, .gif and .bmp) can be uploaded";
hyperlink.Visiblefalse;
}
}
}

In our next video we will discuss how to download an image from the database and display it on the web page.

سيتم تفعيل التعليقات قريبا

y
Sep 23, 2020 07:32:27
youtube