Load image from database in 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
c# load image from sql database

Step 1 : Create stored procedure to retrieve image by Id.
Create procedure spGetImageById
@Id int
as
Begin
Select ImageData
End
Go

<asp:Image ID="Image1" Height="500px" Width="500px" runat="server" />

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace Demo
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spGetImageById", con);
cmd.CommandType = CommandType.StoredProcedure;

SqlParameter paramId = new SqlParameter()
{
Value = Request.QueryString["Id"]
};
cmd.Parameters.Add(paramId);

con.Open();
byte[] bytes = (byte[])cmd.ExecuteScalar();
string strBase64 = Convert.ToBase64String(bytes);
Image1.ImageUrl = "data:Image/png;base64," + strBase64;
}
}
}
}
Рекомендации по теме
Комментарии
Автор

This was a brilliant tutorial! I did things a little different as I wanted an upload-able profile picture for my website, plus I wanted my DB calls to come from an external C# file to clean up the code-behind. I worked off the User_ID instead of the Context Identity, done an UP-SERT for the profile picture, as to not retain old images and bog down the DB and worked perfectly :D Am very happy!

TechEreb
Автор

Another excellent lesson(169 & 170). Thank you!

CodingSquid
Автор

Thank you so much! I was searching this for hours!

lynceiV
Автор

Thanks Alot. You are very good. Appreciate your work. it helped me accurately.

ghayurhaider
Автор

Many thanks Venkat, I learned a lot from you

raedabugharbeiah
Автор

I like it, do you have with jquery!? thanks !!!

jiorcho
Автор

Why did you convert the bytes array into base64 string?

TheHobbyvideos
Автор

I would like to display the image which is stored in the database on the html page itself, rather than have a link to it which directs a user to a new page.
Kindly help how it can be achieved?

shockingvideos
Автор

I wonder why inline DateURI not working as my image is not showing up. Neither Microsoft Edge  nor Google browser showing up the image. Is there any way to resolve the issue?

andyb
Автор

Do you you have Paypal integration on webstore in asp.net?

arifulislamkuet
Автор

on page webform2.aspx.cs
this line is showing me an error
byte[] bytes= (byte[])cmd.ExecuteScalar();

Error:-
Unable to cast object of type 'System.String' to type 'System.Byte[]'.
Please tell me how can i fix this and help me out

pulkitagrawal
Автор

How to upload and retrieve it from Oracle SQL Database?

univerzalnistudent
Автор

i have tried this code bt picture is nt displaying in browser can anyone help me

shikhatiwari
Автор

you there. I've a query i need answered

faraznisar
Автор

שמעון עמר שולח לכם הודעה חשובה:

אופטה גופטה🇮🇳

amitoshrat
join shbcf.ru