Client Side code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PhotoGridview.aspx.cs"
Inherits="WebApplication3.PhotoGridview" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
width: 150px;
}
.auto-style3 {
width: 220px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style1">
<tr>
<td class="auto-style2">
<asp:Label ID="lblimage" runat="server" Text="Please Select an
Image"></asp:Label>
</td>
<td class="auto-style3">
<asp:FileUpload ID="ImageFileUpload" runat="server" />
</td>
<td rowspan="7">
<asp:GridView ID="GridImage" runat="server" AutoGenerateColumns="False"
style="margin-left: 0px">
<Columns>
<asp:TemplateField HeaderText="Images">
<EditItemTemplate>
Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("photo")
%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="70" Width="70"
ImageUrl='<%# Eval("photo") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td class="auto-style3">
<asp:Button ID="btnInsert" runat="server" OnClick="btnInsert_Click"
Text="Insert" />
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td class="auto-style3"> </td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td class="auto-style3"> </td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td class="auto-style3"> </td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td class="auto-style3"> </td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td class="auto-style3"> </td>
</tr>
</table>
</div>
</form>
</body>
</html>
}
Server Side code
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;
using System.Configuration;
namespace WebApplication3
{
public partial class PhotoGridview : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("data source=MDASIFPC\\SQLEXPRESS;database=test;user id=sa;password=sa123");
private void FillPhoto()
Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)
{
SqlDataAdapter da = new SqlDataAdapter("select * from photo", con);
DataSet ds = new DataSet();
da.Fill(ds, "photo");
GridImage.DataSource = ds;
GridImage.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
FillPhoto();
GridImage.Visible = false;
}
}
protected void btnInsert_Click(object sender, EventArgs e)
{
if(ImageFileUpload.HasFile!=false)
{
string fname = ImageFileUpload.FileName;
string fpath = "~//images//" + fname;
ImageFileUpload.SaveAs(Server.MapPath(fpath));
con.Open();
string query = "insert into photo values('" + fpath + "')";
SqlCommand cmd = new SqlCommand(query, con);
int i = cmd.ExecuteNonQuery();
con.Close();
if (i == 1)
{
ClientScript.RegisterStartupScript(this.GetType(), "alert",
"alert('Image is inserted successfully in database')", true);
GridImage.Visible = true;
FillPhoto();
}
}
else
{
GridImage.Visible = false;
ClientScript.RegisterStartupScript(this.GetType(), "alert",
"alert('Please Select any image from source')", true);
}
}
}
}
Output will be like this
Thankyou any doubts please put a comment Thankyou
No comments:
Post a Comment