Monday, January 30, 2012

Level 3 -Class Using to Gridview Editing

3_INVOICINGAPPLICATIONWITHACLASS\INVOICING\INVOICE.CS

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

namespace Invoicing

{

public class clsInvoice

{

private string _InvoiceReference;

private string _InvoiceComments;

private DateTime _InvoiceDate;

private string _ProductName;

private string _ProductDescription;

private int _Qty;

private double _Amount;

private double _PaidAmount;

private double _TaxAmount;

private string _CustomerName;

private string _CustomerAddress;

private int _UnitCost;

public string InvoiceReference

{

set

{

if (value.Length == 0)

{

throw new Exception("Invoice number is compulsory");

}

_InvoiceReference = value;

}

get

{

return _InvoiceReference;

}

}

public string InvoiceComments

{

set

{

_InvoiceComments = value;

}

get

{

return _InvoiceComments;

}

}

public DateTime InvoiceDate

{

set

{

_InvoiceDate = value;

}

get

{

return _InvoiceDate;

}

}

public string ProductName

{

set

{

if (value.Length == 0)

{

throw new Exception("Product name is compulsory");

}

_ProductName = value;

}

get

{

return _ProductName;

}

}

public string ProductDescription

{

set

{

_ProductDescription = value;

}

get

{

return _ProductDescription;

}

}

public int Quantity

{

set

{

if (value < 1)

{

throw new Exception("Quantity should be numeric");

}

_Qty = value;

}

get

{

return _Qty;

}

}

public double Amount

{

set

{

_Amount = value;

}

get

{

return _Amount;

}

}

public double PaidAmount

{

set

{

_PaidAmount = value;

}

get

{

return _PaidAmount;

}

}

public double TaxAmount

{

set

{

_TaxAmount = value;

}

get

{

return _TaxAmount;

}

}

public string CustomerName

{

set

{

if (value.Length == 0)

{

throw new Exception("Customer Name is compulsory");

}

_CustomerName = value;

}

get

{

return _CustomerName;

}

}

public string CustomerAddress

{

set

{

_CustomerAddress = value;

}

get

{

return _CustomerAddress;

}

}

public int UnitCost

{

set

{

if (value == 0)

{

throw new Exception("Unit cost is compulsory");

}

_UnitCost = value;

}

get

{

return _UnitCost;

}

}

public void Insert()

{

SqlConnection ObjConnection = new SqlConnection();

ObjConnection.ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];

ObjConnection.Open();

SqlCommand objCommand = new SqlCommand();

objCommand.Connection = ObjConnection;

objCommand.CommandText = "Insert into invoice(InvoiceReference,InvoiceComments,InvoiceDate,ProductName,Description,Qty,Amount,TaxAmount,PaidAmount,CustomerName,CustomerAddress,UnitCost) values('" + _InvoiceReference + "','" + _InvoiceComments + "','" + _InvoiceDate + "','" + _ProductName + "','" + _ProductDescription + "'," + _Qty.ToString() + "," + _Amount.ToString() + "," + _TaxAmount.ToString() + "," + _PaidAmount.ToString() + ",'" + _CustomerName + "','" + _CustomerAddress + "'," + _UnitCost.ToString() + ")";

objCommand.ExecuteNonQuery();

ObjConnection.Close();

}

public void Update()

{

SqlConnection ObjConnection = new SqlConnection();

ObjConnection.ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];

ObjConnection.Open();

SqlCommand objCommand = new SqlCommand();

objCommand.Connection = ObjConnection;

objCommand.CommandText = "update invoice set InvoiceComments='" + _InvoiceComments + "',InvoiceDate='" + _InvoiceDate + "',ProductName='" + _ProductName + "',Description='" + _ProductDescription + "',Qty=" + _Qty.ToString() + ",Amount=" + _Amount + ",TaxAmount=" + _TaxAmount + ",PaidAmount=" + _PaidAmount + ",CustomerName='" + _CustomerName + "',CustomerAddress='" + _CustomerAddress + "',UnitCost=" + _UnitCost + "where InvoiceReference='" + _InvoiceReference + "'";

objCommand.ExecuteNonQuery();

}

public void Delete()

{

SqlConnection ObjConnection = new SqlConnection();

ObjConnection.ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];

ObjConnection.Open();

SqlCommand objCommand = new SqlCommand();

objCommand.Connection = ObjConnection;

objCommand.CommandText = "delete from invoice where InvoiceReference='" + _InvoiceReference + "'";

objCommand.ExecuteNonQuery();

}

public DataSet getInvoice()

{

SqlConnection ObjConnection = new SqlConnection();

ObjConnection.ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];

ObjConnection.Open();

DataSet objdataset = new DataSet();

SqlDataAdapter objDataAdapter = new SqlDataAdapter("Select * from invoice", ObjConnection);

objDataAdapter.Fill(objdataset);

