Using Web Services with Web Forms Clients

Team-Fly team-fly    

 
ADO.NET Programming in Visual Basic .NET
By Steve  Holzner, Bob  Howell

Table of Contents
Chapter 10.   Building XML Web Services


Using Web Services with Web Forms Clients

Web Forms clients to Web Services sounds redundant. Why not just access the data directly in the Web Form? It is transparent to the user anyway, right? Yes, but you are missing the point of Web Services. What if a Web Service existed that was published by one of your company's vendors ? The Web Service enabled you as a programmer to access data about your company's order status with this vendor. You can present this data in any way you wish. Suppose you had a requirement to build a robust Windows interface for use in-house, but needed a web interface as well for your road warriors to use. By using the Web Service you can build both interfaces and they will conform to the business rules defined by the vendor.

To keep it simple, we will use the same Web Form we used in the previous chapter. We will merely change the code to use the Web Service we just built for our Windows Form. To refresh our memories, Figure 10.22 shows the form we will use from ADOBook09-02.

Figure 10.22. The Web Form for the Web Service.

graphics/10fig22.jpg

For this project, create a new project in the same solution we have been using, with the Web Service and the Windows Form in it. Next, go to Windows Explorer and copy the Web Form from ADOBook09-02 into ADOBook10-03. Now in the Solution Explorer, include the form in the project using the Show All Files button. (We've done this before, right?) Now add the web reference as we did in the Windows Forms project. It's the same process.

Next, make this the startup project and the Web Form the startup form. You can delete the default Web Form that was created when we created the project. Now let's do something drastic. Open the Web Form in Design mode. Delete the DataAdapters and the Connection object. Add a new DataSet as a dsOrders type the same way we did in the Windows Form. Now go through the code and change the references to dsCust1 and dsCustList1 to reference the classes from the Web Service, just like in the Windows Form. There is one change: The Orders table is no longer in dsCust1. It is now in dsOrders1. You should change the code to reflect this.

Now search for each reference to the DataAdapters. We must change these to get the data from and update it to the Web Service. The whole program follows . We have already done all of this so I don't think it needs much explanation.

 Private mWebSvc As localhost.CustomersOrders = New  localhost.CustomersOrders()    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As  System.EventArgs) Handles MyBase.Load      If Not IsPostBack Then        Session("IsAdding") = False        DsCust1 = New localhost.dsCust()        Session("dscust1") = DsCust1.Copy        DsOrders1 = New localhost.dsOrders()        Session("dsorders1") = DsOrders1.Copy        DsCustList1 = New localhost.dsCustList()        Session("dscustlist1") = DsCustList1.Copy      Else        DsCust1 = CType(Session("dscust1"), localhost.dsCust).Copy        DsOrders1 = CType(Session("dsorders1"), localhost.dsOrders).Copy      End If    End Sub    Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As  System.Object, ByVal e As System.EventArgs) Handles  DropDownList1.SelectedIndexChanged      If Me.IsPostBack Then        Session("selIndex") = DropDownList1.SelectedIndex  DsCust1.Merge(mWebSvc.GetCustomer(DropDownList1.SelectedItem.Value))  DsOrders1.Merge(mWebSvc.GetOrders(DropDownList1.SelectedItem.Value))        Me.DataBind()      End If    End Sub    Private Sub Page_PreRender(ByVal sender As Object, ByVal e As  System.EventArgs) Handles MyBase.PreRender      If IsPostBack Then        DsCustList1 = Session("CustList")        DropDownList1.DataBind()        DropDownList1.Items(Session("selIndex")).Selected = True      Else        DsCustList1.Merge(mWebSvc.GetCustomerList())        Session("CustList") = DsCustList1        DropDownList1.DataBind()      End If      Session("dsCust1") = DsCust1.Copy    End Sub    Private Sub ClearScreen()      TextBox1.Text = ""      TextBox3.Text = ""      lblError.Text = ""    End Sub    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As  System.EventArgs) Handles Button4.Click      DsCustList1.Clear()      DsCustList1.Merge(mWebSvc.GetCustomerList())      Session("CustList") = DsCustList1      DropDownList1.DataBind()    End Sub    Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal  e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles  DataGrid1.PageIndexChanged      DsCust1 = CType(Session("dscust1"), localhost.dsCust).Copy      DataGrid1.CurrentPageIndex = e.NewPageIndex      DataGrid1.DataBind()    End Sub    Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As  System.Web.UI.WebControls.DataGridCommandEventArgs) Handles  DataGrid1.EditCommand      DataGrid1.EditItemIndex = e.Item.ItemIndex      DataGrid1.DataBind()    End Sub    Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e  As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles  DataGrid1.UpdateCommand      Dim i As Integer      Dim dsTemp As localhost.dsOrders      Try        ' This only works if the order of the grid columns is the same as  the        ' order of the columns in the DataTable.        For i = 3 To e.Item.Cells.Count - 1          If Not DsOrders1.Orders.Columns(i - 3).ReadOnly Then            DsOrders1.Orders.Rows(e.Item.ItemIndex)(i - 3) =  CType(e.Item.Cells(i).Controls(0), TextBox).Text          End If        Next        dsTemp = DsOrders1.GetChanges(DataRowState.Modified)        DsOrders1.Merge(mWebSvc.UpdateOrders(dsTemp))      Catch errobj As Exception        lblError.Text = errobj.Message & vbCrLf & errobj.StackTrace        Exit Sub      End Try      DataGrid1.EditItemIndex = -1      DataGrid1.DataBind()    End Sub    Private Sub DataGrid1_CancelCommand(ByVal source As Object, ByVal e  As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles  DataGrid1.CancelCommand      DataGrid1.EditItemIndex = -1      DataGrid1.DataBind()    End Sub    Private Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal e  As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles  DataGrid1.DeleteCommand      Dim dsTemp As localhost.dsOrders      Try        DsOrders1.Orders.Rows(e.Item.ItemIndex).Delete()        dsTemp = DsOrders1.GetChanges(DataRowState.Deleted)        DsOrders1.Merge(mWebSvc.UpdateOrders(dsTemp))      Catch errobj As Exception        lblError.Text = errobj.Message & vbCrLf & errobj.StackTrace        Exit Sub      End Try      DataGrid1.DataBind()    End Sub 

As you can see, we are using the same web methods we used in the Windows Forms project, combined with the techniques we use for Web Forms, such as saving session state, and so on. The great thing is that your web programmers do not have to know any SQL at all! Just document your Web Service well and give them what they need.


Team-Fly team-fly    
Top


ADO. NET Programming in Visual Basic. NET
ADO.NET Programming in Visual Basic .NET (2nd Edition)
ISBN: 0131018817
EAN: 2147483647
Year: 2005
Pages: 123

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net