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

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 StudioDharma Raju
 
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 universitylhkslkdh89009
 
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 7helpido9
 
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.docxhanneloremccaffery
 
Lecture2 interactive-start
Lecture2 interactive-startLecture2 interactive-start
Lecture2 interactive-startDennis Seidel
 
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 automationWinton Winton
 
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 MahmoudOHassouna
 
Oracle apex hands on lab#2
Oracle apex hands on lab#2Oracle apex hands on lab#2
Oracle apex hands on lab#2Amit Sharma
 
windows azure cloud services - Lab Manual
windows azure cloud services - Lab Manualwindows azure cloud services - Lab Manual
windows azure cloud services - Lab ManualArun Kumar
 
Creating & consuming simple web service
Creating & consuming simple web serviceCreating & consuming simple web service
Creating & consuming simple web serviceAbhijit B.
 
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 10Azharul Islam Shopon
 
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-ivAmit 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-ivAmit Sharma
 
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.6Shay Hassidim
 
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.pdfHost It Smart
 
Open microsoft visual studio/tutorialoutlet
Open microsoft visual studio/tutorialoutletOpen microsoft visual studio/tutorialoutlet
Open microsoft visual studio/tutorialoutletMitchinson
 
Creating a dot netnuke
Creating a dot netnukeCreating a dot netnuke
Creating a dot netnukeNguyễn Anh
 
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 optionSanchita Singha
 

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

Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 

Recently uploaded (20)

Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
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.