![](/user_photo/1438_p9ksI.png)
Microsoft ASP .NET Professional Projects - Premier Press
.pdf<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>
<tr>
<td style="padding-top:15">
<asp:Button id="SubmitDetailsBtn" text="Submit" onclick="add_Click" runat="server" />
</td>
</tr>
</table>
</asp:Panel>
<!--------Insert Logic ends ------> </td>
</tr>
</table>
</form>
![](/html/1438/356/html_wpziZPj8IA.ZbBg/htmlconvd-0Ag2qI362x1.jpg)
</body>
</html>
Stock_masters.vb is the Code Behind file.
StockMasters.vb
Option Strict Off
Imports System
Imports System.Collections
Imports System.Text
Imports System.Data
Imports System.Data.OleDb
Imports System.Web.UI
Imports System.Web.UI.WebControls
Public Class BaseClass
Inherits System.Web.UI.Page
Protected Grid1 as DataGrid
Protected Message as label
Protected AddPanel as Panel
Public SortField As String
Sub Page_Load(Source As Object, E As EventArgs)
If NOT (isPostBack)
If SortField = "" Then
SortField = "code_display"
End If rebind
End If
End Sub
Sub ReBind()
Dim ConnStr As String
Dim SQL As String
ConnStr = "Provider=SQLOLEDB; Data Source=(local); " ConnStr = ConnStr+" Initial Catalog=ASPNET;User ID=sa;" Dim t As New NameSpaceHersh.SQLService
Dim ds As DataSet ' Bind Grid
SQL = "Select * from stock_master" ds = t.Populate(ConnStr, SQL)
Dim dv2 As DataView
dv2 = ds.Tables("vTable").DefaultView dv2.Sort = SortField Grid1.DataSource= dv2 Grid1.DataBind()
hidePanel() End Sub
Sub Grid1_Edit(Sender As Object, E As DataGridCommandEventArgs)
Grid1.EditItemIndex = E.Item.ItemIndex
ReBind()
End Sub
Sub Grid1_Cancel(Sender As Object, E As DataGridCommandEventArgs)
Grid1.EditItemIndex = -1
ReBind()
End Sub
Sub hidePanel()
if AddPanel.visible = true then AddPanel.visible = false
End if
End Sub
Sub RunSql(vSQL as string)
Dim ConnStr As String
Dim SQL As String
ConnStr = "Provider=SQLOLEDB; Data Source=(local); "
ConnStr = ConnStr+" Initial Catalog=ASPNET;User ID=sa;"
Dim t As New NameSpaceHersh.SQLService
Dim s As string
s = t.RunSQL(ConnStr,vSQL) Grid1.EditItemIndex = -1
if s <> "Success" then Message.Text = s Message.Style("color") = "red"
End if response.write (vsql) Rebind
End Sub
End Class
Chapter 25: Inventory Movements
The Inventory Management System records movements of inventory items. You can add to the inventory by purchasing stock, by inventory returns from customers, by return of unused materials from the shop floor (in the case of manufacturing units), and so on. You can reduce inventory by sales, issues to shop floor, supplier returns, and so on. The web form that we will build in this chapter will be responsible for recording stock additions and depletions. Each stock movement (addition or depletion) has a "header" entry, which is recorded in the tr_header table and a "detail" entry, which is recorded in the stock_detail table. A DataGrid on the web form lists all available movements and also allows editing of any particular movement. A number of TextBox controls residing on a panel on the form provide the functionality to add stock movements. Both the Insert and the Update modes gather the user inputs and pass them on to the stored procedure p_stock_trans, which encapsulates the insert and update logic.
Inserting and Updating Transactions
The stored procedure p_stock_trans handles both the insert and update logic. It has seven input parameters that are passed to it by the DataGrid or the record addition form. To insert a new inventory movement, you pass a null document number (doc_no) to this procedure and to modify an existing movement, you pass it the document number of the inventory movement.
Stored Procedure p_stock_trans
![](/html/1438/356/html_wpziZPj8IA.ZbBg/htmlconvd-0Ag2qI365x1.jpg)
create procedure p_stock_trans @date datetime ,
@ref varchar(30) = NULL, @qty_in money = 0, @qty_out money =0,
@id char(3),
@doc_no integer = NULL, @narr varchar(150) = NULL, @code_value integer
as /*
call with a null doc_no to insert, a valid doc_no to update Example :
Execute p_stock_trans @date =getdate() , @ref = test1,
@qty_in = 10, @qty_out =0, @id ="STK", @doc_no = NULL,
@narr = "Test Entry" */
DECLARE @ll_doc integer
DECLARE @ret integer
BEGIN TRANSACTION
--To insert a new movement, pass doc_no =NULL to procedure-- IF isnull(@doc_no,0) = 0
BEGIN
--SafeGuard: Check if tranaction with same ref# exists. --If so dont insert
select @ret = count(*) from tr_header where ref = @ref if @ret > 0
BEGIN
GOTO doerror
END
Select @ll_doc = isnull(max(doc_no),0)+1 from tr_header IF @@ERROR != 0
Begin
GOTO doerror
End
END
ELSE
------To UPDATE pass the doc_no------
BEGIN
SELECT @ll_doc = @doc_no
delete from stock_detail where doc_no = @doc_no IF @@ERROR != 0
Begin
GOTO doerror
End
delete from tr_header where doc_no = @doc_no IF @@ERROR != 0
Begin
GOTO doerror
End
END
![](/html/1438/356/html_wpziZPj8IA.ZbBg/htmlconvd-0Ag2qI367x1.jpg)
BEGIN
INSERT INTO tr_header ( id, date,ref,doc_no ,narr)
VALUES (@id, isnull(@date,getdate()),@ref,@ll_doc, @narr)
IF @@ERROR != 0
Begin
GOTO doerror
END
INSERT INTO stock_detail ( doc_no, qty_in, qty_out, code_value, sr_no )
VALUES (@ll_doc, isnull(@qty_in,0), ISNULL(@qty_out,0), @code_value, 1 )
IF @@ERROR != 0
Begin
GOTO doerror
End
END
COMMIT TRANSACTION
SELECT 0
GOTO doreturn
doerror:
Rollback TRANSACTION
doreturn:
RETURN 0
SELECT -100
To add (insert) a new record, you will pass a null value to @doc_no parameter of the stored procedure. In this case, the procedure selects the maximum doc_no from the tr_header table, increments it by one, and stores it in the variable @ll_doc.
To modify (update) an existing stock detail transaction, you pass an existing document number to the @doc_no parameter of the stored procedure. In this case, the procedure stores the passed document number to the variable @ll_doc. It then deletes the stock_detail records transactions having this doc_no, as they will again be recreated with the passed parameters. This process of deleting and reinserting the
![](/html/1438/356/html_wpziZPj8IA.ZbBg/htmlconvd-0Ag2qI368x1.jpg)
stock_detail records enable the delete and insert triggers associated with the stock_detail table to fire. As will be explained in the next section, this process updates the closing balance field of the appropriate stock_master record. Finally, for both the insert and update modes, a row is created in the tr_header table having a document number equal to the value stored in the variable @ll_doc. A row is also created in the stock_detail table with the passed parameters.
Triggers on the stock_detail Table
The stock_detail table has an insert, an update, and a delete trigger. These triggers update the closing balance field of the stock_master table to reflect the most current stock balance. The closing stock balance is thus current after every stock movement. These triggers are listed below.
The insert_stk trigger is an insert trigger on the stock_detail table. Its code is as follows:
Insert_stk
CREATE TRIGGER insert_stk ON stock_detail for insert as
DECLARE @sql varchar(200)
DECLARE @mtype char(1)
DECLARE @bal money
SELECT *
into #temp
from inserted
BEGIN
SELECT @bal = ISNULL(#temp.qty_in,0) - ISNULL(#temp.qty_out,0)
FROM #temp
END
UPDATE stock_master
SET closing = isnull(closing,0) + @bal
FROM stock_master, #temp
WHERE ( stock_master.code_value = #temp.code_value )
The update_stk trigger is an update trigger on the table stock_detail. Its code is as follows:
Update_stk
![](/html/1438/356/html_wpziZPj8IA.ZbBg/htmlconvd-0Ag2qI369x1.jpg)
CREATE TRIGGER update_stk ON stock_detail for update as
Declare @sql varchar(200)
DECLARE @mtype char(1)
DECLARE @bal money
SELECT *
into #temp from inserted
SELECT * into #t2 from deleted BEGIN
SELECT @bal = ISNULL(#temp.qty_in,0) - ISNULL(#temp.qty_out,0) -(ISNULL(#t2.qty_in,0) - ISNULL(#t2.qty_out,0))
From #temp, #t2
Where #temp.code_value = #t2.code_value
END
UPDATE stock_master
SET closing = isnull(closing,0) + @bal
FROM stock_master, #temp WHERE ( stock_master.code_value = #temp.code_value)
The delete_stk trigger is an update trigger on the table stock_detail. Its code is as follows:
delete_stk
CREATE TRIGGER delete_stk ON stock_detail for delete as
DECLARE @sql varchar(200)
DECLARE @mtype char(1)
DECLARE @bal money
![](/html/1438/356/html_wpziZPj8IA.ZbBg/htmlconvd-0Ag2qI370x1.jpg)
SELECT *
into #temp
from deleted
UPDATE stock_master
SET Closing =Closing-
(ISNULL(#temp.qty_in,0) - ISNULL(#temp.qty_out,0))
FROM stock_master, #temp
WHERE ( stock_master.code_value = #temp.code_value )
These triggers make use of the Microsoft SQL Server deleted and inserted tables to access the before and after values of the fields. An inserted table is a SQL Server table, which holds the inserted values in case of an insert statement, or the updated values in case of an update statement. A deleted table is a Microsoft SQL Server table that holds the original values in case of an update statement or the deleted value in case of a delete statement. These tables have the same fields as the table it references, which, in this case, is the stock_detail table. These triggers simply apply an arithmetic formula to arrive at the closing balance. Table 25.1 describes the calculations performed by each trigger in calculating the closing balance.
|
Table 25.1 Closing Balance Calculations |
|
|
|
||
|
|
|
|
|
|
|
|
Action |
|
Trigger |
|
Closing balance |
|
|
|
|
|
|
formula |
|
|
|
|
|
|
|
|
|
Insert |
|
insert_stk |
|
closing + |
|
|
|
|
|
(inserted.qty_ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
in - |
|
|
|
|
|
|
inserted.qty_o |
|
|
|
|
|
|
ut) |
|
|
|
|
|
|
|
|
|
Update |
|
update_stk |
|
closing + |
|
|
|
|
|
(inserted.qty_ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
in - |
|
|
|
|
|
|
inserted.qty_o |
|
|
|
|
|
|
ut) minus |
|
|
|
|
|
|
(deleted.qty_i |
|
|
|
|
|
|
n - |
|
|
|
|
|
|
deleted.qty_ou |
|
|
|
|
|
|
t) |
|
|
|
|
|
|
|
|
|
Delete |
|
delete_stk |
|
closing - |
|
|
|
|
|
(deleted.qty_i |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
n - |
|
|
|
|
|
|
deleted.qty_ou |
|
|
|
|
|
|
t) |
|
|
|
|
|
|
|
|