Monday, August 29, 2011

Insert Images Into Data Base



For table Creation and Connection.cs class File Refer Previous Post

In Default.aspx.cs file
--------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
public partial class UI_ImagesUpload : System.Web.UI.Page
{
DashBoard obj = new DashBoard();

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridData();
}
}
protected void btn_SaveImage_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
//getting length of uploaded file
int length = FileUpload1.PostedFile.ContentLength;
//create a byte array to store the binary image data
byte[] imgbyte = new byte[length];
//store the currently selected file in memeory
HttpPostedFile img = FileUpload1.PostedFile;
//set the binary data
img.InputStream.Read(imgbyte, 0, length);
string productName = txt_ImageName.Text;
obj.con.Close();
obj.con.Open();
obj.cmd = new SqlCommand("INSERT INTO tbl_ModuleImages (ImageName,Image) VALUES (@imagename,@imagedata)", obj.con);
obj.cmd.Parameters.Add("@imagename", SqlDbType.VarChar, 50).Value = txt_ImageName.Text;
obj.cmd.Parameters.Add("@imagedata", SqlDbType.Image).Value = imgbyte;
int count = obj.cmd.ExecuteNonQuery();
if (count == 1)
{
BindGridData();
txt_ImageName.Text = string.Empty;
ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + txt_ImageName.Text + " image inserted successfully')", true);
}
else
{
lblMessage.Text = "Please Select Product Image File";
}
}


}
private void BindGridData()
{
//Connection obj = new Connection();
obj.con.Close();
obj.con.Open();
obj.cmd = new SqlCommand("Select * from tbl_ModuleImages", obj.con);
obj.da = new SqlDataAdapter(obj.cmd);
obj.da.Fill(obj.ds, "asdd");
DataList1.DataSource = obj.ds;
DataList1.DataBind();
obj.con.Close();
}
protected void btn_UpdateImage_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile == true)
{
int imagelength = FileUpload1.PostedFile.ContentLength;
Byte[] imagebytes = new byte[imagelength];
HttpPostedFile image = FileUpload1.PostedFile;
image.InputStream.Read(imagebytes, 0, imagelength);

string ModuleId=txt_ImageName.Text;
obj.con.Close();
obj.con.Open();
obj.cmd = new SqlCommand("Update tbl_Modules set ModuleImage=@imagedata where Moduleid='"+ModuleId+"'",obj.con);
obj.cmd.Parameters.Add("@ModuleID", SqlDbType.VarChar, 50).Value = txt_ImageName.Text;
obj.cmd.Parameters.Add("@imagedata", SqlDbType.Image).Value = imagebytes;
obj.cmd.ExecuteNonQuery();
ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + txt_ImageName.Text + " Image Updated successfully')", true);
txt_ImageName.Text = "";
obj.con.Close();
}
}
}



in Handler.ashx
=================================================

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

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;


public class Handler : IHttpHandler
{
Connection obj = new Connection();
public void ProcessRequest (HttpContext context)
{
obj.con.Close();
obj.con.Open();
obj.cmd = new SqlCommand("Select ImageName,Image from tbl_ModuleImages where ImageID =@ImageID",obj.con);
obj.cmd.CommandType = CommandType.Text;
SqlParameter ImageID = new SqlParameter("@ImageID", System.Data.SqlDbType.Int);
ImageID.Value = context.Request.QueryString["ImageID"];
obj.cmd.Parameters.Add(ImageID);
obj.dr=obj.cmd.ExecuteReader();
obj.dr.Read();
context.Response.BinaryWrite((Byte[])obj.dr["Image"]);
obj.dr.Close();
obj.con.Close();
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
}
}


------------------------


In Default .aspx HTML Source code


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ImagesUpload.aspx.cs" Inherits="UI_ImagesUpload" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Image UpLoads</title>
<style type="text/css">
.style1
{
width: 100%;
float: left;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<table class="style1">
<tr>
<td>
ImageName</td>
<td>
<asp:TextBox ID="txt_ImageName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Select Image</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
</tr>
<tr>
<td>
&nbsp;</td>
<td>
<asp:Button ID="btn_SaveImage" runat="server" onclick="btn_SaveImage_Click"
Text="Save & Display" />
<asp:Button ID="btn_UpdateImage" runat="server" Text="Update_Image"
onclick="btn_UpdateImage_Click" />
</td>
</tr>
<tr>
<td>
&nbsp;</td>
<td>
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</td>
</tr>
</table>
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3"
RepeatDirection="Horizontal">
<ItemTemplate>
<asp:Image ID="img2" runat="server" Height="150px"
ImageUrl='<%#"Handler.ashx?ImageId="+ Eval("ImageID") %>' Width="200px" />
<br />
<asp:Label ID="lb4" runat="server" Text='<%# Eval("ImageName") %>'></asp:Label>
<br />
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ImageId") %>'></asp:Label>
<br />

</ItemTemplate>
</asp:DataList>
</form>
</body

------------------------


Out Put
--------------





No comments: