Show Image from SQL Server in ASP.NET Using C#
SqlConnection objConnection;
SqlCommand objCommand;
SqlDataAdapter objAdapter;
DataSet objDS=new DataSet();
using (objConnection = new SqlConnection(“server=sujitkumar\\sqlexpress;database=livechat;uid=sa;pwd=1234″))
{
objConnection.Open();
using (objCommand = new SqlCommand())
{
objCommand.Connection = objConnection;
objCommand.CommandType = CommandType.Text;
objCommand.CommandText = “select img_online from image_master”;
objAdapter = new SqlDataAdapter(objCommand);
objAdapter.Fill(objDS, “CustomerAttributes”);
}
objConnection.Close();
}
MemoryStream stream = new MemoryStream();
byte[] image = (byte[])objDS.Tables[0].Rows[0][0];
Response.ContentType = “image/jpeg”;
Response.Expires = 0; Response.Buffer =true;
Response.Clear();
Response.BinaryWrite(image);
Response.End();
The above code will write image on the page.
You can show this image in any image button or image control like: