GridView

PurchaseOrder.cs

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

using System.Collections.Generic;

/// <summary>

/// Summary description for PurchaseOrder

/// </summary>

public class PurchaseOrder

{

#region Constructor

public PurchaseOrder()

{

}

public PurchaseOrder(int purchaseordernumber, string companyname, string partnumber, string itemdescription, int quantity, string manufacturer, decimal targetprice, DateTime deliverydate)

{ }

#endregion

#region Declarations

public string testConnectionString = "Data Source=INBLRBOASQLSVR;Initial Catalog=test;Integrated Security=True";

public string SP_ADD_PURCHASEORDER = "SavePurchaseOrder";

public string SP_DELETE_PURCHASEORDER = "DeletePurchaseOrder";

public string SP_GET_ALL_PARTNUMBER_BY_PURCHASEORDER = "GetAllPartNumberByPurchaseOrder";

public string SP_UPDATE_PURCHASEORDER = "UpdatePurchaseOrder";

public string DeletePartNumber = "DeletePartNumber";

/* Private Variables Declaration */

private int _purchaseOrder;

private string _companyName;

private string _partNumber;

private string _itemDescription;

private int _quantity;

private string _manufacturer;

private decimal _targetPrice;

private DateTime _deliveryDate;

#endregion

#region Propeties

 

/* Properties */

public int PurchaseOrderNumber

{

get { return _purchaseOrder; }

set { _purchaseOrder = value; }

}

public string CompanyName

{

get { return _companyName; }

set { _companyName = value; }

}

public string PartNumber

{

get { return _partNumber; }

set { _partNumber = value; }

}

public string ItemDescription

{

get { return _itemDescription; }

set { _itemDescription = value; }

}

public int Quantity

{

get { return _quantity; }

set { _quantity = value; }

}

public string Manufacturer

{

get {return _manufacturer; }

set { _manufacturer = value; }

}

public decimal TargetPrice

{

get { return _targetPrice; }

set { _targetPrice = value; }

}

public DateTime DeliveryDate

{

get { return _deliveryDate; }

set { _deliveryDate = value; }

}

#endregion

#region Methods

public void AddPurchaseOrder(PurchaseOrder pur)

{

try

{

SqlConnection myConnection = new SqlConnection(testConnectionString);

SqlCommand myCommand = new SqlCommand(SP_ADD_PURCHASEORDER, myConnection);

myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@PurchaseOrderNumber",pur.PurchaseOrderNumber);

myCommand.Parameters.AddWithValue("@CompanyName", pur.CompanyName);

myConnection.Open();

myCommand.ExecuteNonQuery();

myConnection.Close();

}

catch (Exception ex)

{

string st = ex.Message;

}

}

 

public void UpdatePurchaseOrder(PurchaseOrder pur)

{

try

{

SqlConnection myConnection = new SqlConnection(testConnectionString);

SqlCommand myCommand = new SqlCommand(SP_UPDATE_PURCHASEORDER, myConnection);

myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@PurchaseOrderNumber", pur.PurchaseOrderNumber);

myCommand.Parameters.AddWithValue("@PartNumber", pur.PartNumber);

myConnection.Open();

myCommand.ExecuteNonQuery();

myConnection.Close();

}

catch (Exception ex)

{

string st = ex.Message;

}

}

public void DeletePurchaseOrder(PurchaseOrder pur)

{

try

{

SqlConnection myConnection = new SqlConnection(testConnectionString);

SqlCommand myCommand = new SqlCommand(SP_DELETE_PURCHASEORDER, myConnection);

myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@PurchaseOrderNumber", pur.PurchaseOrderNumber);

myCommand.Parameters.AddWithValue("@PartNumber", pur.PartNumber);

myConnection.Open();

myCommand.ExecuteNonQuery();

myConnection.Close();

}

catch(Exception ex)

{

string st = ex.Message;

}

}

public List<PurchaseOrder> GetAllPartNumberByPurchaseOrder()

{

List<PurchaseOrder> partList = new List<PurchaseOrder>();

SqlConnection myConnection = new SqlConnection(testConnectionString);

SqlCommand myCommand = new SqlCommand(SP_GET_ALL_PARTNUMBER_BY_PURCHASEORDER, myConnection);

myConnection.Open();

SqlDataReader reader = myCommand.ExecuteReader();

while (reader.Read())

{

PurchaseOrder purchaseorder = new PurchaseOrder((int)reader["PurchaseOrderNumber"], (string)reader["PartNumber"], (string)reader["ItemDescription"], (int)reader["Quantity"], (string)reader["Manufacturer"], (decimal)reader["TargetPrice"], (DateTime)reader["DeliveryDate"]);

partList.Add(purchaseorder);

}

myConnection.Close();

reader.Close();

myCommand.Dispose();

if (partList != null && partList.Count > 0)

return partList;

else return null;

}

#endregion

 

}

 

 

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

