We are going to create two services in which first we will calculate simple interest, and next we will bind repeater control with SQL database table.
Step 1: Create a WCF Service Application,
- Open Visual Studio
- File, New, then Project
- Select WCF Service Application.
- Enter the name of application as "WcfTst".
- Click OK.
Step 2: Add New WCF service file.
- Right click on the Service in the solution explorer.
- Go to Add and select New Item.
- Select WCF Service, write the service name as "TstSI" and click on Add button.
Step 3: Create a Service
- Open TstSI.svc file as in the following screenshot and also write the following code.
TstSI.svc.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.Text;
- using System.Data.SqlClient;
- using System.Data;
- namespace WcfTst
- {
- public class TstSI: ITstSI
- {
- string con = "Data Source=BITS-PC;Initial Catalog=MVCDB;Integrated Security=True;";
- public int CalculateSI(int p, int r, int t)
- {
- int SI;
- SI = (p * r * t) / 100;
- return SI;
- }
- public DataTable GetRecordList()
- {
- SqlDataAdapter da = new SqlDataAdapter();
- da = new SqlDataAdapter("select * from Users", con);
- DataSet ds = new DataSet();
- DataTable dt = new DataTable();
- da.Fill(ds);
- if (ds.Tables[0].Rows.Count > 0)
- {
- dt = ds.Tables[0];
- }
- return dt;
- }
- }
- }
Now, open ITstSI.svc.cs and write the code as follows:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.Text;
- using System.Data;
- namespace WcfTst
- {
- [ServiceContract]
- public interface ITstSI
- {
- [OperationContract]
- int CalculateSI(int p, int r, int t);
- [OperationContract]
- DataTable GetRecordList();
- }
- }
Our WCF service has been completed. Press F5 to run the service.
Right click on the service URL as given in the above image and copy address.
Paste the copied service URL anywhere or keep in mind that we have to use it.
Step 4: Now, create a new ASP.NET web application to integrate that service.
- Open Visual Studio
- File, New, then Project.
- Select ASP.NET Empty web Application.
- Enter the name of application as "TstWebWcf".
- Click OK.
- Add new web page with the name "Default.aspx" and write the following code:
Default.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TstWebWcf.Default" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
-
- <head runat="server">
- <title></title>
- </head>
-
- <body>
- <form id="form1" runat="server">
- <div class="BgGrid" style="margin:100px;">
- <table>
- <asp:Repeater ID="grduser" runat="server">
- <HeaderTemplate>
- <tr>
- <th style="margin:5px,auto;text-align:center">First Name</th>
- <th style="margin:5px,auto;text-align:center">Last Name</th>
- <th style="margin:5px,auto;text-align:center">Email</th>
- </tr>
- </HeaderTemplate>
- <ItemTemplate>
- <tr>
- <td>
- <%#Eval("FirstName") %>
- </td>
- <td>
- <%#Eval("LastName") %>
- </td>
- <td>
- <%#Eval("Email") %>
- </td>
- </tr>
- </ItemTemplate>
- </asp:Repeater>
- </table>
- </div>
- <div id="dvSI" style="margin:100px,20px;">
- <asp:TextBox ID="txtP" runat="server" Placeholder="Enter Principle"></asp:TextBox>
- <asp:TextBox ID="txtR" runat="server" Placeholder="Enter Rate"></asp:TextBox>
- <asp:TextBox ID="txtT" runat="server" Placeholder="Enter Time"></asp:TextBox>
- <asp:Button ID="btnSmt" Text="Calculate" OnClick="btnSmt_click" runat="server" /> </div>
- <div style="margin:50px;">
- <asp:Label ID="lblval" runat="server"></asp:Label>
- </div>
- </form>
- </body>
-
- </html>
Right click on the "
References" and choose "
Add Service Reference" in
Solution Explorer as in the following screenshot.
Paste the URL which you had copied during service creation in the address bar as in the following screenshot.
When you add this service reference, the end point will automatically create in web.config file as given below.
- <system.serviceModel>
- <bindings>
- <basicHttpBinding>
- <binding name="BasicHttpBinding_ITstSI" /> </basicHttpBinding>
- </bindings>
- <client>
- <endpoint address="http://localhost:3125/TstSI.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ITstSI" contract="MyWcfService.ITstSI" name="BasicHttpBinding_ITstSI" /> </client>
- </system.serviceModel>
Note: If end point is not added in web.config file, then create an end point.
Now, open Default.aspx.cs file and write the code as given below.
Default.aspx.cs - using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using TstWebWcf.MyWcfService;
- using System.Data;
- namespace TstWebWcf
- {
- public partial class Default: System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- DataTable dt = new DataTable();
- MyWcfService.TstSIClient siclnt = new TstSIClient();
- dt = siclnt.GetRecordList();
- if (dt.Rows.Count > 0)
- {
- grduser.DataSource = dt;
- grduser.DataBind();
- }
- }
- }
- protected void btnSmt_click(object sender, EventArgs e)
- {
- MyWcfService.TstSIClient siclnt = new TstSIClient();
- int tst = siclnt.CalculateSI(Convert.ToInt32(txtP.Text), Convert.ToInt32(txtR.Text), Convert.ToInt32(txtT.Text));
- lblval.Text = "Simple Intrest : " + Convert.ToString(tst);
- }
- }
- }
Press F5 to run the code.