In the same way add another class library and name it to BAL and add reference of DAL to BAL and write following code in it,
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using SCHEMA;
- using DAL;
- using System.Data;
- namespace BAL {
- public class Class1 {
- public int Insert(SCHEMA.Class1 objSchema) {
- try {
- DAL.Class1 objDAL = new DAL.Class1();
- return objDAL.InsertData(objSchema);
- } catch (Exception ex) {
- throw ex;
- }
- }
- public DataTable BindGrid() {
- try {
- DAL.Class1 objDAL = new DAL.Class1();
- return objDAL.BindGrid();
- } catch (Exception ex) {
- throw ex;
- }
- }
- public DataTable GetById(int Id) {
- try {
- DAL.Class1 objDAL = new DAL.Class1();
- return objDAL.GetById(Id);
- } catch (Exception ex) {
- throw ex;
- }
- }
- public int Update(SCHEMA.Class1 objSchema, int Id) {
- try {
- DAL.Class1 objDAL = new DAL.Class1();
- return objDAL.UpdateData(objSchema, Id);
- } catch (Exception ex) {
- throw ex;
- }
- }
- public int Delete(int Id) {
- try {
- DAL.Class1 objDAL = new DAL.Class1();
- return objDAL.DeleteData(Id);
- } catch (Exception ex) {
- throw ex;
- }
- }
- public DataTable BindImages() {
- try {
- DAL.Class1 objDAL = new DAL.Class1();
- return objDAL.BindImages();
- } catch (Exception ex) {
- throw ex;
- }
- }
- }
- }
Now right click on project in solution explorer and add a webform and name it to webform1.aspx. This WebForm is for Inserting, Updating and Deleting Images.
On design side write the following code:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ThreeTier.WebForm1" %>
- <!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>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <table>
- <tr>
- <td>
- <asp:Label ID = "lblName" runat="server" Text="Enter Image Name"></asp:Label>
- </td>
- <td>
- <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td>
- <asp:Label ID = "lblImage" runat="server" Text="UploadImage"></asp:Label>
- </td>
- <td>
- <asp:FileUpload ID="imgUpload" runat="server" />
- <asp:HiddenField ID="hdnImage" runat="server" />
- </td>
- </tr>
- <tr>
- <td>
- <asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
- </td>
- </tr>
- <tr>
- <td>
- <asp:GridView ID="gdImages" runat="server" AutoGenerateColumns="False"
- AllowPaging="true" PageSize = 5
- CssClass="Gridview t_view" HeaderStyle-BackColor="#61A6F8" ShowFooter="True"
- HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White"
- onpageindexchanging="gdImages_PageIndexChanging"
- onrowdeleting="gdImages_RowDeleting"
- onselectedindexchanging="gdImages_SelectedIndexChanging"
- onrowcommand="gdImages_RowCommand">
- <Columns>
- <asp:BoundField DataField="Id" HeaderText="Id"/>
- <asp:BoundField DataField="Name" HeaderText="Name"/>
- <asp:TemplateField HeaderText="Images">
- <ItemTemplate>
- <a target="_blank" id="lnk" runat="server" href='<%# string.Format("~/Site/Upload/Images/{0}", Eval("Path"))%>'><%#Eval("Path")%>
- </a>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:CommandField ButtonType="Button" SelectText="Edit" ShowSelectButton="True" />
- <asp:TemplateField>
- <ItemTemplate>
- <asp:Button runat="server" ID="btnDelete" OnClientClick="return confirm('Are you sure,you want to delete this record ?');" Text="Delete" CommandArgument='
-
- <%# Eval("Id") %>' CommandName="Delete" />
- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
- <HeaderStyle BackColor="#61A6F8" Font-Bold="True" ForeColor="White"></HeaderStyle>
- </asp:GridView>
- </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
- </html>
In cs side write the following code,add reference of DAL, Schema and BAL in the project.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.IO;
- using SCHEMA;
- using BAL;
- using System.Data;
- namespace ThreeTier {
- public partial class WebForm1: System.Web.UI.Page {
- DataTable dt;
- protected void Page_Load(object sender, EventArgs e) {
- if (!IsPostBack) BindGrid();
- }
- private void BindGrid() {
- try {
- BAL.Class1 objBal = new BAL.Class1();
- gdImages.Columns[0].Visible = true;
- gdImages.DataSource = objBal.BindGrid();
- gdImages.DataBind();
- gdImages.Columns[0].Visible = false;
- gdImages.Visible = true;
- } catch (Exception ex) {
- return;
- }
- }
- protected void btnSubmit_Click(object sender, EventArgs e) {
- try {
- if (btnSubmit.Text == "Submit") {
- if (checkBlank()) {
- InsertData();
- }
- } else if (btnSubmit.Text == "Update") {
- int Id = int.Parse(gdImages.Rows[gdImages.SelectedIndex].Cells[0].Text);
- UpdateData(Id);
- }
- } catch (Exception ex) {
- throw ex;
- }
- }
- private void InsertData() {
- string FileName = UploadImages();
- SCHEMA.Class1 objSchema = new SCHEMA.Class1();
- objSchema.Name = txtName.Text;
- if (imgUpload.HasFile) {
- objSchema.Path = FileName;
- } else {
- objSchema.Path = hdnImage.Value;
- }
- BAL.Class1 objBAL = new BAL.Class1();
- int result = objBAL.Insert(objSchema);
- if (result > 0) {
- Showmsg("Image Uploaded Successfully");
- }
- BindGrid();
- Clear();
- }
- public void UpdateData(int Id) {
- string FileName = UploadImages();
- SCHEMA.Class1 objSchema = new SCHEMA.Class1();
- objSchema.Name = txtName.Text;
- if (imgUpload.HasFile) {
- objSchema.Path = FileName;
- } else {
- objSchema.Path = hdnImage.Value;
- }
- BAL.Class1 objBAL = new BAL.Class1();
- int result = objBAL.Update(objSchema, Id);
- if (result > 0) {
- Showmsg("Image Updated Successfully");
- }
- btnSubmit.Text = "Submit";
- BindGrid();
- Clear();
- }
- private void Clear() {
- txtName.Text = "";
- }
- private bool checkBlank() {
- bool flag = true;
- string msg = string.Empty;
- if (!imgUpload.HasFile) {
- Showmsg("Please Upload Image");
- flag = false;
- } else {
- if (!(Path.GetExtension(imgUpload.PostedFile.FileName).Equals(".jpg") || Path.GetExtension(imgUpload.PostedFile.FileName).Equals(".img") || Path.GetExtension(imgUpload.PostedFile.FileName).Equals(".png"))) {
- Showmsg("Please Upload Valid File");
- flag = false;
- }
- }
- return flag;
- }
- private void Showmsg(string Message) {
- ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Alert", "alert('" + Message + "');", true);
- }
- public string UploadImages() {
- string result = string.Empty;
- if (imgUpload.HasFile) {
- string extension = System.IO.Path.GetExtension(imgUpload.PostedFile.FileName);
- if (extension == ".jpg" || extension == ".png" || extension == ".img") {
- result = imgUpload.PostedFile.FileName;
- imgUpload.PostedFile.SaveAs(MapPath("~") + "/Site/Upload/Images/" + result);
- }
- }
- return result;
- }
- protected void gdImages_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) {
- try {
- BAL.Class1 objBAL = new BAL.Class1();
- int Id = int.Parse(gdImages.Rows[e.NewSelectedIndex].Cells[0].Text);
- dt = new DataTable();
- dt = objBAL.GetById(Id);
- if (dt.Rows.Count > 0) {
- txtName.Text = dt.Rows[0]["Name"].ToString();
- hdnImage.Value = dt.Rows[0]["Path"].ToString();
- btnSubmit.Text = "Update";
- }
- } catch (Exception ex) {
- throw ex;
- }
- }
- protected void gdImages_RowDeleting(object sender, GridViewDeleteEventArgs e) {}
- protected void gdImages_PageIndexChanging(object sender, GridViewPageEventArgs e) {
- gdImages.PageIndex = e.NewPageIndex;
- BindGrid();
- }
- protected void gdImages_RowCommand(object sender, GridViewCommandEventArgs e) {
- if (e.CommandName.Equals("Delete")) {
- int Id = int.Parse(e.CommandArgument.ToString());
- DeleteRecord(Id);
- }
- }
- private void DeleteRecord(int Id) {
- try {
- BAL.Class1 objBAL = new BAL.Class1();
- int Result = objBAL.Delete(Id);
- if (Result > 0) {
- Showmsg("Image Deleted Sucessfully");
- }
- BindGrid();
- Clear();
- } catch (Exception ex) {
- throw ex;
- }
- }
- }
- }
Now right click on project in solution explorer and add a webform and name it to webform2.aspx. This WebForm is for displaying images.
Write the following code on design i.e. aspx page:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="ThreeTier.WebForm2" %>
- <!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>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:DataList ID = "dtImages" runat="server" RepeatColumns = "3" RepeatDirection = "Horizontal" Width="50%" BorderColor="#336699" BorderStyle="Solid" BorderWidth="2px">
- <ItemTemplate>
- <asp:Label ID = "lblImages" runat = "server" Text = '<%#Eval("Name")%>'>
- </asp:Label>
- <br />
- <asp:Image ID="imgGallery" runat="server" ImageUrl='
- <%# Bind("Path", "~/Site/Upload/Images/{0}") %>' />
- </ItemTemplate>
- </asp:DataList>
- </div>
- </form>
- </body>
- </html>
And on the cs side write the following code:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace ThreeTier {
- public partial class WebForm2: System.Web.UI.Page {
- protected void Page_Load(object sender, EventArgs e) {
- if (!IsPostBack) {
- BindImages();
- }
- }
- private void BindImages() {
- try {
- BAL.Class1 objBal = new BAL.Class1();
- dtImages.DataSource = objBal.BindImages();
- dtImages.DataBind();
- dtImages.Visible = true;
- } catch (Exception ex) {
- throw ex;
- }
- }
- }
- }
I have attached the project for reference, if there are some issues mention it in the comment section.