<a href="#" onclick="MyWindow=window.open('https://www.aspsnippets.com/Articles/660/Display-image-from-database-in-Image-control-without-using-Generic-Handler-in-ASPNet/','MyWindow','width=600,height=300'); ">click to open new window</a>
Friday, August 15, 2025
How to view the image in browser from database - binary image from database - asp.net
string alert1 = "var image = new Image();";
alert1+= "image.src='data:image/jpg;base64,"+ Convert.ToBase64String(bytes)+"';";
alert1 += "var w = window.open('');";
alert1+= "w.document.write(image.outerHTML);";
ScriptManager.RegisterStartupScript(this, this.GetType(), "OpenNewTabScript", alert1, true);
ref:-https://stackoverflow.com/questions/27798126/how-to-open-the-newly-created-image-in-a-new-tab
Thursday, August 14, 2025
System.InvalidOperationException: ExecuteScalar: Connection property has not been initialized.
This error comes when you write below code:-
using (SqlCommand cmd=new SqlCommand("SELECT TOP 1 [FileID] FROM [UploadFiles] order by Fileid desc"))
and not passed con as second parameter in SqlCommand. Use below code
using (SqlCommand cmd=new SqlCommand("SELECT TOP 1 [FileID] FROM [UploadFiles] order by Fileid desc", con))
also you have to add below line
cmd.CommandType = System.Data.CommandType.Text;
How to display binary images from database - How to view images from database - view binary images - asp.net c# - How to download file from database
protected void FetchImage(object sender, EventArgs e)
{
string id = ddlImages.SelectedItem.Value;
Image1.Visible = id != "0";
if (id != "0")
{
byte[] bytes = (byte[])GetData("SELECT Data FROM tblFiles WHERE Id =" + id).Rows[0]["Data"];
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
Image1.ImageUrl = "data:image/png;base64," + base64String;
}
}