Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Web services in asp.net
1. Web Services in Asp.net (VB Code)
1. Run Visual Web Developer / Visual Studio (I am using Visual Studio 2013)
2. Click on New project.
3. From the left-side pane, choose Templates → Visual Basic → Web
4. Choose Asp.net web application from the central window. Give some file name and
choose your convenient drive and a folder.
5. Click OK to proceed.
6. Choose Empty from ‘Select a Template’ screen.
Stage 1: Creating Web Service
1. Right Click on Project (from the Solution Explorer). Select Add→New Item →
Web Service (ASMX)
2. 2. Give some name for this webservice (or retain the default name suggested by
your system). I have retained the default name ie. WebService1.asmx. Click on
Add button. The system switch over to the screen with the following codes.
3. Now, concentrate on the section starting with the line <WebMethod()>_
4. Here you can write your own codes. Now, let us try ourselves.
5. Let us add two methods in this section to add and subtract two numbers.
a. First remove the lines Public Function ….. End Function
b. Add the following lines as shown below (I have added two methods – add
and subtract). See the code below.
3. 6. Now, you can check the above codes for any errors. For this purpose, click on
Build → Build Solution.
7. You can see message is 1 succeeded, 0 failed ….. This shows that the new codes
added is OK.
8. You can also run your web service code in a browser. For this, right click on the
screen and choose ‘Run to Cursor’. The program runs in your default browser.
9. Notice our two methods (functions) namely, add and subtract are appearing on
the screen. Click on them and supply two numeric values to test whether these
functions are working properly or not.
10. That’s it. We have created a web service with two functions – one for addition of
two numbers and another for subtracting two numbers.
Stage 2: Importing this webservice to our website
Now, let us see how to import the Webservice created in the above to our website.
1. Stop the debugging of the above webservice, if any by pressing Shift+F5 or clicking
on the stop debugging button as shown below
4. 2. Right click on the Project (Visible in the Solution Explorer window) → Add → Service
Reference.
3. Click on the Discover button
4. You can see WebService1.asmx appears on the Services window.
5. Expand all the nodes to view Add and Subtract functions in the Operations Window.
5. 6. Press Ok to save.
Stage 3: Consuming the WebService from a web page
Now, let us see, how to consume (or use in a web page)
1. Right click on Project → Add → New Item → Web Form
2. Change to design mode.
3. Add Two Text Boxes, Two Labels and a Button as shown below.
4. Double click on the Button. Add codes for the Button1 click event.
5. Save the webpage. Go back to design mode. View your webpage in a browser (ie.
Right click on your webpage and select View in Browser option).
Note: When you are in debug mode, you cannot view the webpage in a browser. For
this purpose, close the debug mode by pressing Shift+F5 buttons)
That’s it. You have created a webservice and a webpage which consume it. I have used
Visual Basic codes for the above. It can also be developed using C# with few modifications.
Webservices with Database Connectivity:
I have given below code required to connect a back-end database (in this case, I have used MS SQL
Server) from a web services.
6. Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Data.SqlClient
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following
line.
' <System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class WebService
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function HelloWorld() As String
Return "Hello World"
End Function
<WebMethod()> _
Public Function connect(vcode as string ) As String
Dim connection As SqlConnection
Dim command1 As SqlCommand
Dim dr1 As SqlDataReader
Dim sql1, sql2 As String
connection = New SqlConnection("Data Source=xxxx;Initial Catalog=xxxx;User
ID=xxxx;Password=xxxx")
connection.Open()
7. Dim vcode as As String
sql1 = "select VendorName from vendorMaster where vendorCode='" + vcode + "'"
command1 = New SqlCommand(sql1, connection)
dr1 = command1.ExecuteReader
While dr1.Read
Return (dr1("VendorName").ToString)
End While
End Function
End Class
--------------------------------
Happy coding.
- BK Dharmaraju, Bangalore, India.
dharma.rmm@gmail.com
15th
Apr 2020.