<%@Page Language="VB" Inherits="BaseClass" Src="stockmasters.vb" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Register TagPrefix="Hersh" TagName="nav" Src="nav.ascx" %>
<html>
<script language="VB" runat="server">
Sub Grid1_Update(sender As Object, e As DataGridCommandEventArgs)
	dim sql as string
	Dim code_display  As String 
	Dim rate As String 
	Dim uom As String 
	Dim opening As String 
	Dim closing As String 
	Dim myTextBox    As TextBox
	'This is the key value :
	'Retrieved from the DataKey, since it's a read only field
	Dim code_value as string   = Grid1.DataKeys.Item(E.Item.ItemIndex).ToString
	myTextBox = E.Item.FindControl("edit_name")
	code_display = mytextbox.text
	myTextBox = E.Item.FindControl("edit_rate")
	rate = mytextbox.text
	myTextBox = E.Item.FindControl("edit_uom")
	uom = mytextbox.text
	myTextBox = E.Item.FindControl("edit_opening")
	opening = mytextbox.text
	myTextBox = E.Item.FindControl("edit_closing")
	closing = mytextbox.text
	'Now execute stored procedure 
	sql = "Execute p_Stock_master @code_value =" 
	sql = sql+code_value+", @code_display = '"+code_display+"',@rate=" 
	sql = sql+rate+", @uom='"+uom +"' ,@opening ="+opening+",@closing="+closing
	RunSql(sql)
End Sub
Sub add_click(Source As Object, E As EventArgs) 
	dim sql as string  
	if acode_display.text = ""  then
		response.write("Incomplete information")
		exit sub
	end if
	SQL = "Execute p_stock_master @code_value=NULL,@code_display='" 
	SQL = SQL+ acode_display.text + "',@rate=" 
	SQL = SQL+arate.Text+",@uom="+auom.text+",@opening="+aopening.text 
	RunSql(sql) 
	'reset values
	acode_display.text = ""
	aopening.text  = ""
	arate.text = ""
	auom.text = ""
	hidePanel()
End Sub
Sub add_show(Source As Object, E As EventArgs) 
	 AddPanel.visible = true
end sub
Sub Grid1_delete(sender As Object, e As DataGridCommandEventArgs)
	Dim code_value as string   = Grid1.DataKeys.Item(E.Item.ItemIndex).ToString
	Dim sql as string
	sql = "Delete from stock_master where code_value = " + cstr(code_value)
	RunSql(sql)
	
end sub
Sub MyDataGrid_Page(sender As Object, e As DataGridPageChangedEventArgs)
	Grid1.CurrentPageIndex = e.NewPageIndex
	ReBind
End Sub
Sub MyDataGrid_Sort(sender As Object, e As DataGridSortCommandEventArgs)
	 SortField = e.SortExpression
	ReBind
End Sub
</script>
<head>
	<style>
	a	{	color:black;
			text-decoration:none;}
	a:hover	{	color:red;
			text-decoration:underline;}
	</style>
</head>
<body style="font: 10pt verdana; background-color:ivory">
<asp:ValidationSummary runat=server
headertext="There were errors on the page:" />
<form runat="server">
<!---- Navigation Start-------------->
  <Hersh:nav id="menu" runat = server 
  vGridlines = Both
  vBorderColor = "Black"
  vCellPadding = 7 />
