Thursday 9 February 2012

SHOW AND STORE IMAGE IN ASP.NET



Showing Image From Data Base Using Handler

<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public class Handler : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
string query = "Select Image from  Table_Image WHERE Name = '" + context.Request.QueryString["Name"].ToString() +"'" ;
SqlConnection connection = new SqlConnection("Type your Connection String Here");
SqlCommand command = new SqlCommand(query, connection );
connection .Open();
byte[] img = (byte[])command.ExecuteScalar();
context.Response.BinaryWrite(img);      
connection.Close();
}
public bool IsReusable
{
get
{
return false;
}
}
}
After making a handler as above. Set Image URL like this ::
Image1.ImageUrl = "Handler.ashx?Name=" + TextBox1.Text;

How To Store Image In Database In Asp.Net

byte[] imgbyte = FileUpload1.FileBytes;
SqlConnection connection = new SqlConnection("Type Connection String Here");
SqlCommand command = new SqlCommand("INSERT INTO Table_Image(Name,Image) VALUES (@name,@image)", connection );
connection .Open();
command .Parameters.AddWithValue("@name", TextBox1.Text);
SqlParameter img = new SqlParameter("@image", SqlDbType.Image, imgbyte.Length);
img.Value = imgbyte;
command .Parameters.Add(img);
command .ExecuteNonQuery();
connection .Close();






No comments:

Post a Comment