Wednesday, 11 December 2013

How to save and retrieve binary format image with database in Asp.Net

How to save and retrieve binary format image with database in Asp.Net

-- execute this query in Sql Server................................

use SqlPractice

create table UserImage(Id int primary key,Image varbinary(max))


Source Code for ShowAndUpload.aspx …………………………….

%@ Page Language="C#" AutoEventWireup="true" CodeFile="ShowAndUpload.aspx.cs" Inherits="ShowAndUpload" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
                 width: 100%;
                  }
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style1">
<tr><td colspan="2"><center><p>How to save and retrive Image with     Database</p></center></td>
</tr>
<tr>
<td>Enter your Id</td>
<td><asp:TextBox ID="txtid" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Upload Image</td>
<td><asp:FileUpload ID="FileUpload1" runat="server" /></td>
</tr>
<tr><td></td>
<td>
<asp:Button ID="btnSaveImage" runat="server" OnClick="btnSaveImage_Click" Text="Save Image" />
<asp:Button ID="btnshowimage" runat="server" OnClick="btnshowimage_Click" Text="Show Image" Width="109px" />
</td>
</tr>
<tr><td></td>
<td><asp:Label ID="lblmessage" runat="server" Text="Label" Visible="False"></asp:Label></td>
</tr>
<tr><td>&nbsp;</td>
<td>
<asp:Image ID="Image1" runat="server" Height="115px" Width="128px" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

Source Code for ShowAndUpload.aspx.cs …………………………….

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ShowAndUpload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
//code for how to save Binary format image in database
protected void btnSaveImage_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection("Data Source=KUSH-PC;Initial Catalog=SqlPractice;User ID=sa;Password=tiwari");
SqlCommand cmd = new SqlCommand("insert into UserImage values(@id,@userimg)", con);
cmd.Parameters.AddWithValue("@id", Convert.ToInt32(txtid.Text));
cmd.Parameters.AddWithValue("@userimg", FileUpload1.FileBytes);
con.Open();
int i = cmd.ExecuteNonQuery();
if (i > 0)
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Green;
lblmessage.Text = "Image save successfully";


}
catch (Exception ex)
{
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Red;
lblmessage.Text = ex.Message;
}

}
// button for retrive image from database
protected void btnshowimage_Click(object sender, EventArgs e)
{
Image1.ImageUrl = "Default.aspx?id=" + txtid.Text;
}
}

Note: Before adding btnshowimage button , please add another web Page in your Web Application with name Default.aspx and write the code given below inside it on Page_Load Event

Source Code for Default.aspx.cs …………………………….

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=KUSH-PC;Initial Catalog=SqlPractice;User ID=sa;Password=tiwari");
SqlCommand com = new SqlCommand("select Image from UserImage where id=" + Request.QueryString["id"].ToString(), con);
con.Open();
SqlDataReader dr = com.ExecuteReader();
dr.Read();
Response.BinaryWrite((byte[])dr[0]);
con.Close();
}
}



1 comments: