How to convert Fahrenheit to Celsius or Celsius to Fahrenheit through ASP.NET Web service?


In this article I am going to explain about how to convert Fahrenheit to Celsius or Celsius to Fahrenheit. I have create separate service for this and call that service to find exact values.

Description :


Here I have create one web service class inside that create two web methods to calculate Fahrenheit to Celsius or Celsius to Fahrenheit.

Service class

Create web service class using below code and defined two methods and calculation

using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.Services.Protocols;

///
/// Convert Fahrenheit To Celsius by Ravindran on 12/11/2012
///

[WebService(Namespace = "http://localhost:2323/04WsExample/WebService.asmx")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Convert_FarToCel_CelFar : System.Web.Services.WebService
{
[WebMethod(BufferResponse = true, Description = "Get Fahrenheit value to exact Celsius")]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml, XmlSerializeString = true)]

//Get Celsius To Fahrenheit
public string CelsiustoFahrenheit(Double Cels)
{

Double d1 = 0.0;
Double d2 = 0.0;
Double d3 = 0.0;

d1 = Cels * 9;
d2 = d1 / 5;
d3 = d2 + 32;

return d3.ToString();
}

[WebMethod(BufferResponse = true, Description = " Get Celsius value to exact Fahrenheit")]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml, XmlSerializeString = true)]
//Get Fahrenheit to Celsius details
public string FahrenheitToCelsius(Double Fval)
{

Double d1 = 0.0;
Double d2 = 0.0;
Double d3 = 0.0;

d1 = Fval - 32;
d2 = d1 / 9;
d3 = d2 * 5;

return d3.ToString();
}
}


Service Output


images


Client Side

I have design webpage with use of one text (to get input value), one drop down list to select user want to select Fahrenheit to Celsius or Celsius to Fahrenheit.

<h3>Convert Celsius To Fahrenheit or Fahrenheit To Celsius using webservice</h3>
Enter Value<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
Select Value<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Celsius To Fahrenheit</asp:ListItem>
<asp:ListItem>Fahrenheit To Celsius</asp:ListItem>
</asp:DropDownList><br/>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" /><br/>
<asp:Label ID="lblResult" runat="server"></asp:Label><br/>

images

Code behind

Add that web service url as web reference in the project and create instance for that service class through that access two methods to convert values.

//add namespace
using localhost;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblResult.Text = "";
}
protected void Button1_Click(object sender, EventArgs e)
{
//Create instance for service reference class
Convert_FarToCel_CelFar obj = new Convert_FarToCel_CelFar();
//Based on user selection call the which service method to be execute
if (DropDownList1.SelectedIndex == 0)
{
lblResult.Text = "Result " + obj.CelsiustoFahrenheit(Convert.ToDouble(TextBox1.Text)) + " 0 F" ;
}
else
{
lblResult.Text = "Result " + obj.FahrenheitToCelsius(Convert.ToDouble(TextBox1.Text)) + " 0 C";
}
}
}

Source code:

Client Side: ASP.NET
Code Behind: C#

Conclusion

I hope this code snippet is help you to know about conversion of Fahrenheit to Celsius or Celsius to Fahrenheit.


Attachments

  • Ws_Convert_FrCel_CelFr (44353-71253-Ws-Convert-FrCel-CelFr.rar)
  • Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: