To create the QueryTrackerClient application, you need to create the Executive Login and the Query Tracker Client windows , as well as the InitializeQueryProvider module.
The ExecutiveLogin class defines the Executive Login window.
Listing 4-10 shows the code for the frmExecutiveLogin.vb file that defines the ExecutiveLogin class:
Imports System.Data.SqlClient Imports System.IO Imports System.Collections Imports System.Runtime.Remoting.Channels Public Class ExecutiveLogin Inherits System.Windows.Forms.Form Private Sub cmdGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGo.Click 'Calling the Login function executiveId = objQueryProvider.login(txtUserName.Text, txtPassWord.Text) 'Checking whether the login is succesful If executiveId <> 0 Then boolLoginConfirmaion = True strUserName = txtUserName.Text Dim objQueryTrackerClient As New QueryTrackerClient objQueryTrackerClient.ShowDialog() End If Me.Close() End Sub End Class
Download this Listing .
Figure 4-9 shows the output of Listing 4-10:
The QueryTrackerClient class defines the Query Tracker Client window.
Listing 4-11 shows the code for the frmQueryTrackerClient.vb file that defines the QueryTrackerClient class:
'This class provides methods to allow the executives to view the queries meant 'for them and mark them as attended when they have attended the query Public Class QueryTrackerClient Inherits System.Windows.Forms.Form 'Dataset variable to receive the dataset from queryprovider getQuery method Dim ds As DataSet 'Dataset variable to return the dataset containing the queries that 'has to be marked as attended in the QueryTracker database Dim updateds As New DataSet 'A datatable to store the QueryId of the queries that has been marked as attended by the executive Dim queryTable As New DataTable("Queries") Private Sub QueryTrackerClient_Closing(ByVal sender As Object, ByVal e As _ System.ComponentModel.CancelEventArgs) Handles MyBase.Closing If queryTable.Rows.Count = 0 Then Exit Sub End If 'Assigning the queryTable datatable to updateds dataset updateds.Tables.Add(queryTable) objQueryProvider.updateQuery(updateds) End Sub Private Sub cmdAttended_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles cmdAttended.Click 'Check whether a query has been selected from the list view ListViewQuery If ListViewQuery.SelectedItems.Count = 0 Then MsgBox("Please select a query first", MsgBoxStyle.OKOnly, "WAIT HERE") ListViewQuery.Focus() Exit Sub End If 'Marking the selected query as attended ListViewQuery.SelectedItems.Item(0).SubItems(6).Text = "A" Try If queryTable.Rows.Count = 0 Then 'Creating the queryTable DataTable to contain the QueryId of the 'queries to be marked as attended Dim queryDataColumn As New DataColumn queryDataColumn.DataType = System.Type.GetType("System.Int32") queryDataColumn.ColumnName = "QueryId" queryTable.Columns.Add(queryDataColumn) Dim querydataRow As DataRow = queryTable.NewRow querydataRow.Item(0) = ListViewQuery.SelectedItems.Item(0).Text queryTable.Rows.Add(querydataRow) Else 'Adding new row to queryTable DataTable Dim querydataRow As DataRow = queryTable.NewRow querydataRow.Item(0) = ListViewQuery.SelectedItems.Item(0).Text queryTable.Rows.Add(querydataRow) End If Catch ex As Exception End Try End Sub Private Sub QueryTrackerClient_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load If boolLoginConfirmaion = True Then 'Splashing a welcome message to the executive lblWelcome.Text = "Welcome " & InitializeQueryProvider.strUserName 'Retrieving the queries for the executive ds = objQueryProvider.getQuery(executiveId) Try If ds.Tables(0).Rows.Count = 0 Then lblWelcome.Text = lblWelcome.Text & " : " & "No Queries For You." ElseIf ds.Tables(0).Rows.Count = 1 Then lblWelcome.Text = lblWelcome.Text & " : " & _ ds.Tables(0).Rows.Count & " Query For You." Else lblWelcome.Text = lblWelcome.Text & " : " & _ ds.Tables(0).Rows.Count & " Queries For You." End If 'Enabling the cmdAttended command button cmdAttended.Enabled = True Dim rowcount As Integer Dim tempListViewItem As ListViewItem 'Populating ListViewQuery with Query information retrieved by the dataset For rowcount = 0 To ds.Tables(0).Rows.Count - 1 tempListViewItem = ListViewQuery.Items.Add(ds.Tables(0).Rows(rowcount).Item(0)) tempListViewItem.SubItems.Add(rowcount + 1) tempListViewItem.SubItems.Add(ds.Tables(0).Rows(rowcount).Item(1)) tempListViewItem.SubItems.Add(ds.Tables(0).Rows(rowcount).Item(2)) tempListViewItem.SubItems.Add(ds.Tables(0).Rows(rowcount).Item(4)) tempListViewItem.SubItems.Add(ds.Tables(0).Rows(rowcount).Item(3)) 'marking the query as unattended tempListViewItem.SubItems.Add("U") Next Catch ex As Exception MsgBox("Unable to retrieve queries", MsgBoxStyle.OKOnly, "Retrieval Error") End Try Else Me.Close() End If End Sub Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click Me.Close() End Sub End Class
Download this Listing .
The above listing defines the following methods:
QueryTrackerClient_Load() : Retrieves and loads the queries specified for the executive in the ListViewQuery list view control.
cmdAttended_Click() : Executes when an executive selects a query from the list and clicks on the Mark as Attended button to mark that query as attended to. This method updates the query status in the list as ˜A .
QueryTrackerClient_Closing() : Executes when an executive closes the Query Tracker Client window. This method makes a request to the remoting object to update all the queries marked as attended.
Figure 4-10 shows the output of Listing 4-11:
The InitializeQueryProvider.vb file defines the InitializeQueryProvider module.
Listing 4-12 shows the code for the InitializeQueryProvider.vb file:
Imports System.Runtime.Remoting.Channels Imports System.Runtime.Remoting.Channels.Tcp Module InitializeQueryProvider 'A boolean variable to store the login status of the executive Public boolLoginConfirmaion As Boolean 'A string variable to store the user name of the executive Public strUserName As String 'A channelservices variable to register the tcp channel Public cs As ChannelServices 'A tcpchannel variable to create an instance of tcpchannel Public tcpchannel As New tcpchannel 'An instance of queryprovider remoting object Public objQueryProvider As New QueryTrackerRemotingObject.QueryProvider 'A variable to hold the executive id returned by the login function provided by QueryProvider Public executiveId As Integer Sub main() Try 'creating and registering tcp channel and as this is client you do not need to specify the port tcpchannel = New tcpchannel 'registering the channel cs.RegisterChannel(tcpchannel) 'create an instance of the remote object objQueryProvider = Activator.GetObject(objQueryProvider.GetType, _ "tcp://192.168.0.12:8080/QueryProvider") 'Opening the frmExecutiveLogin form Dim objExecutiveLogin As New ExecutiveLogin objExecutiveLogin.ShowDialog() Catch ex As Exception MsgBox("Unable to locate the server", MsgBoxStyle.OKOnly, "Server not found") End Try End Sub End Module
Download this Listing .
In the above listing, the main() function creates a TCP channel as well as an instance of the remoting object, and invokes the Executive Login window.