<!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>Untitled Page</title>

</head>

<body>

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

<div>

<table>

<tr>

<td style="width:25%"><asp:Label ID="lblPurchaseOrderNumber" runat="server" Text="PurchaseOrderNumber"></asp:Label></td>

<td style="width:25%"><asp:TextBox ID="txtPurchaseOrderNumber" runat="server"></asp:TextBox></td>

<td style="width:25%"><asp:Label ID="lblCompanyName" runat="server" Text="Label"></asp:Label></td>

<td style="width:25%">

<asp:DropDownList ID="drpCompanyName" runat="server">

<asp:ListItem Text="XXXXX & co" Value="1"></asp:ListItem>

<asp:ListItem Text="YYYYY & co" Value="2"></asp:ListItem>

<asp:ListItem Text="ZZZZZ & co" Value="3"></asp:ListItem>

<asp:ListItem Text="AAAAA & co" Value="4"></asp:ListItem>

<asp:ListItem Text="QQQQQ & co" Value="5"></asp:ListItem>

</asp:DropDownList>

</td>

</tr>

<tr>

<td colspan="5">

<!-- Grid View -->

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowEditing="GridView1_RowEditing" DataKeyNames="ID" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting" ShowFooter="True" OnRowCommand="GridView1_RowCommand" DataSourceID="objUser">

<Columns>

<asp:TemplateField HeaderText="">

<FooterTemplate>

<asp:Button ID="Btn_Add1" runat="server" CommandName="AddPurchaseOrder" Text="Add" />

</FooterTemplate>

</asp:TemplateField>

<!-- start of PartNumber -->

<asp:TemplateField HeaderText="PartNumber">

<ItemTemplate>

<asp:Label ID="lblPartNumber" runat="server" Text='<%# Eval("PartNumber") %>'></asp:Label>

</ItemTemplate>

<EditItemTemplate>

<asp:TextBox ID="txtPartNumber" runat="server" Text="<%# Eval("PartNumber") %>"></asp:TextBox>

</EditItemTemplate>

<FooterTemplate>

<asp:TextBox ID="txtAddPartNumber" runat="server"></asp:TextBox>

</FooterTemplate>

</asp:TemplateField>

<!-- End Of PartNumber -->

<!-- start of PartDescription -->

<asp:TemplateField>

<ItemTemplate>

<asp:Label ID="lblPartDescription" runat="server" Text='<%# Eval("PartDescription") %>'></asp:Label>

</ItemTemplate>

<EditItemTemplate>

<asp:TextBox ID="txtPartDescription" runat="server" Text="<%# Eval("PartDescription") %>"></asp:TextBox>

</EditItemTemplate>

<FooterTemplate>

<asp:TextBox ID="txtAddPartDescription" runat="server"></asp:TextBox>

</FooterTemplate>

</asp:TemplateField>

<!-- End of PartDescription -->

<!-- start of Qunatity -->

<asp:TemplateField>

<ItemTemplate>

<asp:Label ID="lblQuantity" runat="server" Text='<%# Eval("Quantity") %>'></asp:Label>

</ItemTemplate>

<EditItemTemplate>

<asp:TextBox ID="txtQuantity" runat="server" Text="<%# Eval("Quantity") %>"></asp:TextBox>

</EditItemTemplate>

<FooterTemplate>

<asp:TextBox ID="txtAddQuantity" runat="server"></asp:TextBox>

</FooterTemplate>

</asp:TemplateField>

<!-- End of Qunatity -->

<!-- Start of Manufacturer -->

<asp:TemplateField>

<ItemTemplate>

<asp:Label ID="lblManufacturer" runat="server" Text='<%# Eval("Manufacturer") %>'></asp:Label>

</ItemTemplate>

<EditItemTemplate>

<asp:TextBox ID="txtManufacturer" runat="server" Text="<%# Eval("Manufacturer") %>"></asp:TextBox>

</EditItemTemplate>

<FooterTemplate>

