Database:
Design in database:- Column Name Data Type Allow Null
galleryid int untick
gallerytitle nvarchar(100) untick
galleryname nvarchar(100) untick
Step 1: AddImage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddImage.aspx.cs" Inherits="AddImage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
Image Title: <asp:TextBox ID="txtimagetitle" runat="server"></asp:TextBox><br /><br />
Upload Image: <asp:FileUpload ID="uploadimage" runat="server" /><br /><br />
<asp:Button ID="btnimagesave" runat="server" Text="Add Image" OnClick="btnimagesave_Click" />
<asp:Button ID="btnupdate" runat="server" Text="update" Visible="false" OnClick="btnupdate_Click" />
<asp:Label ID="lblmsg" runat="server" Visible="false" ></asp:Label>
<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" CssClass="add-gallery">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img width="100%" height="40%" src='Addgallery/<%# Eval("GalleryName") %>'>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
...................................................................................................................................................................
Step 2: Create a class file in app-code.
Class name is : galleryclass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
/// <summary>
/// Summary description for galleryclass
/// </summary>
public class galleryclass
{
public galleryclass()
{
//
// TODO: Add constructor logic here
//
}
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["testname"].ToString());
bool result = false;
public void Addimageingallery( string imagetitle, string imagename)
{
con.Open();
string query = "insert into galleryadd (gallerytitle,galleryname) values(@gallerytitle,@galleryname)";
SqlCommand cmd = new SqlCommand(query,con);
cmd.Parameters.AddWithValue("gallerytitle", imagetitle);
cmd.Parameters.AddWithValue("galleryname", imagename);
cmd.ExecuteNonQuery();
con.Close();
}
public DataTable Bindimage()
{
string query = "select * from galleryadd";
SqlDataAdapter da = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public bool deleteimage(string imgid)
{
con.Open();
string query = "Delete from galleryadd where galleryid='" + imgid + "'";
SqlCommand cmd = new SqlCommand(query, con);
int r = cmd.ExecuteNonQuery();
if (r > 0)
{
result = true;
}
return result;
}
// ..............................................................................
public void updateimage_ingridview(string imagetitle, string imagename, string id)
{
con.Open();
string query = "update galleryadd set gallerytitle=@gallerytitle, galleryname=@galleryname where galleryid=@galleryid ";
SqlCommand cmd = new SqlCommand(query,con);
cmd.Parameters.AddWithValue("gallerytitle",imagetitle);
cmd.Parameters.AddWithValue("galleryname", imagename);
cmd.Parameters.AddWithValue("galleryid", id);
cmd.ExecuteNonQuery();
con.Close();
}
public DataTable Update_image_bind(string update_id)
{
string query = "select galleryname from galleryadd where galleryid='"+ update_id + "' ";
SqlDataAdapter da = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
}
...................................................................................
Step 3: AddImage.aspx.cs
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;
using System.Resources;
using System.IO;
public partial class AddImage : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["testname"].ToString());
galleryclass objgallery = new galleryclass();
string filename;
protected void Page_Load(object sender, EventArgs e)
{
if(Convert.ToUInt32 (Request.QueryString["id"])!= 0)
{
btnupdate.Visible = true;
getdata();
up_bind();
}
else
{
btnimagesave.Visible = true;
}
}
protected void btnimagesave_Click(object sender, EventArgs e)
{
if(uploadimage.HasFile)
{
// path name space system.io;
filename = Path.GetFileName(uploadimage.PostedFile.FileName);
uploadimage.PostedFile.SaveAs(Server.MapPath("~/Addgallery/")+ filename);
}
string uploadfile = (string)filename;
objgallery.Addimageingallery(txtimagetitle.Text, uploadfile);
lblmsg.Visible = true;
lblmsg.Text = "Image Added Successfully";
txtimagetitle.Text = "";
}
protected void btnupdate_Click(object sender, EventArgs e)
{
if (uploadimage.HasFile)
{
// path name space system.io;
filename = Path.GetFileName(uploadimage.PostedFile.FileName);
uploadimage.PostedFile.SaveAs(Server.MapPath("~/Addgallery/") + filename);
}
string uploadfile = (string)filename;
objgallery.updateimage_ingridview(txtimagetitle.Text, uploadfile,Request.QueryString["id"].ToString());
lblmsg.Visible = true;
lblmsg.Text = "Image update Successfully";
txtimagetitle.Text = "";
up_bind();
}
public void getdata()
{
con.Open();
string query = "select gallerytitle from galleryadd where galleryid= '"+ Request.QueryString["Id"].ToString() + "' ";
SqlDataAdapter da = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();
da.Fill(dt);
if(dt.Rows.Count > 0)
{
txtimagetitle.Text = dt.Rows[0]["gallerytitle"].ToString();
}
con.Close();
}
public void up_bind()
{
DataTable dt = new DataTable();
dt = objgallery.Update_image_bind(Request.QueryString["id"].ToString());
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
.................................................................................................................................................................
Step 4 Imageview.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Imageview.aspx.cs" Inherits="Imageview" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GvGallery" DataKeyNames="galleryid" OnRowDeleting="GvGallery_RowDeleting" runat="server" AutoGenerateColumns="false" CellPadding="4">
<Columns>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:ImageButton ID="lnkBtnEdit" PostBackUrl='<%#"~/AddImage.aspx?id="+Eval("galleryid")%>' runat="server" Text="Edit" ToolTip="Edit Record" ImageUrl="~/Admin_panel/ad/Gallery/GalleryImage/edit.png" />
<asp:ImageButton ID="lnlBtnDelete" runat="server" Text="Delete" ToolTip="Delete Record" ImageUrl="~/Admin_panel/ad/Gallery/GalleryImage/delete.png" CommandName="Delete" OnClientClick="return confirm('Are You Sure? Do You Want to Delete The Record?');" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="GalleryTitle">
<ItemTemplate>
<%#Eval("gallerytitle") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<img width="50%" height="40%" src="Addgallery/<%#Eval("galleryname") %>">
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
.................................................................................................................................................................
Step 5 Imageview.aspx.cs
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;
public partial class Imageview : System.Web.UI.Page
{
galleryclass objimg = new galleryclass();
DataTable dt = new DataTable();
bool result = false;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
imagebindgridview();
}
}
public void imagebindgridview()
{
dt = objimg.Bindimage();
GvGallery.DataSource = dt;
GvGallery.DataBind();
}
protected void GvGallery_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string Id = e.Keys["galleryid"].ToString();
result = objimg.deleteimage(Id);
if (result)
{
Response.Write("delete");
}
else
{
Response.Write("failed");
}
imagebindgridview();
}
}
No comments:
Post a Comment