ObjConnection.Close();

return objdataset;

}

public void LoadInvoice(string InvoiceNumber)

{

SqlConnection ObjConnection = new SqlConnection();

ObjConnection.ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];

ObjConnection.Open();

DataSet objdataset = new DataSet();

SqlDataAdapter objDataAdapter = new SqlDataAdapter("Select * from invoice where InvoiceReference='" + InvoiceNumber + "'", ObjConnection);

objDataAdapter.Fill(objdataset);

_CustomerName = objdataset.Tables[0].Rows[0]["Customername"].ToString();

_CustomerAddress = objdataset.Tables[0].Rows[0]["CustomerAddress"].ToString();

_Amount = Convert.ToInt16(objdataset.Tables[0].Rows[0]["Amount"]);

_PaidAmount = Convert.ToDouble(objdataset.Tables[0].Rows[0]["PaidAmount"]);

_InvoiceDate = Convert.ToDateTime(objdataset.Tables[0].Rows[0]["InvoiceDate"]);

_InvoiceComments = objdataset.Tables[0].Rows[0]["InvoiceComments"].ToString();

_InvoiceReference = objdataset.Tables[0].Rows[0]["InvoiceReference"].ToString();

_ProductName = objdataset.Tables[0].Rows[0]["ProductName"].ToString();

_ProductDescription = objdataset.Tables[0].Rows[0]["Description"].ToString();

_Qty = Convert.ToInt16(objdataset.Tables[0].Rows[0]["Qty"]);

_TaxAmount = Convert.ToDouble(objdataset.Tables[0].Rows[0]["TaxAmount"]);

_UnitCost = Convert.ToInt16(objdataset.Tables[0].Rows[0]["UnitCost"]);

}

}

}


3_InvoicingApplicationWithaClass\InvoiceEntry.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="InvoiceEntry.aspx.cs" Inherits="InvoiceEntry" %>

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">

.style2

{

text-align: center;

}

.style3

{

color: #663300;

font-weight: bold;

}

style>

head>

<body>

<div class="style2">

<span class="style3" lang="en-us">Simple Invoicing Applicationspan>

div>

<form id="form1" runat="server">

<div>

<asp:Label ID="Label1" runat="server" Text="Customer Name">asp:Label>

<asp:TextBox ID="txtCustomerName" runat="server">asp:TextBox>

<asp:Label ID="Label2" runat="server" Text="Customer Address">asp:Label>

<asp:TextBox ID="txtCustomerAddress" runat="server" TextMode="MultiLine">asp:TextBox>

<asp:Label ID="Label3" runat="server" Text="Invoice Number">asp:Label>

<asp:TextBox ID="txtInvoiceNumber" runat="server">asp:TextBox>

<br />

<br />

<br />

<asp:Label ID="Label4" runat="server" Text="Invoice Date">asp:Label>

<span lang="en-us">

<asp:TextBox ID="txtInvoiceDate" runat="server">asp:TextBox>

<asp:Label ID="Label5" runat="server" Text="Product Name">asp:Label>

<asp:TextBox ID="txtProductName" runat="server">asp:TextBox>

<asp:Label ID="Label6" runat="server" Text="Product Description">asp:Label>

<asp:TextBox ID="txtProductDescription" runat="server">asp:TextBox>

<br />

<br />

<asp:Label ID="Label7" runat="server" Text="Unit Cost">asp:Label>

<asp:TextBox ID="txtUnitCost" runat="server">asp:TextBox>

<asp:Label ID="Label8" runat="server" Text="Quantity">asp:Label>

<asp:TextBox ID="TxtQuantity" runat="server">asp:TextBox>

<asp:Label ID="Label9" runat="server" Text="Total Amount to be paid">asp:Label>

<asp:TextBox ID="txtAmountToBePaid" runat="server">asp:TextBox>

<br />

<br />

<asp:Label ID="Label10" runat="server" Text="Total Amount paid">asp:Label>

<asp:TextBox ID="txtAmountPaid" runat="server">asp:TextBox>

<asp:Label ID="Label12" runat="server" Text="Tax Amount">asp:Label>

<asp:TextBox ID="txtTaxAmount" runat="server">asp:TextBox>

<asp:Label ID="Label11" runat="server" Text="Comments">asp:Label>

<asp:TextBox ID="txtComments" runat="server" TextMode="MultiLine">asp:TextBox>

<br />

<asp:Button ID="btnAddInvoice" runat="server" Text="Add Invoice"

style="height: 26px" onclick="btnAddInvoice_Click" />

<asp:Button ID="btnUpdateInvoice" runat="server" Text="update Invoice" onclick="btnUpdateInvoice_Click"

/>

<asp:Button ID="btnDeleteInvoice" runat="server" Text="Delete Invoice" onclick="btnDeleteInvoice_Click"

/>

span>

div>

