Save Image Into Database and Show in grid view
Save Image Into Database and Show in Grid View
Create Table: PloiticalParty

Make Interface: TestSaveAndShowGridview.aspx
<%@ Page
Language="C#"
AutoEventWireup="true"
CodeBehind="TestSaveAndShowGridview.aspx.cs"
Inherits="EVSByAli.TestSaveAndShowGridview"
%>
<!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></title>
<style type="text/css">
.style1
{
font-size: xx-large;
}
.style2
{
font-size: xx-large;
color: #660066;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<center>
<table class="style1">
<tr>
<td
class="style2"
colspan="2">
<strong><em>Save and Show Images in from database</em></strong></td>
</tr>
<tr>
<td
class="style2"
colspan="2">
</td>
</tr>
<tr>
<td>
Name</td>
<td>
<asp:TextBox ID="TextBox1"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Abbrevation</td>
<td>
<asp:TextBox ID="TextBox2"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Symbol</td>
<td>
<asp:FileUpload ID="FileUpload1"
runat="server"
/>
</td>
</tr>
<tr>
<td>
In which year it was created</td>
<td>
<asp:TextBox ID="TextBox3"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Leader</td>
<td>
<asp:TextBox ID="TextBox4"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="Button1"
runat="server"
onclick="Button1_Click"
Text="Save"
/>
<asp:Button ID="Button2"
runat="server"
onclick="Button2_Click"
Text="Cancel"
/>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:GridView ID="grdParties"
runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="Name"
DataField="Name"
/>
<asp:BoundField HeaderText="Abbrevation"
DataField="Abbrevation"
/>
<asp:BoundField HeaderText="Year"
DataField="Year"
/>
<asp:BoundField HeaderText="Symbol"
DataField="Symbol"
Visible="false"
/>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "PoliticalPartiesHandler.ashx?Id="+
Eval("Id") %>'
Height="150px"
Width="150px"
/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
</table>
</center>
</div>
</form>
</body>
</html>
Code Source:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace EVSByAli
{
public partial class TestSaveAndShowGridview
: System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
BindGridData();
}
protected void
Button1_Click(object sender, EventArgs e)
{
//Data
Source=.\SQLEXPRESS;AttachDbFilename="c:\users\umer\documents\visual
studio 2010\Projects\EVSByAli\EVSByAli\App_Data\AliEVSDB.mdf";Integrated
Security=True;User Instance=True
string fileName = @"c:\users\umer\documents\visual
studio 2010\Projects\EVSByAli\EVSByAli\App_Data\AliEVSDB.mdf";
string constring = @"Data
Source=.\SQLEXPRESS;AttachDbFilename=" + fileName + ";Integrated Security=True;User Instance=True";
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 query = "insert
into PloiticalParty (Name,Abbrevation,Symbol,year,Leader) VALUES
(@Name,@Abbrevation,@Symbol,@year,@Leader)";
SqlConnection connection = new SqlConnection(constring);
//Connection String
//Open The Connection
connection.Open();
SqlCommand cmd = new
SqlCommand(query, connection);
cmd.Parameters.Add("@Name",
SqlDbType.NVarChar, 50).Value =
TextBox1.Text;
cmd.Parameters.Add("@Abbrevation",
SqlDbType.NVarChar, 50).Value =
TextBox2.Text;
cmd.Parameters.Add("@year",
SqlDbType.NVarChar, 50).Value =
TextBox3.Text;
cmd.Parameters.Add("@Leader",
SqlDbType.NVarChar, 50).Value =
TextBox4.Text;
cmd.Parameters.Add("@Symbol",
SqlDbType.Image).Value = imgbyte;
int count = cmd.ExecuteNonQuery();
//Close The Connection
connection.Close();
if (count == 1)
{
//BindGridData();
ScriptManager.RegisterStartupScript(this, this.GetType(),
"alertmessage", "javascript:alert('Record added successfully')",
true);
//call the method to bind the grid
BindGridData();
}
}
private void
BindGridData()
{
string fileName = @"c:\users\umer\documents\visual
studio 2010\Projects\EVSByAli\EVSByAli\App_Data\AliEVSDB.mdf";
string constring = @"Data
Source=.\SQLEXPRESS;AttachDbFilename=" + fileName + ";Integrated Security=True;User Instance=True";
SqlConnection connection = new SqlConnection(constring);
SqlCommand command = new
SqlCommand("SELECT
* from PloiticalParty ",
connection);
SqlDataAdapter daimages = new
SqlDataAdapter(command);
DataTable dt = new
DataTable();
daimages.Fill(dt);
grdParties.DataSource = dt;
grdParties.DataBind();
}
protected void
Button2_Click(object sender, EventArgs e)
{
Response.Redirect("TestSaveAndShowGridview.aspx");
}
}
}
Comments
Post a Comment