< Network & .NET Programming | Main | VB .NET, C# & Framework Intro Examples >


 

Chapter 1 Part 2

Network Programming with the Microsoft .NET Framework

 

 

What do we have in this chapter 1 part 2?

  1. C# .NET: ASP.NET Weather Display Page

 

 

 

C# .NET: ASP.NET Weather Display Page

 

Create a new ASP.NET Web Service Application. You can use the solution and project name as in the following Figure.

 

Creating a new C# and ASP project in VS 2008

 

C# ASP .NET web service application project in visual studio

 

Add the following code.

 

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.Xml.Linq;

 

namespace WeatherServiceCS

{

    /// <summary>

    /// Summary description for Service1

    /// </summary>

    [WebService(Namespace = "http://tempuri.org/")]

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    [ToolboxItem(false)]

    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

    // [System.Web.Script.Services.ScriptService]

 

    // This class exposes a Web Service method that

    // takes a zip code and returns the forecast

    // for that area.

    public class Service1 : System.Web.Services.WebService

    {

        // The logic for GetTodaysForecast is limited for the

        // purposes of this example to check for

        // zip = 11111 and return "rainy" if it matches,

        // otherwise it will return "sunny".

        [WebMethod]

        public string GetTodaysForecast(string zip)

        {

            string forecast = "sunny";

 

            if (zip == "11111")

                forecast = "rainy";

 

            return forecast;

        }

    }

}

 

Build the project.

 

Building an ASP and C# project in Visual Studio 2008

 

Add a web form.

 

Adding a new item, web form into existing asp and C# project

 

Adding C# web form into existing C# project

 

Add the following code.

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WeatherServiceCS.WebForm1" %>

<%@ Import Namespace="WeatherServiceCS" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>A very simple asp-web application</title>

</head>

<script language="C#" runat="server">

  string zipcode = "11111";

  public void Submit_Click(Object sender, EventArgs E)

  {

      try

      {

          // Get the zip code from the form

          zipcode = ZipCode.Text;

      }

      catch (Exception) { /* ignored */ }

          // Create the weather service that will return the forecast

          Service1 weatherService = new Service1();

          // Set the display with the result of GetTodaysForecast

          Result.Text = "Today’s forecast is: <b>" +  weatherService.GetTodaysForecast(zipcode) + "</b>.";

  }

</script>

<body style="font: 10pt verdana">

  <h4>Weather Report </h4>

  <form id="Form1" runat="server">

 

  <div style="padding:15,15,15,15;background-color:Gray;

    width:300;border-color:black;border-width:1;

    border-style:solid">

 

    Zip Code: <br/>

      <asp:TextBox id="ZipCode" Text="11111" runat="server"/><br/>

 

    <input type="submit" id="Add" value="Get Weather Report"

     OnServerClick="Submit_Click" runat="server"/>

    <p/>

    <asp:Label id="Result" runat="server"/>

  </div>

  </form>

</body>

</html>

 

 

 

 

You can preview the form by switching to the Design page as shown below.

 

webform in the design mode seen in the VS 3008

 

Run the project.

 

Running asp and C# project without debugging

 

The following are sample outputs.

 

ASP and C# project in action Testing the C# and asp program in the Internet Explorer Browser

 

OK, so we just called a pretty basic service on a form that ran on the local machine. Now consider the possibility that the weather service resides on the Internet rather than on the local machine and is connected to live weather feeds from around the world. The programming model for calling the “ultimate weather service” in this scenario would be exactly the same as the sample you just saw.

 


< Network & .NET Programming | Main | VB .NET, C# & Framework Intro Examples >