code 39 c# class XML WEB SERVICE S in C#

Encoder Code 39 in C# XML WEB SERVICE S

CHAPTER 9 XML WEB SERVICE S
Code 39 Full ASCII Maker In Visual C#
Using Barcode generation for Visual Studio .NET Control to generate, create Code 39 image in .NET framework applications.
www.OnBarcode.com
Code39 Decoder In Visual C#.NET
Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Listing 9-22. Using a SOAP Header public class Service : System.Web.Services.WebService { public User CurrentUser; [WebMethod] [SoapHeader("CurrentUser",Direction=SoapHeaderDirection.In,Required=true)] public DataSet GetEmployees() { if (CurrentUser == null) { throw new SoapHeaderException("Authentication details not found!", SoapException.ClientFaultCode); } if (CurrentUser.UserID == "Admin" && CurrentUser.Password == "password") { DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM employees", "data source=.\\sqlexpress;initial catalog=northwind;Integrated Security=True"); da.Fill(ds, "myemployees"); return ds; } else { throw new SoapException("Authentication failed!", SoapException.ClientFaultCode); } } } Examine the web service class carefully. At the top it declares a variable of type User. The GetEmployees() web method fills a DataSet with all the records from the Employees table and returns the DataSet back to the caller. The important thing to note about the GetEmployees() web method is that it is decorated with the SoapHeader attribute. This is how you inform the web method that a SOAP header is to be processed. The SoapHeader attribute specifies one parameter and two properties. The first parameter specifies the instance name of the SOAP header class that we wish to use. In our example, the name of the User variable is CurrentUser and hence that is what we pass to the web method. This instance variable must be available publicly in the web service class. The Direction property
ANSI/AIM Code 128 Generator In C#
Using Barcode creation for .NET framework Control to generate, create Code 128 Code Set C image in Visual Studio .NET applications.
www.OnBarcode.com
Print Linear 1D Barcode In C#.NET
Using Barcode maker for .NET Control to generate, create 1D image in .NET framework applications.
www.OnBarcode.com
C H APTE R 9 XM L WEB S ERVI CES
Generating UCC-128 In Visual C#.NET
Using Barcode maker for VS .NET Control to generate, create EAN 128 image in .NET framework applications.
www.OnBarcode.com
Code 3/9 Encoder In Visual C#
Using Barcode generator for .NET Control to generate, create Code39 image in VS .NET applications.
www.OnBarcode.com
indicates the direction of the SoapHeader and is of enumeration type SoapHeaderDirection. The possible values of the SoapHeaderDirection enumeration are as follows: In: The direction of In indicates that the SOAP header is passed from the client to the web service. Out: The direction of Out indicates that the SOAP header is passed from the web service to the client. InOut: The direction of InOut indicates that the SOAP header is passed to and from the web service and its client. The Required property indicates that the presence of the SOAP header is mandatory. Inside the GetEmployees() web method, we check whether the SOAP header is null. If so, this indicates that the authentication details were not sent and hence the code raises a SoapHeaderException. The SoapHeaderException class is used to represent an error in the SOAP header. The first parameter of the SoapHeaderException constructor is the error message, and the second parameter is the SOAP fault code for the client call. The code then checks the user credentials. If the credentials are correct, a DataSet is created and filled with all the records from the Employees table. Otherwise, a SoapException is raised. The SoapException class is used to represent an error with the SOAP request processing. The constructor of SoapException takes the same two parameters as the SoapHeaderException class. To consume the web service you just created, you need to develop a client application like the one shown in Figure 9-15.
Generating QR Code In Visual C#
Using Barcode drawer for .NET Control to generate, create QR Code JIS X 0510 image in .NET applications.
www.OnBarcode.com
MSI Plessey Generator In C#
Using Barcode creator for .NET Control to generate, create MSI Plessey image in .NET applications.
www.OnBarcode.com
Figure 9-15. Application that uses SOAP headers
Print Code 39 Full ASCII In None
Using Barcode encoder for Software Control to generate, create Code 39 image in Software applications.
www.OnBarcode.com
Recognize Code 39 Full ASCII In Java
Using Barcode scanner for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
CHAPTER 9 XML WEB SERVICE S
Create Barcode In Java
Using Barcode creator for Android Control to generate, create Barcode image in Android applications.
www.OnBarcode.com
Create Barcode In Java
Using Barcode maker for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
The application consists of two text boxes for specifying the user ID and password. The Get Employees button calls the GetEmployees() web method and displays the results in a DataGridView control. After you create the user interface of the application, add a web reference to the web service. Then write the code as shown in Listing 9-23 in the Click event of the Get Employees button. Listing 9-23. Passing a SOAP Header from the Client to the Web Service private void button1_Click(object sender, EventArgs e) { Service proxy = new Service(); if (textBox1.Text != "" && textBox2.Text != "") { User currentuser = new User(); currentuser.UserID = textBox1.Text; currentuser.Password = textBox2.Text; proxy.UserValue = currentuser; } try { DataSet ds = proxy.GetEmployees(); dataGridView1.DataSource = ds.Tables["myemployees"].DefaultView; } catch (SoapHeaderException ex2) { MessageBox.Show(ex2.Message + "[" + ex2.Code + "]"); } catch (SoapException ex1) { MessageBox.Show(ex1.Message + "[" + ex1.Code + "]"); } } Notice the code marked in bold. The code checks whether the user ID and password have been entered. If so, it creates an instance of the User class. Remember that this User class is created when you add a web reference to the web service. Then the UserID and Password properties of the User class are set with corresponding values from the text boxes. Next, the UserValue property of the web service proxy is set. You might be wondering where this UserValue property has come from. When you create a proxy for the web service, it automatically creates a property of the form XXXXValue, where XXXX is the name of the SOAP header class. The type of the XXXXValue property is the same as the class XXXX. After this property has been set, the GetEmployees() web method is called and the DataSet returned is bound to the DataGridView control. The code also has some exception-handling code. The try...catch block checks for two types of exceptions: SoapHeaderException and SoapException. Remember that these are the same exceptions that we raise from the web service if there is an authentication error. The
Print EAN / UCC - 14 In Java
Using Barcode drawer for Android Control to generate, create EAN128 image in Android applications.
www.OnBarcode.com
GS1 128 Generation In Objective-C
Using Barcode creation for iPad Control to generate, create EAN 128 image in iPad applications.
www.OnBarcode.com
Barcode Maker In Visual Basic .NET
Using Barcode creation for VS .NET Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Recognizing Code 39 In None
Using Barcode scanner for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
PDF417 Reader In None
Using Barcode scanner for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Generate Code 128C In Java
Using Barcode generation for Java Control to generate, create Code 128C image in Java applications.
www.OnBarcode.com
Printing Barcode In Objective-C
Using Barcode printer for iPad Control to generate, create Barcode image in iPad applications.
www.OnBarcode.com
ANSI/AIM Code 128 Scanner In C#.NET
Using Barcode decoder for .NET framework Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Copyright © OnBarcode.com . All rights reserved.