<!---- Navigation Ends-------------->
<h3><font face="Verdana">Inventory Masters </font></h3>
<asp:Label id="Message" runat="server"/>
<asp:Button id="Addshow" text="Add Account" onclick="add_show" runat="server" />
<table width="95%">
<tr>
<td valign="top">
<asp:DataGrid id="Grid1" runat="server"
  AutoGenerateColumns="false"
  BackColor="White"
  BorderWidth="1px" BorderStyle="Solid" BorderColor="Tan"
  CellPadding="2" CellSpacing="0"
  Font-Name="Verdana" Font-Size="8pt"
  OnEditCommand="Grid1_Edit"
  OnCancelCommand="Grid1_Cancel"
  OnUpdateCommand="Grid1_Update"
  OnDeleteCommand = "Grid1_delete"
  DataKeyField="code_value"
  AllowPaging="True"
  PageSize="20"
  PagerStyle-Mode="NumericPages"
  PagerStyle-HorizontalAlign="Right"
  PagerStyle-NextPageText="Next"
  PagerStyle-PrevPageText="Prev"
  OnPageIndexChanged="MyDataGrid_Page"
  AllowSorting="true"
  OnSortCommand="MyDataGrid_Sort"
        >
  <Columns>
  <asp:EditCommandColumn
  EditText="Edit"
  CancelText="Cancel"
  UpdateText="OK"
  ItemStyle-Wrap="false"
  HeaderText="Edit"
  HeaderStyle-Wrap="false"/>
  <asp:ButtonColumn Text="Delete" CommandName="Delete" HeaderText="Delete"/>
  <asp:BoundColumn HeaderText="Account #"  ReadOnly="true" DataField="code_value" SortExpression="code_value" />
  <asp:TemplateColumn HeaderText="Name" SortExpression="code_display">
  <ItemTemplate>
  <asp:Label Text='<%# Container.DataItem("code_display") %>' runat="server"/>
  </ItemTemplate>
  <EditItemTemplate>
  <asp:RequiredFieldValidator runat=server 
   controltovalidate=edit_Name
   errormessage="Name is required.">*
   </asp:RequiredFieldValidator>
   <asp:TextBox id="edit_name" Text='<%# Container.DataItem("code_display") %>'
    runat="server"/>
   </EditItemTemplate>
   </asp:TemplateColumn>
  <asp:TemplateColumn HeaderText="Rate" SortExpression="rate"   >
  <ItemTemplate>
  <asp:Label Text='<%# Container.DataItem("rate") %>' runat="server" />
  </ItemTemplate>
  <EditItemTemplate>
  <asp:TextBox id="edit_rate"   Text='<%# Container.DataItem("rate") %>'
    runat="server"/>
  </EditItemTemplate>
  </asp:TemplateColumn>
  <asp:TemplateColumn HeaderText="uom" SortExpression="uom"   >
    <ItemTemplate>
    <asp:Label Text='<%# Container.DataItem("uom") %>' runat="server"/>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox id="edit_uom"   Text='<%# Container.DataItem("uom") %>'
    runat="server"/>
    </EditItemTemplate>
  </asp:TemplateColumn>
  <asp:TemplateColumn HeaderText="Opening" >
    <ItemTemplate>
    <asp:Label Text='<%# Container.DataItem("opening") %>' runat="server"/>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox id="edit_opening"  Text='<%# Container.DataItem("opening") %>'
    runat="server"/>    
    </EditItemTemplate>
  </asp:TemplateColumn>
  <asp:TemplateColumn HeaderText="Closing" >
    <ItemTemplate>
    <asp:Label Text='<%# Container.DataItem("closing") %>' runat="server"/>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox id="edit_closing" BorderStyle="None" Readonly="True" Text='<%# Container.DataItem("closing") %>'
    runat="server"/>
    </EditItemTemplate>
    </asp:TemplateColumn>
    </Columns>
<HeaderStyle BackColor="Gray" ForeColor="White" Font-Bold="true"/>

<ItemStyle ForeColor="DarkSlateBlue"/>

<AlternatingItemStyle BackColor="Beige"/>

</asp:DataGrid>
</td>
<td valign="top">
<!---- insert row logic-------------->
<asp:Panel id="AddPanel" runat="server" Visible="false">
<table style="font: 8pt verdana">
<tr>
<td colspan="2" bgcolor="#aaaadd" style="font:10pt verdana">Add a New Account:</td>
</tr>
<tr>
   <td nowrap>Name: </td>
   <td><asp:TextBox id="acode_display" runat="server" /></td>
   <td> <asp:RequiredFieldValidator runat=server 
       controltovalidate=acode_display
       errormessage="Name is required.">*
 </asp:RequiredFieldValidator></td>
</tr>
<tr>
  <td nowrap>Rate: </td>
   <td><asp:TextBox id="arate" value = "0" runat="server" /></td>
</tr>
<tr>
  <td nowrap>Unit: </td>
   <td><asp:TextBox id="auom" value = "pcs" runat="server" /></td>
</tr>
  <tr>
   <td nowrap>Opening Value: </td>
   <td><asp:TextBox id="aopening" value = "0" runat="server" /></td>
</tr>
</td>
<td style="padding-top:15">
<asp:Button id="SubmitDetailsBtn" text="Submit" onclick="add_Click" runat="server" />
</td>
</tr>
</tr>
  </table>
</asp:Panel>
 <!-------------Insert Logic ends -------->
</td>
</table>
</form>
</body>
</html>
