Injustice - Developers Among Us (SciFiDevCon 2024)
CODE SNIPPETS FOR BWM MOTOR FRANCHISE PROJECT
1. CODE SNIPPETS
FOR
BWM – MOTOR FRANCHISE PROJECT
Created by Team Members
Reviewed by Team Members
Module: Web Services
Created Date 19 April 2012
Assignment: Team Assignment Revised Date 27 April 2012
Revision No. 1.2
Team Name: SPOCK
Document Name F03-001
10. 3. UPDATE SOLD VEHICLE
10
(VEHICLE FOR SALES)
Consuming Web Service
'========================================================
*Option if want mark SOLD in garage DB and Delete in BWM website ' 2.2.1) UPDATE TO BWM WEBSERVICE
'========================================================
ReturnFunction = MyProxy.SetVehicleAsSold(RegNumber, Price,
UserName)
'========================================================
' 2.2.2) DELETE VEHICLE FROM SALES TABLE
' DELETE IS OPTION IN SELLING PAGE
'========================================================
'ReturnFunction = MyProxy.DeleteVehicleFromSalesTable(RegNumber)
11. 11
4. DELETE VEHICLE
(NON-SELL VEHICLE USING SECURED XML)
Consuming Web Service
'========================================================
' 2.1) AUTHEN TO BWM SITE
'========================================================
Dim Auth As New AuthenticationHeader()
Auth.UserName = "tutor"
Auth.PassWord = "password"
MyProxy.AuthenticationHeaderValue = Auth
'========================================================
' 2.2) DELETE VEHICLE FROM REGISTER TABLE (CALL WEBSERVICE)
'========================================================
ReturnFunction = MyProxy.DeleteVehicleFromRegisterTable(RegNumber)
13. 13 1. GET VEHICLE INFORMATION
• Parameter: Vehicle Register No
• Return: DataSet (0 or 1 record)
Only basic vehicle information transferred to BWM website
14. 14 2. GET VEHICLE INFORMATION (ALL)
• Parameter: Vehicle Register No
• Return: DataSet (0 or 1 record)
All Information collected in garage database
15. 3. GET ORDER INFORMATION
15
(SOLD CAR)
• Parameter: From Date and To Date
• Return: DataSet
• Only: SOLD Vehicle
This function used for BWM to monitor vehicle sold by garage
16. CLASSES
UI (aspx files)
We separate data layer from business logic by using three
classes: Car, User and Utils
Business Logic (VB
files)
Our design
- SQL queries are used only in classes (Except to quick search feature)
Data Layer
(Class files) - No SQL queries in VB, aspx, asmx files
- User interfaces (aspx files) are separated from business logic (vb files)
- Data layer is separated from business logic layer
DB 16
17. 17 1. CLASS “CAR”
Variable in class Functions in class
18. 18 2. CLASS “USER”
Use SQL DataReader
to collect data
Authentication and account functions Authentication User
19. 19 3. CLASS “UTILS”
Class Utils used for general functions
Benefits: Reusability
22. 2. UPDATE VEHICLE
22
(FUNCTION IN CLASS “CAR”)
'================================================================ '============================================================
' FUNCTION#6: UPDATE SELLING_CAR_INFO TO DB ' 2.3) FILL DATA TO DATASET
'================================================================ '============================================================
Public Function UpdateSellingCarInfoByDataSet(ByVal RegNumber As String) As ds.Tables(0).Rows(0).Item("DateUpdated") = Now
Boolean ds.Tables(0).Rows(0).Item("UpdatedBy") = UpdatedBy
'============================================================ ds.Tables(0).Rows(0).Item("DateSold") = DateSold
' 1) DECLARE VARIABLE ds.Tables(0).Rows(0).Item("SoldBy") = SoldBy
'============================================================ ds.Tables(0).Rows(0).Item("Price") = Price
Dim MyUtils As New Utils ds.Tables(0).Rows(0).Item("SellingStatus") = SellingStatus
Dim cn As SqlConnection ds.Tables(0).Rows(0).Item("SynStatus") = SynStatus
Dim da As SqlDataAdapter ds.Tables(0).Rows(0).Item("SynTime") = SynTime
Dim ds As New DataSet ds.Tables(0).Rows(0).Item("CustFullName") = CustFullName
Dim cnString As String = Global.Resources.Resource.cnString ds.Tables(0).Rows(0).Item("CustAddress") = CustAddress
Dim sqlCmd As String = "SELECT * FROM TM_CAR WHERE RegNo = '" & ds.Tables(0).Rows(0).Item("CustContactNo") = CustContactNo
RegNumber & "'" ds.Tables(0).Rows(0).Item("CustEmail") = CustEmail
'============================================================ ds.Tables(0).Rows(0).Item("Comment2") = Comment2
' 2) GET DATA AND UPDATE DATA TO DB
'============================================================ '============================================================
Try ' 4) UPDATE DATA IN DB
'========================================================
' 2.1) FILL DATA FROM DATABASE TO DATASET '============================================================
'======================================================== Dim cmb As New SqlCommandBuilder(da)
cn = New SqlConnection(cnString) da.Update(ds, "TM_CAR")
da = New SqlDataAdapter(sqlCmd, cn) ds.AcceptChanges()
da.Fill(ds, "TM_CAR")
'======================================================== '============================================================
' 2.2) NOT FOUND REG_NO ' 5) UPDATE SUCCESSFUL
'========================================================
If ds.Tables(0).Rows.Count <> 1 Then '============================================================
Return False Return True
End If Catch ex As Exception
'============================================================
' 6) UPDATE ERROR
Use DataSet and SQLDataAdaptor to '============================================================
update database Return False
End Try
End Function
23. 3. DELETE VEHICLE
23
(FUNCTION IN CLASS “CAR”)
'================================================================ '================================================================
' FUNCTION#8: DELETE CAR_INFO FROM DATABASE ' FUNCTION#10: DELETE VEHICLE FROM REGISTER TABLE (CALL WEBSERVICE)
'================================================================
'================================================================
Function DeleteVehicleFromRegisterTableAtBwm(ByVal RegNumber As String) As Boolean
Public Function DeleteCarInfo(ByVal RegNumber As String) As Boolean '============================================================
'============================================================ ' 1) RETURN VARIABLE
' 1) RETURN VARIABLE '============================================================
'============================================================ Dim ReturnFunction As Boolean = False
Dim ReturnFunction As Boolean = True Dim MyUtils As New Utils
Dim MyProxy = New BMWHOWebService.BMWHOWebService
Dim MyUtils As New Utils
'============================================================
'============================================================ ' 2) PREPARE INSERT COMMAND
' 2) PREPARE INSERT COMMAND '============================================================
'============================================================ Try
Dim SqlCmd As String = "" '========================================================
SqlCmd = "DELETE TM_CAR " ' 2.1) AUTHEN TO BWM SITE
'========================================================
SqlCmd = SqlCmd & "WHERE RegNo = '" & RegNumber & "' "
Dim Auth As New AuthenticationHeader()
'============================================================ Auth.UserName = "tutor"
' 3) EXECUTE DELETE COMMAND Auth.PassWord = "password"
'============================================================ MyProxy.AuthenticationHeaderValue = Auth
Dim cnString As String = Global.Resources.Resource.cnString '========================================================
Dim ds As New SqlDataSource ' 2.2) DELETE VEHICLE FROM REGISTER TABLE (CALL WEBSERVICE)
'========================================================
Try
ReturnFunction = MyProxy.DeleteVehicleFromRegisterTable(RegNumber)
ds.ConnectionString = cnString '============================================================
ds.DeleteCommand = SqlCmd ' 3) UPDATE SYN_STATUS OF UPDATING TO BWM_WEBSERVICE (AYSN)
ds.Delete() '============================================================
ReturnFunction = True '============================================================
Catch ' 3.1) MARK "COMPLETED" INTO DB
'============================================================
ReturnFunction = False
If ReturnFunction = True Then
End Try UpdateSynWebServiceStatus(RegNumber, "COMPLETED")
'============================================================ End If
' 4) RETURN VARIABLE '============================================================
'============================================================ ' 3.2) MARK "INCOMPLETED" INTO DB
Return ReturnFunction '============================================================
If ReturnFunction = False Then
End Function
UpdateSynWebServiceStatus(RegNumber, "INCOMPLETED")
End If
Catch
'========================================================
' 2.2) UPDATE TO WS INCOMPLETEED
'========================================================
Use SQLDataSource and SQL ReturnFunction = False
End Try
'============================================================
Command to delete data ' 4) RETURN VARIABLE
'============================================================
Return ReturnFunction
24. 4. GET CAR INFORMATION
24
(FUNCTION IN CLASS “CAR”)
'=============================================
'================================================================ ' B) GET DATA
'=============================================
' FUNCTION#4: GET CAR_INFO FROM DB While (dr.Read)
'================================================================ RegNo = dr.GetValue(0)
Public Function GetCarInfo(ByVal RegNumber As String) As Boolean BodyStyle = dr.GetValue(1)
Model = dr.GetValue(2)
'============================================================ DateRegistered = dr.GetValue(3)
' 1) DECLAR VARIABLE Mileage = dr.GetValue(4)
'============================================================ FuelType = dr.GetValue(5)
CarDescription = dr.GetValue(6)
Dim ReturnFunction As Boolean = True IsEnable = dr.GetValue(7)
'============================================================
' 2) OPEN DATABASE CONNECTION
DateAdded = dr.GetValue(8)
AddedBy = dr.GetValue(9) Use SQLDataReader
DateUpdated = dr.GetValue(10)
'============================================================
Dim cn As New System.Data.SqlClient.SqlConnection(Global.Resources.Resource.cnString)
UpdatedBy = dr.GetValue(11)
DateSold = dr.GetValue(12) to collect data
Try SoldBy = dr.GetValue(13)
Price = dr.GetValue(14)
'======================================================== SellingStatus = dr.GetValue(15)
' 3) CONNECT DATA SynStatus = dr.GetValue(16)
'======================================================== SynTime = dr.GetValue(17)
Transmission = dr.GetValue(18)
Dim sql As String = "" Colour = dr.GetValue(19)
sql = sql & "SELECT * FROM TM_CAR " InteriorDetails = dr.GetValue(20)
sql = sql & "WHERE UPPER(RegNo) = UPPER('" & RegNumber.ToString & "') " AdditionalEquipments = dr.GetValue(21)
StandardEquipments = dr.GetValue(22)
Dim cmd As New System.Data.SqlClient.SqlCommand(sql, cn) TechnicalSpecification = dr.GetValue(23)
cn.Open() CustFullName = dr.GetValue(24)
'======================================================== CustAddress = dr.GetValue(25)
CustContactNo = dr.GetValue(26)
' 4) RETRIVE DATA CustEmail = dr.GetValue(27)
'======================================================== Comment1 = dr.GetValue(28)
Dim dr As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader Comment2 = dr.GetValue(29)
End While
'======================================================== End If
' 5.1) NOT FOUND USERID '=============================================
'======================================================== ' 6) CLOSE DATABASE
'=============================================
If Not dr.HasRows Then cn.Close()
ReturnFunction = False '=============================================
End If ' 7)RETURN VALUE
'=============================================
'======================================================== Return ReturnFunction
' 5.2) FOUND USERID Catch ex As Exception
'======================================================== '========================================================
' RETURN FALSE
If dr.HasRows Then '========================================================
'=================================================== Return False
' A) FOUND USER ID '========================================================
' ANY ERROR CASE, CLOSE DB CONNECTION
'=================================================== '========================================================
ReturnFunction = True cn.Close()
End Try
Return True
End Function
25. 5 . U SER A U T HEN IC AT ION & R EG IST ER AT IO N
25
(F U N C T IO N IN C L A SS “ U SER ” )
'================================================================
' FUNCTION#1: LOGIN '================================================================
'================================================================ ' FUNCTION#3: RESGISTER_USER (PERMISSION ONLY ADMIN)
Function Login(ByVal UserName As String, ByVal UserPassword As String) As Boolean '================================================================
'============================================================ Function RegisterUser() As Boolean
' OPEN DATABASE CONNECTION '============================================================
'============================================================
' RETURN VARIABLE
Dim cn As New System.Data.SqlClient.SqlConnection(Global.Resources.Resource.cnString)
Try
'============================================================
'======================================================== Dim ReturnFunction As Boolean = True
' CONNECT DATA '============================================================
'======================================================== ' PREPARE INSERT COMMAND
Dim sql As String = "" '============================================================
sql = sql & "SELECT * FROM TM_USER " Dim MyUtils As New Utils
sql = sql & "WHERE UPPER(UserId) = UPPER('" & UserName.ToString & "') AND "
Dim SqlCmd As String = ""
sql = sql & "UPPER(UserPassword) = UPPER('" & UserPassword.ToString & "')"
Dim cmd As New System.Data.SqlClient.SqlCommand(sql, cn)
SqlCmd = "INSERT INTO TM_USER "
cn.Open() SqlCmd = SqlCmd &
'======================================================== "(UserId,UserPassword,UserRole,UserFirstName,UserLastName,UserEmail,UserDat
' SELECT VALUE eRegistered) "
'======================================================== SqlCmd = SqlCmd & " VALUES ('" & UserId & "' ,'" & UserPassword & "' ,'" &
Dim dr As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader UserRole & "' ,'" & UserFirstName & "' ,'" & UserLastName & "' ,'" & UserEmail & "' ,'"
'========================================================
& MyUtils.ConvertDateForSQL(UserDateRegistered) & "')"
' WRONG USER & PASSWORD
'========================================================
If Not dr.HasRows Then '=================================================================
Return False 'EXECUTE INSERT COMMAND
End If
'======================================================== '=================================================================
' CORRECT USER & PASSWORD Dim cnString As String = Global.Resources.Resource.cnString
'========================================================
Dim ds As New SqlDataSource
If dr.HasRows Then
Return True
Try
ds.ConnectionString = cnString
End If
cn.Close()
Catch ex As Exception
ds.InsertCommand = SqlCmd
ds.Insert()
Use
'========================================================
' RETURN FALSE
Use ReturnFunction = True
Catch
SQLDataSoruce
'========================================================
Return False
'========================================================
DataReader ReturnFunction = False
End Try and SQL
'============================================================
' ANY ERROR CASE, CLOSE DB CONNECTION
to collect data
'======================================================== ' RETURN VARIABLE Command to
cn.Close() '============================================================
End Try
Return True
Return ReturnFunction
End Function
insert data
End Function
27. 27 WEB.CONFIG
Connection Strings for
.net controls in aspx files
Web Service References
28. 28 RESOURCE.RESX
Flexible to change connection String by resource.resx file
Connection Strings
for source code in
VB files
Benefit:
Flexible to change variable of connection strings e.g. Server Name, Database Name, DB User and DB Password.
The cnString variable is used in Car and User classes