<asp:DataGrid ID="dtgInvoice" runat="server"

onselectedindexchanged="dtgInvoice_SelectedIndexChanged" BackColor="White"

BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"

ForeColor="Black" GridLines="Vertical">

<FooterStyle BackColor="#CCCC99" />

<SelectedItemStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />

<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right"

Mode="NumericPages" />

<AlternatingItemStyle BackColor="White" />

<ItemStyle BackColor="#F7F7DE" />

<Columns>

<asp:ButtonColumn CommandName="Select" Text="Select">asp:ButtonColumn>

Columns>

<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />

asp:DataGrid>

<asp:Label ID="lblErrorMessage" runat="server" ForeColor="Red" Text="Label">asp:Label>

form>

body>

html>

3_INVOICINGAPPLICATIONWITHACLASS\InvoiceEntry.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.SqlClient;

using System.Data;

using Invoicing;

public partial class InvoiceEntry : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

LoadGrid();

}

private void LoadGrid()

{

// load the data set in to the data grid

clsInvoice objInvoice = new clsInvoice();

dtgInvoice.DataSource = objInvoice.getInvoice();

dtgInvoice.DataBind();

}

public void clearText()

{

txtInvoiceNumber.Text = "";

txtComments.Text = "";

txtInvoiceDate.Text = "";

txtProductName.Text = "";

txtProductDescription.Text = "";

TxtQuantity.Text = "";

txtAmountToBePaid.Text = "";

txtTaxAmount.Text = "";

txtAmountPaid.Text = "";

txtCustomerName.Text = "";

txtCustomerAddress.Text = "";

txtUnitCost.Text = "";

}

protected void btnAddInvoice_Click(object sender, EventArgs e)

{

try

{

clsInvoice objInvoice = new clsInvoice();

setObjectFromUI(objInvoice);

objInvoice.Insert();

LoadGrid();

clearText();

}

catch (Exception ex)

{

lblErrorMessage.Text = ex.Message.ToString();

}

}

public void setObjectFromUI(clsInvoice objInvoice)

{

try

{

objInvoice.CustomerName = txtCustomerName.Text;

objInvoice.CustomerAddress = txtCustomerAddress.Text;

objInvoice.Amount = Convert.ToDouble(txtAmountToBePaid.Text);

objInvoice.PaidAmount = Convert.ToDouble(txtAmountPaid.Text);

objInvoice.InvoiceDate = Convert.ToDateTime(txtInvoiceDate.Text);

objInvoice.InvoiceComments = txtComments.Text;

objInvoice.InvoiceReference = txtInvoiceNumber.Text;

objInvoice.ProductName = txtProductName.Text;

objInvoice.ProductDescription = txtProductDescription.Text;

objInvoice.Quantity = Convert.ToInt16(TxtQuantity.Text);

objInvoice.TaxAmount = Convert.ToDouble(txtTaxAmount.Text);

objInvoice.UnitCost = Convert.ToInt16(txtUnitCost.Text);

}

catch (Exception ex)

{

throw ex;

}

}

public void setUIfromObject(clsInvoice objInvoice)

{

txtCustomerName.Text = objInvoice.CustomerName;

txtCustomerAddress.Text = objInvoice.CustomerAddress;

txtAmountToBePaid.Text = objInvoice.Amount.ToString();

txtAmountPaid.Text = objInvoice.PaidAmount.ToString();

txtInvoiceDate.Text = objInvoice.InvoiceDate.ToString();

txtComments.Text = objInvoice.InvoiceComments.ToString();

txtInvoiceNumber.Text = objInvoice.InvoiceReference.ToString();

txtProductName.Text = objInvoice.ProductName.ToString();

txtProductDescription.Text = objInvoice.ProductDescription.ToString();

TxtQuantity.Text = objInvoice.Quantity.ToString();

txtTaxAmount.Text = objInvoice.TaxAmount.ToString();

txtUnitCost.Text = objInvoice.UnitCost.ToString();

}

protected void btnUpdateInvoice_Click(object sender, EventArgs e)

{

clsInvoice objInvoice = new clsInvoice();

try

{

setObjectFromUI(objInvoice);

objInvoice.Update();

clearText();

LoadGrid();

}

catch (Exception ex)

{

lblErrorMessage.Text = ex.Message.ToString();

}

}

protected void btnDeleteInvoice_Click(object sender, EventArgs e)

{

clsInvoice objInvoice = new clsInvoice();

setObjectFromUI(objInvoice);

objInvoice.Delete();

clearText();

LoadGrid();

}

protected void dtgInvoice_SelectedIndexChanged(object sender, EventArgs e)

{

clsInvoice objInvoice = new clsInvoice();

objInvoice.LoadInvoice(dtgInvoice.SelectedItem.Cells[1].Text.ToString());

setUIfromObject(objInvoice);

}

}

No comments: