Академический Документы
Профессиональный Документы
Культура Документы
Net : Views
Besides the views, this application also explores the fundamentals of stored procedures,
including how to create, execute, and use one in a graphical application.
Imports System.Data.SqlClient
Public Class Central
Friend Sub CreateDatabase()
Dim strAction As String
Dim Command As SqlCommand = Nothing
Using Connect As SqlConnection = _
New SqlConnection("Data Source=(local); " & _
"Integrated Security='SSPI';")
Connect.Open()
Command.ExecuteNonQuery()
MsgBox("A table named Employees has " & _
"been added to the database.")
End Using
Connect.Open()
Command.ExecuteNonQuery()
MsgBox("A table named Customers has " & _ Page | 3
"been added to the database.")
End Using
Connect.Open()
Command.ExecuteNonQuery()
MsgBox("A table named AccountTransactions " & _
"has been added to the database.")
End Using
Page | 5
15. Access the Central form, add a button and change its properties as follows: Page | 6
(Name): BtnAccountTypes
Text: Account Types...
16. Double-click the Account Types button and implement its event as follows:
17. Execute the application and open the Account Types form
18. Create the following records:
AccountType
Saving
Checking
Certificate of Deposit
22. In the Data Sources window, click Employees and click the arrow on its right side to
drop the combo box
23. Select Details
24. Drag the Employees node and drop it on the form
25. Design the form as follows: Page | 7
26. Double-click the Close button and implement its even as follows:
27. Access the Central form, add a button and change its properties as follows:
(Name): BtnEmployees
Text: Employees...
28. Double-click the Account Types button and implement its event as follows:
Page | 8
Page | 9
Other
Control Text Name
Properties
Label Employee #:
MaskedTextBox TxtEmployeeNumber Mask: 00000
Label . LblEmployeeName
Label Start Date:
DateTimePicker DtpStartDate
Label End Date:
Label . LblEndDate
Label Mon
Label Tue
Label Wed
Label Thu
Label Fri
Label Sat
Label Sun
Label Week 1:
TextBox 0.00 TxtWeek1Monday TextAlign: Right
TextBox 0.00 TxtWeek1Tuesday TextAlign: Right
TextBox 0.00 TxtWeek1Wednesday TextAlign: Right
TextBox 0.00 TxtWeek1Thursday TextAlign: Right
TextBox 0.00 TxtWeek1Friday TextAlign: Right
TextBox 0.00 TxtWeek1Saturday TextAlign: Right
Ado.Net : Views
Imports System.Data.SqlClient
End If
Dim strSelect As String = "SELECT * FROM Employees " & _
"WHERE EmployeeNumber = '" & _
TxtEmployeeNumber.Text & "';"
Dim Connect As SqlConnection = _
New SqlConnection("Data Source=(local); " & _
"Database='YugoNationalBank1';" & _ Page | 11
"Integrated Security=true")
Dim cmdDatabase As SqlCommand = New SqlCommand(strSelect, Connect)
sda.SelectCommand = cmdDatabase
sda.Fill(dsEmployees)
Try
Dim recEmployee As DataRow = dsEmployees.Tables(0).Rows(0)
If recEmployee.IsNull("EmployeeNumber") Then
ValidTimeSheet = False
Throw New System.IndexOutOfRangeException("Bad Employee Number!")
Else
ValidTimeSheet = True
EmployeeID = CType(recEmployee("EmployeeID"), Integer)
Dim strFullName As String = _
CStr(recEmployee("FirstName")) & _
" " & CStr(recEmployee("LastName"))
LblEmployeeName.Text = "Welcome " & strFullName
End If
DtpStartDate.Value = Date.Today
TxtWeek1Monday.Text = "0.00"
TxtWeek1Tuesday.Text = "0.00"
TxtWeek1Wednesday.Text = "0.00"
TxtWeek1Thursday.Text = "0.00"
TxtWeek1Friday.Text = "0.00"
TxtWeek1Saturday.Text = "0.00"
TxtWeek1Sunday.Text = "0.00"
TxtWeek2Monday.Text = "0.00"
TxtWeek2Tuesday.Text = "0.00"
TxtWeek2Wednesday.Text = "0.00"
TxtWeek2Thursday.Text = "0.00"
TxtWeek2Friday.Text = "0.00"
TxtWeek2Saturday.Text = "0.00"
TxtWeek2Sunday.Text = "0.00"
Ado.Net : Views
Connect.Close()
End Sub
TxtWeek2Monday.Text = "0.00"
TxtWeek2Tuesday.Text = "0.00"
TxtWeek2Wednesday.Text = "0.00"
TxtWeek2Thursday.Text = "0.00"
TxtWeek2Friday.Text = "0.00"
TxtWeek2Saturday.Text = "0.00"
TxtWeek2Sunday.Text = "0.00"
IsNewRecord = True
End Sub
To implement the electronic time, we will use two pieces of information are required:
an employee's number and a starting period. After an employee has opened a time
sheet:
1. The employee must first provide an employee number, which we will check in
the Employees table. If the employee provides a valid employee number, we can
continue with the time sheet. If the employee number is invalid, we will let the
user know and we cannot continue with the time sheet
2. After the employee has provided a valid employee number, we will request the
starting period. After entering a (valid) date, we will check the time. If there is a
record that holds both the employee number and the start date, this means that
the employee had previously worked on a time sheet and we will open that
existing time sheet.
After the the employee or contractor has entered a valid employee number and a start
date, we will create a number called a time sheet code, represented in the TimeSheet
as the TimeSheetCode column. This number is created as follows:
0000000000000
The first 5 digits represent the employee's number. The second 4 digits represent the
year of the start date. The next 2 digits represent the month, and the last 2 digits
Ado.Net : Views
represent the day. This number must be unique so that there would not be a duplicate
number throughout the time sheet.
To make sure the value of the TimeSheetCode is unique for each record, after the
employee has provided a valid employee number and a start date, we will create the
time sheet code and check if that number exists in the TimeSheet table already:
Page | 13
o If that number exists already, this means that the employee has previously
worked on that time sheet and he or she simply wants to verify or update it. We
will then open the time values for that record and let the user view or change it
o If there is no record with the specified time sheet code, we will conclude that the
employee is working on a new time sheet
43. In the Method Name combo box, select CloseUP and implement the event as follows:
If TxtEmployeeNumber.Text.Equals("") Then
ValidTimeSheet = False
Exit Sub
End If
StartDate = DtpStartDate.Value
IntMonth = StartDate.Month
IntDay = StartDate.Day
"Integrated Security=true")
Dim CmdTimeSheet As SqlCommand = _
New SqlCommand(StrSQL, ConnectTimeSheet)
ConnectTimeSheet.Close()
Try
Dim RecordTimeSheet As DataRow = dsTimeSheet.Tables(0).Rows(0)
StrTimeSheetCode = CStr(RecordTimeSheet("TimeSheetCode"))
If RecordTimeSheet.IsNull("TimeSheetCode") Then
IsNewRecord = True
Throw New System.IndexOutOfRangeException( _
"No TimeSheet with that number exists!")
Else
TxtWeek1Monday.Text = CStr(RecordTimeSheet("Week1Monday"))
TxtWeek1Tuesday.Text = CStr(RecordTimeSheet("Week1Tuesday"))
TxtWeek1Wednesday.Text = _
CStr(RecordTimeSheet("Week1Wednesday"))
TxtWeek1Thursday.Text = _
CStr(RecordTimeSheet("Week1Thursday"))
TxtWeek1Friday.Text = CStr(RecordTimeSheet("Week1Friday"))
TxtWeek1Saturday.Text = _
CStr(RecordTimeSheet("Week1Saturday"))
TxtWeek1Sunday.Text = CStr(RecordTimeSheet("Week1Sunday"))
TxtWeek2Monday.Text = CStr(RecordTimeSheet("Week2Monday"))
TxtWeek2Tuesday.Text = CStr(RecordTimeSheet("Week2Tuesday"))
TxtWeek2Wednesday.Text = _
CStr(RecordTimeSheet("Week2Wednesday"))
TxtWeek2Thursday.Text = _
CStr(RecordTimeSheet("Week2Thursday"))
TxtWeek2Friday.Text = CStr(RecordTimeSheet("Week2Friday"))
TxtWeek2Saturday.Text = _
CStr(RecordTimeSheet("Week2Saturday"))
TxtWeek2Sunday.Text = CStr(RecordTimeSheet("Week2Sunday"))
IsNewRecord = False
End If
Catch Exc As IndexOutOfRangeException
BtnReset_Click(sender, e)
End Try
End Sub
44. Access the Central form, add a button and change its properties as follows:
(Name): BtnTimesheet
Text: Employee's Time Sheet...
45. Double-click the Account Types button and implement its event as follows:
Ado.Net : Views
Page | 16
Object Name
customersBindingSource BsCustomers
customersTableAdapter TaCustomers
customersBindingNavigator BnCustomers
19. Once again, from the Data Sources window, drag EmployeeIdentification and drop it
on the form
20. While the data grid view is still selected, press Delete to remove it
21. Under the form, click the objects and, using the Properties window, change their
names as follows:
Ado.Net : Views
22. Once again, from the Data Sources window, drag AccountTypes and drop it on the Page | 17
form
23. While the data grid view is still selected, press Delete to remove it
24. Under the form, click the objects and, using the Properties window, change their
names as follows:
Object Name
accountTypesBindingSource BsAccountTypes
accountTypesTableAdapter TaAccountTypes
25. On the form, click the text box on the right side of Employee ID and press Delete
26. On the form, click the text box on the right side of Date Created and press Delete
27. On the form, click the text box on the right side of Account Type ID and press Delete
28. On the form, click the text box on the right side of Account Number and press Delete
29. On the form, click the text box on the right side of Account Status and press Delete
30. Design the form as follows:
Ado.Net : Views
31.
32. Double-click the Close button and implement its even as follows:
33. Access the Central form, add a button and change its properties as follows:
(Name): BtnCustomers
Text: Customers...
34. Double-click the Account Types button and implement its event as follows:
Items:
Cash
Check
Money Order
Amount
Label
Deposited: Page | 20
TextBox TxtAmount TextAlign: Right
Label Notes
Multiline: True
TextBox TxtNotes
ScrollBars: Vertical
Button Submit BtnSubmit
Button Close BtnClose
Imports System.Data.SqlClient
Connect.Open()
If EmployeeID = 0 Then
MsgBox("The employee number you entered " & _
"is not recognized in our database.")
TxtEmployeeNumber.Text = ""
End If
End Using
End If
End Sub
sdaYNB.SelectCommand = Command
Dim dsCustomers As DataSet = New DataSet("CustomersSet")
sdaYNB.Fill(dsCustomers)
Connect.Open()
Exit For
Next
If CustomerID = 0 Then
MsgBox("The account number you entered " & _
"is not recognized in our database.")
TxtAccountNumber.Text = "" Page | 22
End If
End Using
End If
End Sub
If EmployeeID = 0 Then
MsgBox("You must specify the employee number " & _
"of the clerk who is processing the deposit.")
Exit Sub
End If
If CustomerID = 0 Then
MsgBox("You must enter an account number " & _
"for the new customer.")
Exit Sub
End If
StrCurrencyType = CbxCurrencyTypes.Text
Try
Amount = CDbl(TxtAmount.Text)
Catch ex As Exception
MsgBox("Invalid Amount.")
End Try
Connect.Open() Page | 23
cmdEmployees.ExecuteNonQuery()
DtpTransactionDate.Value = DateTime.Today
TxtEmployeeNumber.Text = ""
TxtEmployeeName.Text = ""
TxtAccountNumber.Text = ""
TxtCustomerName.Text = ""
CbxCurrencyTypes.SelectedIndex = 0
TxtAmount.Text = "0.00"
TxtNotes.Text = ""
End Using
End Sub
52. Access the Central form, add a button and change its properties as follows:
(Name): BtnNewDeposit
Text: New Deposit...
53. Double-click the Account Types button and implement its event as follows:
54. To create a new form, on the main menu, click Project -> Add Windows Form...
55. Set the Name to NewWithdrawal and click Add
56. Design the form as follows:
Ado.Net : Views
Page | 24
59. In the Method Name combo box, select Load and make the following changes:
Imports System.Data.SqlClient
Connect.Open()
While rdrEmployees.Read()
EmployeeID = rdrEmployees.GetSqlInt32(0)
TxtEmployeeName.Text = rdrEmployees.GetString(1) & " " & _
rdrEmployees.GetString(2)
End While
If EmployeeID = 0 Then
MsgBox("The employee number you entered " & _
"is not recognized in our database.")
TxtEmployeeNumber.Text = ""
End If
End Using
Ado.Net : Views
End If
End Sub
sdaYNB.SelectCommand = Command
Dim dsCustomers As DataSet = New DataSet("CustomersSet")
sdaYNB.Fill(dsCustomers)
Connect.Open()
If CustomerID = 0 Then
MsgBox("The account number you entered " & _
"is not recognized in our database.")
TxtAccountNumber.Text = ""
End If
End Using
End If
End Sub
If CustomerID = 0 Then
MsgBox("You must enter a valid account number " & _
"for the new customer.")
Exit Sub Page | 27
End If
Try
Amount = CDbl(TxtAmount.Text)
Catch ex As Exception
MsgBox("Invalid Amount.")
Exit Sub
End Try
Connect.Open()
cmdCharges.ExecuteNonQuery()
DtpTransactionDate.Value = DateTime.Today
TxtAccountNumber.Text = ""
TxtCustomerName.Text = ""
CbxChargeReasons.SelectedIndex = 0
TxtAmount.Text = "0.00"
TxtNotes.Text = ""
End Using
End Sub
68. Access the Central form, add a button and change its properties as follows:
(Name): BtnNewWithdrawal
Text: New Withdrawal...
69. Double-click the Account Types button and implement its event as follows:
70. To create a new form, on the main menu, click Project -> Add Windows Form...
71. Set the Name to NewCharge and click Add
72. Design the form as follows:
Check Stopping
Monthly Charge
Amount
Label
Charged:
TextBox TxtAmount TextAlign: Right
Page | 29
Label Notes
Multiline: True
TextBox TxtNotes
ScrollBars: Vertical
Button Submit BtnSubmit
Button Close BtnClose
Imports System.Data.SqlClient
sdaYNB.SelectCommand = Command
Dim dsCustomers As DataSet = New DataSet("CustomersSet")
sdaYNB.Fill(dsCustomers)
Connect.Open()
If CustomerID = 0 Then
MsgBox("The account number you entered " & _
"is not recognized in our database.")
txtAccountNumber.Text = ""
End If
End Using
End If
End Sub
If CustomerID = 0 Then
MsgBox("You must enter a valid account number " & _
"for the new customer.")
Exit Sub
End If
Try
Amount = CDbl(TxtAmount.Text)
Catch ex As Exception
MsgBox("Invalid Amount.")
Exit Sub
End Try
Connect.Open() Page | 31
cmdCharges.ExecuteNonQuery()
DtpTransactionDate.Value = DateTime.Today
TxtAccountNumber.Text = ""
TxtCustomerName.Text = ""
CbxChargeReasons.SelectedIndex = 0
TxtAmount.Text = "0.00"
TxtNotes.Text = ""
End Using
End Sub
82. Access the Central form, add a button and change its properties as follows:
(Name): BtnNewCharge
Text: New Charge...
83. Double-click the Account Types button and implement its event as follows:
92. In the Diagram section, click the check boxes of CustomerID, CustomerName,
AccountNumber, AccountType, DateCreated, and AccountStatus
Page | 32
Page | 33
Page | 34
Object Name
accountTransactionsBindingSource BsAccountTransactions
accountTypesTableAdapter TaAccountTransactions
Page | 35
Text: 0.00
TextBox TxtTotalCharges
TextAlign: Right
Button Close BtnClose
Label Total Withdrawals
Text: 0.00 Page | 36
TextBox TxtTotalWithdrawals
TextAlign: Right
Label Balance
Text: 0.00
TextBox TxtBalance
TextAlign: Right
Imports System.Data.SqlClient
118. Locate the Load event and add the following line:
Me.TaAccountTransactions.Fill(Me.DsYugoNationalBank.AccountTransactions
)
BsAccountTransactions.Filter = "AccountNumber = '00-000000-00'"
End Sub
If TxtAccountNumber.Text.Length = 0 Then
MsgBox("You must specify the account number " & _
"of the customer whose transactions you want to view.")
Exit Sub
Else
Using Connect As SqlConnection = _
New SqlConnection("Data Source=(local);" & _
"Database='YugoNationalBank1';" & _
Ado.Net : Views
"Integrated Security=SSPI;")
SdaYNB.SelectCommand = Command
Dim dsCustomers As DataSet = New DataSet("CustomersSet")
SdaYNB.Fill(dsCustomers)
Connect.Open()
TxtCustomerName.Text = Record("CustomerName")
TxtAccountType.Text = Record("AccountType")
TxtAccountStatus.Text = Record("AccountStatus")
DtpDateCreated.Value = Record("DateCreated")
Next
BsAccountTransactions.Filter = _
"AccountNumber = '" & TxtAccountNumber.Text & "'"
End Using
End If
Connect.Open()
Dim rdrTransactions As SqlDataReader = _
Command.ExecuteReader
While rdrTransactions.Read()
Try
Deposits = CDbl(rdrTransactions(0))
Catch ex As Exception
End Try
Ado.Net : Views
Try
Withdraws = CDbl(rdrTransactions(1))
Catch ex As Exception
End Try
Try
Charges = CDbl(rdrTransactions(2))
Catch ex As Exception Page | 38
End Try
TxtTotalDeposits.Text = FormatNumber(Deposits)
TxtTotalWithdrawals.Text = FormatNumber(Withdraws)
TxtTotalCharges.Text = FormatNumber(Charges)
In the Method Name combo box, select Click and implement the even as follows:
Private Sub BtnClose_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles BtnClose.Click
Close()
End Sub
122. Access the Central form, add a button and change its properties as follows:
(Name): BtnAccountTransactions
Text: View an Account's Transactions...
Ado.Net : Views
123. Double-click the Account Types button and implement its event as follows:
Page | 40
Close()
End Sub
6. Save all
Connect.Open()
cmdTimeSheet.ExecuteNonQuery()
Connect.Close()
BtnReset_Click(sender, e)
Else
Page | 44
-- =============================================
-- Author: Darwin Vargas
-- Creation date: Friday, January 28, 2008
-- Description: This stored procedure assigns a
-- default password to each employee.
-- =============================================
AS
BEGIN
UPDATE dbo.Employees
SET Password = 'Password1' FROM dbo.Employees;
END
3. To save the stored procedure, on the Standard toolbar, click the Save button
Running [dbo].[AssignDefaultPassword].
(8 row(s) affected)
(0 row(s) returned)
@RETURN_VALUE = 0
Finished running [dbo].[AssignDefaultPassword].
Page | 45
2. (You should open the Employees table from the Server Explorer and verify that the
Password fields now have Password1 each).
Close the stored procedure tab
-- =============================================
-- Author: Darwin Vargas
-- Create date: Friday, May 25, 2007
-- Description: This stored procedure creates a
-- username for each employee.
-- It also assigns an email to the employee.
-- =============================================
CREATE PROCEDURE dbo.CreateUsername
AS
BEGIN
UPDATE dbo.Employees
SET Username = LOWER(LEFT(FirstName, 1) + LEFT(LastName, 5))
FROM dbo.Employees;
UPDATE dbo.Employees
SET EmailAddress = LOWER(LEFT(FirstName, 1) +
LEFT(LastName, 5)) +
'@yugonationalbank.com'
FROM dbo.Employees;
END
3. To save the stored procedure, on the Standard toolbar, click the Save button
4. To execute the stored procedure, in the Server Explorer and under the
YugoNationalBank1 database, right-click CreateUsername and click execute
5. Close the stored procedure tab