window| Control | Browser uses Windows Forms control in IE browser (ii)
Author:: Thiru Thangarathinam
Translation: Autumn Maple
Accessing Web Service through Windows Forms controls
A key advantage of form controls is that you can implement rich user information on the client. For example, you can use a form control to access a Web service so that it is displayed in IE without having to refresh the page again. To illustrate this, let's first create a Web service and then show you how to invoke the Web service through a form control.
Create a Web Service
We created a new Visual C # asp.net Web service project with the name AuthorsWebService.
After the establishment, we modify the service class name to AuthorsService, while adding a GetAuthors method inside the class, the code for the method is as follows:
SqlConnection sqlconn = new SqlConnection (connstring);
DataSet dstauthors = new DataSet ("Authors");
SqlDataAdapter adapter = new SqlDataAdapter ("Select * from Authors", sqlconn);
Adapter. Fill (dstauthors, "Author");
Sqlconn.close ();
Sqlconn.dispose ();
return dstauthors;
}
The code for the above method is relatively simple, we store the database connection string on the appsettings node of the Web.config file, as follows:
In the code above,
We created a sqlconnnection instance and passed the connection string above as a parameter. Then create a SqlDataAdapter object, passing in two parameters-query string and sqlconnnection instance. Call the Fill method of the SqlDataAdapter instance to execute the database query and populate the DataSet with the results. Now that we've finished creating the Web service, we're going to build one of his client calls.
To create a Windows Forms control as a Web service client
Here, we want to use a form control to invoke the Web Service, so create a new Visual C # form control project named Authorswebserviceclientcontrol:.
When you are done, modify the default user control name called Authorscontrol. We added a DataGrid control above the name Gridauthors also add a button named Btnclick. The Click event for the registration button, in the event handler function, will call WebService. Before that we need to add a Web reference for the project and enter the Web service address that you just created. The editor generates the proxy for the service, and the Add Web Service reference is as follows:
Figure
After the service agent is created, we add code to the button event:
}
In the code above, we created an instance of the Web service proxy and called the GetAuthors method to assign the returned dataset to the Gridauthors DataSource property. Then compile the form control, and then configure the virtual directory.
Creating HTML pages and creating virtual paths
In this step, we create an HTML page to use the Authorswebserviceclientcontrol created above. Here's his code:
Now we need to create a virtual directory to make the control work and put authorsdisplay.htm and AuthorsWebServiceClientControl.dll together. Open the browser and enter the address, you will see a button and an empty DataGrid, if you click the command button, the control will call the Web service and write the result in the DataGrid. The results of the page are shown below.
In the next section, we'll look at debugging the process.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.