only for RuBoard |
In this section, you examine the part of the code in a client of the Address Book web service that you saw in the previous section "Building a Public Address Book Web Service."
Listing 11.11 shows a part of the code for signing into the application. Here, you call the LogonUser() method exposed by the web service.
private void Button1_Click(object sender, System.EventArgs e) { localhost.AddressBookService service = new localhost.AddressBookService(); try { //call the LogonUser method Session["token"] = service.LogonUser( TextBox1.Text.Trim(),TextBox2.Text.Trim() ); Session["userName"]= TextBox1.Text.Trim(); Session["password"]= TextBox2.Text.Trim(); Response.Redirect( "AddressBook.aspx" , true ) ; } catch(SoapException soapEx ) { if( soapEx.Detail.SelectSingleNode("// message").InnerText.Equals(AddressBookExceptions.INVALID_USER ) ) { Label3.Text = AddressBookExceptions.INVALID_USER ; } } catch(Exception ex ) { Label3.Text = ex.Message; } }
Figure 11.17 shows an Address Book Manager in an ASP.NET client of the Address Book web service.
Listing 11.12 shows the part of the code that's used to fetch the summary details of all the addresses in a user 's Address Book and display them in a table.
private void Page_Load(object sender, System.EventArgs e) { localhost.AddressBookService service = new localhost.AddressBookService(); //Delete Address Entry if( Request.QueryString["delete"]=="true" ) { int entryID = Int32.Parse( Request.QueryString["entryid"]) ; try { service.DeleteAddressEntry( Session["token"].ToString(),entryID ); } catch(SoapException soapEx ) { //retry if( soapEx.Detail.SelectSingleNode("// message").InnerText.Equals(AddressBookExceptions.INVALID_USER) ) { Session["token"] = service.LogonUser( Session["userName"].ToString(), Session["password"].ToString() ); try { service.DeleteAddressEntry( Session["token"].ToString(),entryID ); } catch( Exception innerEx ) { Label2.Text = innerEx.Message; } } } catch(Exception ex ) { Label2.Text = ex.Message; } } //Get AddressList and display try { addrList = service.GetAddressList( Session["token"].ToString() ); DisplayAddressList(); } catch(SoapException soapEx ) { //retry if(soapEx.Detail.SelectSingleNode("// message").InnerText.Equals(AddressBookExceptions.INVALID_USER ) ) { Session["token"] = service.LogonUser(Session["userName"].ToString(), Session["password"].ToString() ); try { addrList = service.GetAddressList( Session["token"].ToString() ); DisplayAddressList(); } catch( Exception innerEx ) { Label2.Text = innerEx.Message; } } } catch(Exception ex ) { Label2.Text = ex.Message; } } //Display addresses in a table private void DisplayAddressList() { HyperLink link ; for( int i=0 ;i <addrList.Length ;i++) { link = new HyperLink(); link.Text = addrList[i].NickName; link.NavigateUrl = "AddressEntry.aspx?edit=true&entryid=" + addrList[i].EntryID; TableRow row = new TableRow(); row.Font.Name = "Verdana"; row.Font.Size = 10; TableCell cell = new TableCell(); cell.Controls.Add(link); row.Cells.Add(cell); cell = new TableCell(); cell.Text = addrList[i].FirstName + " " + addrList[i].LastName; row.Cells.Add(cell); cell = new TableCell(); cell.Text = addrList[i].EmailID; row.Cells.Add(cell); cell = new TableCell(); link = new HyperLink(); link.Text = "delete"; link.NavigateUrl = "AddressBook.aspx?delete=true&entryid=" + addrList[i].EntryID; cell.Controls.Add(link); row.Cells.Add(cell); Table1.Rows.Add(row); } }
The AddAddressEntry() method adds new address entries to your Address Book, and the UpdateAddressEntry() method updates your existing address entries. In screen shown in Figure 11.17, if the user clicks on the "New Address" link, the Create/Edit screen appears in the create mode with all the fields empty. If the user clicks on an existing nickname, the Create/Edit screen appears in the edit mode.
Figure 11.18 shows an example of the Create/Edit screen of the Address Book in the Edit mode, when the user clicks on the nickname 'AshwinK' as seen in the Figure 11.17.
Listing 11.13 shows the code to display the Create/Edit screen and adding or updating address information.
In the edit mode, the address entry details for the specified nickname are retrieved into the addrEntry object using the GetAddressEntry () method. The InitAddressEntryForm() method, that is not shown here, initializes all the address fields in the form using the addrEntry object .
After updating the address information in the case of edit mode or entering new address information in the case of create mode, when the user clicks the Save button the Button1_Click event is fired . The event handler calls the UpdateAddressEntry() or the AddAddressEntry() method to update or add new address information respectively.
private void Page_Load(object sender, System.EventArgs e) { Response.Cache.SetExpires( DateTime.Now ); if( Request.QueryString["edit"]== "true" && !Page.IsPostBack ) { localhost.AddressBookService service = new localhost.AddressBookService(); int entryID = Int32.Parse( Request.QueryString["entryid"]) ; try { addrEntry = service.GetAddressEntry( Session["token"].ToString() , entryID ); InitAddressEntryForm(); } catch(SoapException soapEx ) { //retry if( soapEx.Detail.SelectSingleNode("// message").InnerText.Equals(AddressBookExceptions.INVALID_USER ) ) { Session["token"] = service.LogonUser(Session["userName"].ToString(), Session["password"].ToString()); try { addrEntry = service.GetAddressEntry(Session["token"].ToString() , entryID ); InitAddressEntryForm(); } catch( Exception innerEx ) { Label_Message.Text = innerEx.Message; } } } catch(Exception ex ) { Label_Message.Text = ex.Message; } } } //Save button click private void Button1_Click(object sender, System.EventArgs e) { bool saved = false; localhost.AddressBookService service = new localhost.AddressBookService(); InitAddressEntry(); try {//Create if( addrEntry.EntryID == 0) { saved = service.AddAddressEntry( Session["token"].ToString(),addrEntry ); } else //update { saved = service.UpdateAddressEntry(Session["token"].ToString(),addrEntry ); } } catch(SoapException soapEx ) { //retry if( soapEx.Detail.SelectSingleNode("// message").InnerText.Equals(AddressBookExceptions.INVALID_USER ) ) { Session["token"] = service.LogonUser(Session["userName"].ToString(), Session["password"].ToString() ); try { Label_Message.Text += "-" + addrEntry.EntryID; if( addrEntry.EntryID == 0)//Create saved = service.AddAddressEntry(Session["token"].ToString(),addrEntry ); else //update saved = service.UpdateAddressEntry(Session["token"].ToString(),addrEntry ); } catch( Exception innerEx ) { Label_Message.Text = innerEx.Message; } } } catch(Exception ex ) { Label_Message.Text = ex.Message; } if( saved ) Response.Redirect("AddressBook.aspx"); } }
only for RuBoard |