![](/user_photo/1438_p9ksI.png)
Microsoft ASP .NET Professional Projects - Premier Press
.pdf![](/html/1438/356/html_wpziZPj8IA.ZbBg/htmlconvd-0Ag2qI281x1.jpg)
|
Table 16.2 Closing Balance Calculations |
|
|
|
||
|
|
|
|
|
|
|
|
Action |
|
Trigger |
|
Formula for updating Masters |
|
|
|
|
|
|
closing balance |
|
|
|
|
|
|
|
|
|
|
|
|
|
inserted.dr_amount)- |
|
|
|
|
|
|
(deleted.cr_amount - |
|
|
|
|
|
|
deleted.dr_amount) |
|
|
|
|
|
|
|
|
|
Delete |
|
delete_mstr |
|
For A, E : closing - |
|
|
|
|
(deleted.dr_amount - |
|
||
|
|
|
|
|
|
|
|
|
|
|
|
deleted.cr_amount) |
|
|
|
|
|
|
For I, L : closing - |
|
|
|
|
|
|
(deleted.cr_amount - |
|
|
|
|
|
|
deleted.dr_amount) |
|
|
|
|
|
|
|
|
Transaction Maintenance
Transaction maintenance involves adding, modifying, and deleting transactions. This implementation comprises two web forms, one Code-Behind form, and one stored procedure. These are as follows:
1.The Selection Web Form (selection.aspx).
2.The Transactions Web Form (transactions.aspx) and the Code-Behind form (transactions.vb).
3.Stored Procedure (p_trans).
The Selection Form
The Selection form is a simple form that displays a drop-down list of all the bank, cash, or credit card master accounts defined in the system. The user chooses the appropriate financial account that he wants to work with and clicks on a Submit button. This takes him to the Transactions web form where the actual processing takes place.
Figure 16.1 shows what the Selection form looks like.
Figure 16.1: The Selection form allows you to select a bank or cash account.
The code listing of the Selection form is as follows:
Selection.aspx
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<html>
<script language="VB" runat="server"> Dim myConnection As OleDbConnection Dim myCommand As OleDbDataAdapter Dim ds As New DataSet
Dim ConnStr As String
Dim SQL As String
Sub Page_Load(Source As Object, E As EventArgs) ConnStr = "Provider=SQLOLEDB; Data Source=(local); " ConnStr = ConnStr+" Initial Catalog=ASPNET;User ID=sa;" myConnection = New OleDbConnection(ConnStr)
if NOT (isPostBack) rebind
end if End Sub
Sub SubmitBtn_Click(sender As Object, e As EventArgs)
Session("TheSelectionText")= Selection.SelectedItem.Text
Session("TheSelectionValue")= Selection.SelectedItem.Value
Response.Redirect("Transactions.aspx")
End Sub
Sub ReBind()
'DataSetCommand
SQL = "Select * from masters where code_category in (604,605) " SQL = SQL + " order by code_display"
myCommand = New OleDbDataAdapter(SQL, myConnection) 'use Fill method of DataSetCommand to populate dataset myCommand.Fill(ds, "Masters") Selection.DataSource=ds.Tables("Masters").DefaultView Selection.DataBind()
![](/html/1438/356/html_wpziZPj8IA.ZbBg/htmlconvd-0Ag2qI283x1.jpg)
End Sub
</script>
<body>
<h3><font face="Verdana">Financial Account Selection</font></h3>
<form runat="server" >
Select Cash or Bank Account :</B><asp:DropDownList
DataTextField = "code_display"
DataValueField = "code_value" id="selection" runat="server" />
<asp:Button text="Go" runat="server" OnClick="SubmitBtn_Click" />
</form>
</body>
</html>
The Selection form presents a drop-down list of all the cash, bank, or credit card accounts defined in the system (that is, master accounts with code_category of 604 or 605). This form posts to the Transactions form, and in the page_load event of this form, the passed code_value is extracted.
The Transaction Form
The Transactions web form is similar to the Masters web form. It enables users to add and modify records. The add functionality is provided by textboxes, residing on a panel which is made visible when the Add button is clicked. A DataGrid implements the "modify" functionality. Figure 16.2 shows what the form looks like. Figure 16.3 shows what it looks like in Add mode.
Figure 16.2: The Transactions form.
![](/html/1438/356/html_wpziZPj8IA.ZbBg/htmlconvd-0Ag2qI284x1.jpg)
Figure 16.3: The Transactions form in Add mode.
page_load Event
The page_load event creates a new connection to the database. It then extracts the code_value passed to it from the Selection form and stores it into the variable code. This is the primary key of the master's record selected by the user. It then calls the UpdateSelection function with this value.
Transactions.aspx Page_Load Event
Sub Page_Load(Source As Object, E As EventArgs)
ConnStr = "Provider=SQLOLEDB; Data Source=(local); "
ConnStr = ConnStr+" Initial Catalog=ASPNET;User ID=sa;"
myConnection = New OleDbConnection(ConnStr)
if NOT (isPostBack)
Dim code as string, display as string
code = Session("TheSelectionValue")
title.text = Session("TheSelectionText")
if code = "" then
response.redirect("selection.aspx")
end if
UpdateSelection(code)
rebind
end if
End Sub
![](/html/1438/356/html_wpziZPj8IA.ZbBg/htmlconvd-0Ag2qI285x1.jpg)
The UpdateSelection Function
The tblSelections table contains a single column called selection. The code_value of the account selected by the user is stored here. This value will later be used in the function ReBind to bind the DataGrid. The following is the script of the function:
Sub UpdateSelection
Sub UpdateSelection(vselection)
sql = "delete from tblSelection "
sql = sql + " insert into tblSelection(selection)" sql = sql + " values('" + vselection + "')" runSql(sql)
End Sub
The Rebind Function
The ReBind function binds the DataGrid to a SQL query, first at the Page_Load event, and then whenever the data changes and the grid needs to be refreshed. The following is the script of the function:
Sub ReBind
Sub ReBind()
SQL = " select m.code_value,m.code_display,t.*, h.* ,"
sql = sql + "(select code_display from masters where code_value = t.posted_to) "
sql = sql + " as posted_display "
sql = sql + " from tr_header h,transactions t, masters m "
sql = sql + " where t.doc_no = h.doc_no "
sql = sql + " and m.code_value = t.code_value"
sql = sql + " and m.code_value = (select selection from tblSelection)"
myCommand = New OleDbDataAdapter(SQL, myConnection)
'use Fill method to populate dataset
myCommand.Fill(ds, "transactions")
![](/html/1438/356/html_wpziZPj8IA.ZbBg/htmlconvd-0Ag2qI286x1.jpg)
'Binding a Grid
Grid1.DataSource=ds.Tables("transactions").DefaultView
Grid1.DataBind()
'populate account selection drop down list which is
'visible in the add mode
SQL = "Select * from masters where code_value <> "
SQL = SQL + " (select selection from tblSelection)"
myCommand = New OleDbDataAdapter(SQL, myConnection)
myCommand.Fill(ds, "masters")
aposted_display.DataSource=ds.Tables("masters").DefaultView
aposted_display.DataBind()
addshow.visible = true
Notice how the code_value is extracted from the tblSelection table in the last line of the query.
sql = sql + "and m.code_value = (select selection from tblSelection)".
The UpdateSelection function had previously inserted the selected code_value in the table tblSelection. This function populates an OleDbDataAdapter with the SQL query, and the Fill method of the OleDbDataAdapter populates the Transactions table of the DataSet with the rows existing in the OleDbDataAdapter. The DataGrid Grid1 is then bound to the default view of the Transactions table of the DataSet. The addshow button is made visible. Clicking on this button, in turn, makes the panel visible and the user can then add a transaction.
The Add Mode
When the addshow button is clicked, the add_show Sub is fired. This Sub simply sets the visible property of the panel AddPanel to visible. This, in turn, makes all the controls residing on this panel visible.
Sub add_show
Sub add_show(Source As Object, E As EventArgs)
AddPanel.visible = true
End Sub
The input controls for the Insert mode have been marked in Transactions.aspx within HTML comment blocks Insert Logic Starts and Insert Logic Ends. Each control has an associated id property, which will be used later to refer to the control.
![](/html/1438/356/html_wpziZPj8IA.ZbBg/htmlconvd-0Ag2qI287x1.jpg)
There is a RequiredFieldValidator attached to the Date and ref columns. The ref needs to be a unique field. The stored procedure p_trans, which gets called when a transaction needs to be added to the database, checks to see whether this field is unique. If not, the procedure will not do anything and will return an error condition. The add_click button is fired when the user clicks on the submitDetails button. This Sub builds a SQL execute query and passes it on to the RunSql function, which in turn executes it. The following is the script for the add_click button:
Sub add_click
Sub add_click(Source As Object, E As EventArgs)
Dim sql As string
sql = "Execute p_trans @date = '" + adate.text + "' ,"
sql = sql + "@ref= '" + aref.text + "', @dr_amount = "
sql = sql + adr_amount.text + ",@cr_amount = "
sql = sql + acr_amount.text +" , @posted_to = '"
sql = sql + aposted_display.SelectedItem.value + "' ,"
sql = sql + "@id = 'RPT', @doc_no = NULL" + ", @narr= '"
sql = sql + anarr.text + "'"
RunSql(sql)
rebind()
hidePanel()
End Sub
The hidePanel function simply hides the panel (by setting its visible property to 0) and sets the values of all the textboxes to "".
The Update Mode
The DataGrid is activated in the Edit mode when the edit link is clicked. The user makes the appropriate changes and clicks on the Ok link. This fires off the Grid1_Update function. The primary key value and other textboxes' values are extracted and a SQL procedure call string is built. This string is passed onto the RunSql function, which makes the actual procedure call.
Sub Grid1_Update
Sub Grid1_Update(sender As Object, e As DataGridCommandEventArgs)
Dim sql As string
Dim vdate As String
Dim ref As String
Dim code_value As String
Dim dr_amt As String
Dim cr_amt As String
Dim posted_display As String
Dim id As String
Dim narr As String
Dim myTextBox As TextBox
'This is the key value : Retrieved from the DataKey, since it's a read only field Dim doc_no as string = Grid1.DataKeys.Item(E.Item.ItemIndex).ToString myTextBox = E.Item.FindControl("edit_date")
vdate = trim(mytextbox.text)
myTextBox = E.Item.FindControl("edit_ref") ref = trim(mytextbox.text)
myTextBox = E.Item.FindControl("edit_dr_amt") dr_amt = trim(mytextbox.text)
myTextBox = E.Item.FindControl("edit_cr_amt") cr_amt = trim(mytextbox.text)
myTextBox = E.Item.FindControl("edit_posted_display") posted_display= trim(mytextbox.text)
myTextBox = E.Item.FindControl("edit_narr") narr = trim(mytextbox.text)
'Now execute stored procedure
sql = "Execute p_trans @date = '" + vdate+ "' ,@ref= '" sql = sql + ref + "', @dr_amount ="
sql = sql + dr_amt + ",@cr_amount = "
sql = sql + cr_amt +" , @posted_to = " + posted_display + " ,"
sql = sql + "@id = 'RPT', @doc_no = " + doc_no + ", @narr= '" + narr+ "'" RunSql(sql)
![](/html/1438/356/html_wpziZPj8IA.ZbBg/htmlconvd-0Ag2qI289x1.jpg)
rebind()
End Sub
Function RunSql
This is a generic function, which executes a SQL Action statement against the database. This is the same function that we discussed in relation to the Masters web form and the code is exactly the same.
The Delete Mode
I have created a ButtonColumn which has a CommandName of "Delete" as follows:
<asp:ButtonColumn Text = "Delete", CommandName = "Delete", HeaderText = "Delete"/>.
In the DataGrid tag, I have specified the OnDeleteCommand to fire the Grid1_delete function. This function gets fired each time the user clicks on the delete hyperlink. The Grid1_delete function sends a SQL delete query to the RunSql function, which deletes all tr_header and transactions records having a document number equal to the clicked doc_no.
The Delete Sub
Sub Grid1_delete(sender As Object, e As DataGridCommandEventArgs)
Dim doc_no as string = Grid1.DataKeys.Item(E.Item.ItemIndex).ToString
Dim sql As string
sql = " Delete from transactions where doc_no = " + cstr(doc_no)
sql = sql + " Delete from tr_header where doc_no = " + cstr(doc_no)
RunSql(sql)
rebind()
End Sub
Here is the complete code listing. transactions.aspx
<%@Page Language="VB" Inherits="BaseClass" Src="transactions.vb" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %> <html>
<script language="VB" runat="server">
Sub Grid1_Update(sender As Object, e As DataGridCommandEventArgs) Dim sql As string
Dim vdate As String
Dim ref As String
Dim code_value As String
Dim dr_amt As String
Dim cr_amt As String
Dim posted_display As String
Dim id As String
Dim narr As String
Dim myTextBox As TextBox
'This is the key value : Retrieved from the DataKey, since it's a read only field Dim doc_no as string = Grid1.DataKeys.Item(E.Item.ItemIndex).ToString myTextBox = E.Item.FindControl("edit_date")
vdate = trim(mytextbox.text)
myTextBox = E.Item.FindControl("edit_ref") ref = trim(mytextbox.text)
myTextBox = E.Item.FindControl("edit_dr_amt") dr_amt = trim(mytextbox.text)
myTextBox = E.Item.FindControl("edit_cr_amt") cr_amt = trim(mytextbox.text)
myTextBox = E.Item.FindControl("edit_posted_display") posted_display= trim(mytextbox.text)
myTextBox = E.Item.FindControl("edit_narr") narr = trim(mytextbox.text)
'Now execute stored procedure