SlideShare a Scribd company logo
1 of 7
Download to read offline
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. 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.
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
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.
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.
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()
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.

More Related Content

Similar to Web services in asp.net

Cis407 a ilab 1 web application development devry university
Cis407 a ilab 1 web application development devry universityCis407 a ilab 1 web application development devry university
Cis407 a ilab 1 web application development devry university
lhkslkdh89009
 
Cis 407 i lab 1 of 7
Cis 407 i lab 1 of 7Cis 407 i lab 1 of 7
Cis 407 i lab 1 of 7
helpido9
 
Foundation and PathwaysCOS10020 Creating Web Application.docx
Foundation and PathwaysCOS10020 Creating Web Application.docxFoundation and PathwaysCOS10020 Creating Web Application.docx
Foundation and PathwaysCOS10020 Creating Web Application.docx
hanneloremccaffery
 
Oracle apex hands on lab#2
Oracle apex hands on lab#2Oracle apex hands on lab#2
Oracle apex hands on lab#2
Amit Sharma
 
windows azure cloud services - Lab Manual
windows azure cloud services - Lab Manualwindows azure cloud services - Lab Manual
windows azure cloud services - Lab Manual
Arun Kumar
 
Getting started-with-oracle-so a-iv
Getting started-with-oracle-so a-ivGetting started-with-oracle-so a-iv
Getting started-with-oracle-so a-iv
Amit Sharma
 
Getting started-with-oracle-so a-iv
Getting started-with-oracle-so a-ivGetting started-with-oracle-so a-iv
Getting started-with-oracle-so a-iv
Amit Sharma
 

Similar to Web services in asp.net (20)

Windows Communication Foundation (WCF) programming using Visual Studio
Windows Communication Foundation (WCF) programming using Visual StudioWindows Communication Foundation (WCF) programming using Visual Studio
Windows Communication Foundation (WCF) programming using Visual Studio
 
Cis407 a ilab 1 web application development devry university
Cis407 a ilab 1 web application development devry universityCis407 a ilab 1 web application development devry university
Cis407 a ilab 1 web application development devry university
 
Cis 407 i lab 1 of 7
Cis 407 i lab 1 of 7Cis 407 i lab 1 of 7
Cis 407 i lab 1 of 7
 
Foundation and PathwaysCOS10020 Creating Web Application.docx
Foundation and PathwaysCOS10020 Creating Web Application.docxFoundation and PathwaysCOS10020 Creating Web Application.docx
Foundation and PathwaysCOS10020 Creating Web Application.docx
 
Lecture2 interactive-start
Lecture2 interactive-startLecture2 interactive-start
Lecture2 interactive-start
 
Build your first rpa bot using IBM RPA automation
Build your first rpa bot using IBM RPA automationBuild your first rpa bot using IBM RPA automation
Build your first rpa bot using IBM RPA automation
 
Murach : How to develop a single-page MVC web
Murach : How to develop a single-page MVC web Murach : How to develop a single-page MVC web
Murach : How to develop a single-page MVC web
 
Oracle apex hands on lab#2
Oracle apex hands on lab#2Oracle apex hands on lab#2
Oracle apex hands on lab#2
 
windows azure cloud services - Lab Manual
windows azure cloud services - Lab Manualwindows azure cloud services - Lab Manual
windows azure cloud services - Lab Manual
 
Creating & consuming simple web service
Creating & consuming simple web serviceCreating & consuming simple web service
Creating & consuming simple web service
 
Installing Process Oracle 10g Database Software on Windows 10
Installing Process Oracle 10g Database Software on Windows 10Installing Process Oracle 10g Database Software on Windows 10
Installing Process Oracle 10g Database Software on Windows 10
 
Getting started-with-oracle-so a-iv
Getting started-with-oracle-so a-ivGetting started-with-oracle-so a-iv
Getting started-with-oracle-so a-iv
 
Getting started-with-oracle-so a-iv
Getting started-with-oracle-so a-ivGetting started-with-oracle-so a-iv
Getting started-with-oracle-so a-iv
 
ASP DOT NET
ASP DOT NETASP DOT NET
ASP DOT NET
 
GigaSpaces CCF Quick Tour - 2.3.6
GigaSpaces CCF Quick Tour - 2.3.6GigaSpaces CCF Quick Tour - 2.3.6
GigaSpaces CCF Quick Tour - 2.3.6
 
How to Migrate IIS Website to Another Server.pdf
How to Migrate IIS Website to Another Server.pdfHow to Migrate IIS Website to Another Server.pdf
How to Migrate IIS Website to Another Server.pdf
 
Open microsoft visual studio/tutorialoutlet
Open microsoft visual studio/tutorialoutletOpen microsoft visual studio/tutorialoutlet
Open microsoft visual studio/tutorialoutlet
 
Creating a dot netnuke
Creating a dot netnukeCreating a dot netnuke
Creating a dot netnuke
 
Mca 504 dotnet_unit5
Mca 504 dotnet_unit5Mca 504 dotnet_unit5
Mca 504 dotnet_unit5
 
Details Description of Load Runner Recording option
Details Description of  Load Runner Recording optionDetails Description of  Load Runner Recording option
Details Description of Load Runner Recording option
 

Recently uploaded

%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Recently uploaded (20)

%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 

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.