Total Pageviews

Tuesday 28 February 2012

How to show/handle Image from Database?

Create a generic handler like
// DBImageHandler.ashx


    <%@ WebHandler Language="C#" Class="DbImageHandler" %>

    using System;
    using System.Web;
    using System.IO;
    public class DbImageHandler : IHttpHandler
    {

    public void ProcessRequest(HttpContext context)
    {
    Int32 id;
    if (context.Request.Params["id"] != null)
        id = Convert.ToInt32(context.Request.Params["id"]);
    else
        throw new ArgumentException("No parameter specified");

    context.Response.ContentType = "image/jpeg";
    Stream strm = getDBImage(id);
    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);
    }

    }


    public Stream getDBImage(int id)
    {
    System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("SELECT photo FROM  Employee WHERE EmployeeID=" + id, System.Configuration.ConfigurationManager.ConnectionStrings["DBConStr"].ConnectionString);
    System.Data.DataTable dt = new System.Data.DataTable();
    da.Fill(dt);
    if (dt.Rows.Count == 0)
        return null;

    return new MemoryStream((byte[])dt.Rows[0]["photo"]);
    }
    public bool IsReusable
    {
    get
    {
        return false;
    }
    }

    }

Now you can use Image link as
DbImageHandler.ashx?id=1

Exmaple :

<img src="DbImageHandler.ashx?id=1" alt="Employee 1 Photo" width="400px" height="400px" />

Saturday 25 February 2012

How to Cross-thread operation

The same control can't be accessed by multiple threads. in that case the following code is helpful
private void Form1_Load(object sender, EventArgs e)
{

    Thread t1 = new Thread(new ParameterizedThreadStart(printThr1));
    Thread t2 = new Thread(printThr2);
    t1.Start("U");
    t2.Start();
}
private void printThr1(object t)
{
           

    for(int i=1;i<600000;i+=2)
    {
        if (txtControl.InvokeRequired)
        {
            txtControl.Invoke(new MethodInvoker(delegate { txtControl.Text += Environment.NewLine + t.ToString()+"= " + i; }));
            Thread.Sleep(500);
        }
    }
}
private void printThr2()
{
           
    for (int i = 0; i < 50000; i += 2)
    {
              
        if (txtControl.InvokeRequired)
        {
            txtControl.Invoke(new MethodInvoker(delegate { txtControl.Text += Environment.NewLine + "N= " + i; }));
            Thread.Sleep(300);
        }

    }
}


Friday 17 February 2012

How to load DLL and call its methods using C#.NET


// Unbox it to correct object
Type type = null;

String AssemblyDir = @"D:\Project\DataAccess.dll";
//AssemblyDir = AssemblyDir.TrimStart("file:\\".ToArray());

 Assembly a = Assembly.LoadFrom(AssemblyDir);
       type = a.GetType("DataAccess.DerivedClass");

   if (type != null)
       {
          BaseClass objBaseClass = (BaseClass)Activator.CreateInstance(type, args);
          resultCode = objBaseClass.MethodName(Parameters);
        }
       else
          {
              throw new Exception("Can't find correct type name or dll!"); // FATAL ERROR. Can't find correct type name or dll.
            }


Where  BaseClass is like
public class BaseClass : ICloneable
{
// this method is override by DerivedClass
 public virtual string MethodName( parameters )
 {
   throw new NotImplementedException();
 }
}
Where  DerivedClass is like
public class DerivedClass : BaseClass
{
public override sealed string MethodName(parameters)
{
// do something with parameters
}
}