<asp:TextBox ID="txtAddManufacturer" runat="server"></asp:TextBox>

</FooterTemplate>

</asp:TemplateField>

<!-- End of Manufacturer -->

<!-- Start of Target Price -->

<asp:TemplateField>

<ItemTemplate>

<asp:Label ID="lblTargetPrice" runat="server" Text='<%# Eval("TargetPrice") %>'></asp:Label>

</ItemTemplate>

<EditItemTemplate>

<asp:TextBox ID="txtTargetPrice" runat="server" Text="<%# Eval("TargetPrice") %>"></asp:TextBox>

</EditItemTemplate>

<FooterTemplate>

<asp:TextBox ID="txtAddTargetPrice" runat="server"></asp:TextBox>

</FooterTemplate>

</asp:TemplateField>

<!-- End of Target Price-->

<!-- Start of Delivery Date -->

<asp:TemplateField>

<ItemTemplate>

<asp:Label ID="lblDeliveryDate" runat="server" Text='<%# Eval("DeliveryDate") %>'></asp:Label>

</ItemTemplate>

<EditItemTemplate>

<asp:TextBox ID="txtDeliveryDate" runat="server" Text="<%# Eval("DeliveryDate") %>"></asp:TextBox>

</EditItemTemplate>

<FooterTemplate>

<asp:TextBox ID="txtAddDeliveryDate" runat="server"></asp:TextBox>

</FooterTemplate>

</asp:TemplateField>

<!-- End of Delivery Date -->

<asp:CommandField HeaderText="Edit" ShowEditButton="True" />

<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />

</Columns>

</asp:GridView>

<asp:ObjectDataSource DeleteMethod="DeletePurchaseOrder" InsertMethod="AddPurchaseOrder" ID="objPurchaseOrder" runat="server" SelectMethod="GetAllPartNumberByPurchaseOrder" UpdateMethod="UpdatePurchaseOrder" TypeName="User">

</asp:ObjectDataSource>

</td>

</tr>

</table>

</div>

</form>

</body>

</html>

 

 

 

 

 

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

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

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

{

try

{

if (e.CommandName == "AddPurchaseOrder")

{

string PurchaseOrderNumber = txtPurchaseOrderNumber.Text;

string companyName = drpCompanyName.SelectedItem;

string PartNumber = ((TextBox)GridView1.FooterRow.FindControl("txtAddPartNumber")).Text;

string ItemDescription = ((TextBox)GridView1.FooterRow.FindControl("txtAddPartDescription")).Text;

string Quantity = ((TextBox)GridView1.FooterRow.FindControl("txtAddQuantity")).Text;

string Manufacturer = ((TextBox)GridView1.FooterRow.FindControl("txtAddManufacturer")).Text;

string TargetPrice = ((TextBox)GridView1.FooterRow.FindControl("txtAddTargetPrice")).Text;

string DeliveryDate = ((TextBox)GridView1.FooterRow.FindControl("txtAddDeliveryDate")).Text;

objUser.InsertParameters.Add("purchaseorderno", PurchaseOrderNumber);

objUser.InsertParameters.Add("companyname", companyName);

objUser.InsertParameters.Add("partnumber", PartNumber);

objUser.InsertParameters.Add("itemdescription", ItemDescription);

objUser.InsertParameters.Add("quantity", Quantity);

objUser.InsertParameters.Add("manufacturer", Manufacturer);

objUser.InsertParameters.Add("targetprice", TargetPrice);

objUser.InsertParameters.Add("deliverydate", DeliveryDate);

 

objUser.Insert();

}

}

catch (Exception ex)

{

Response.Write(ex.Message);

}

}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

{

GridView1.EditIndex = e.NewEditIndex;

}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

{

 

int index = e.RowIndex;

int userID = Convert.ToInt32(((Label)GridView1.Rows[index].FindControl("lblUserID")).Text);

objUser.DeleteParameters.Add("id", TypeCode.Int32, userID.ToString());

 

 

 

objUser.Delete();

//BindData();

}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

int userID = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("lblUserID")).Text);

string firstName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFirstName")).Text;

string lastName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtLastName")).Text;

objUser.UpdateParameters.Add("id", TypeCode.Int32, userID.ToString());

objUser.UpdateParameters.Add("firstName", firstName);

objUser.UpdateParameters.Add("lastName", lastName);

objUser.Update();

GridView1.EditIndex = -1;

}

}

Ebook Download
View all
Learn
View all