Friday, October 30, 2009

How to retrive the images in asp.net from database

Here i am posting my code for retrive the image from database.
In Aspx page i have Image control....
asp:image id="Image1" runat="server" height="120px" width="120px"

In Code Behind, here i am posting my code which was used in my project....
getPatient(long id)
{
string sSql;
string sPath;string conStr = WebConfigurationManager.ConnectionStrings["AttHospital"].ToString();
con = new SqlConnection(conStr);
con.Open();
sSql = "SELECT tbl_DocStaffRegMaster.FullName, CONVERT(VARCHAR(11), tbl_PatientsMaster.Pat_DateOfBirth)As Date, tbl_PatientsMaster.Pat_Photo, tbl_PatientsMaster.Pat_FullName, tbl_PatientsMaster.Pat_Address, tbl_PatientsMaster.Pat_City, tbl_PatientsMaster.Pat_State, tbl_PatientsMaster.Pat_Country, tbl_PatientsMaster.Pat_ZipCode, tbl_PatientsMaster.Pat_Phone, tbl_PatientsMaster.Pat_MobileNo, tbl_PatientsMaster.Pat_Email, tbl_PatientsMaster.DiseaseId, tbl_PatientsMaster.Pat_Employer, tbl_PatientsMaster.Pat_Occupation, tbl_PatientsMaster.Pat_Notes, tbl_PatientsMaster.InsuranceType, tbl_PatientsMaster.InsuranceNo, tbl_PatientsMaster.Pat_FileId
FROM tbl_PatientsMaster, tbl_DocStaffRegMaster WHERE tbl_PatientsMaster.DrSId=tbl_DocStaffRegMaster.DrSId and PatientId=" + id + ";";
cmd = new SqlCommand(sSql, con);
dr = cmd.ExecuteReader();
try
{
if (dr.Read())
{
lblFullName.Text = Convert.ToString(dr[3]);
lblAddress.Text = Convert.ToString(dr[4]);
lblCity.Text = Convert.ToString(dr[5]);
lblState.Text = Convert.ToString(dr[6]);
lblCountry.Text = Convert.ToString(dr[7]);
lblMobile.Text = Convert.ToString(dr[10]);
lblEmail.Text = Convert.ToString(dr[11]);
lblFileNo.Text = Convert.ToString(dr[18]);
Image1.ImageUrl = "~/ShowImage.ashx?id=" + id;
}
else
{
}
}catch (Exception ex)
{}
finally
{con.Close();}
}

You need to add web service as follows:I give name of this sevice ShowImage.ashx

using System;using System.Configuration;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
public class ShowImage : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
try
{
Int32 PatientId;
if (context.Request.QueryString["id"] != null)PatientId = Convert.ToInt32(context.Request.QueryString["id"]);
elsethrow new ArgumentException("No parameter specified");context.Response.ContentType = "image/jpeg";Stream strm = ShowEmpImage(PatientId);
byte[] buffer = new byte[4096];
int byteSeq = strm.Read(buffer, 0, 4096);
while (byteSeq > 0){context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 4096);
}
}
catch (Exception ex)
{
}
}
public Stream ShowEmpImage(int PatientId)
{
string conn = ConfigurationManager.ConnectionStrings["AttHospital"].ConnectionString;
SqlConnection connection = new SqlConnection(conn);
string sql = "SELECT Pat_Photo FROM tbl_PatientsMaster WHERE PatientId = @ID";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandType = CommandType.Text;cmd.Parameters.AddWithValue("@ID", PatientId);
connection.Open();
object img = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])img);
}
catch
{
return null;
}
finally
{
connection.Close();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}

This code will helps you all....
Thanks
SmadyMakesSimple

Thursday, October 15, 2009

My First Blog

Hello all,
I am smady first time makes a blogs... Here i will share my knowlege with you all.... And  Here i can share my knowledge of in programming and software development...