Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Building a Family History Web Service

  In this article review of Family History Web Service is made. It shows a client application built for collecting data about shared ancestors. Its developed using the Visual Web Developer 2005 Express Edition.

Difficulty: Intermediate
Time Required: 1-3 hours
Cost: Free
Software: Visual Studio Express Editions
Hardware:
Download: Download

 

In my next two entries I’m going to review a family history Web service I’ve built using Visual Web Developer 2005 Express Edition. In this entry, I’ll start with a look at the Web service. In a following article, I’ll show you a client application I’ve built for my sister and cousins to use for collecting data about our shared ancestors (sorry, but Mom still won’t go near a computer).

To begin, I fired up Visual Web Developer 2005 Express Edition and created a new ASP.NET Web site. A nice convenience of the latest version of ASP.NET is that you don’t need to install Internet Information Server (IIS) on your computer to build Web sites or Web services. Instead, you can run the application locally using the built-in Visual Web Developer Web Server. When you run a Web application, you’ll notice an icon appears in your task bar. If you double-click on the icon, a details dialog box similar to the one shown below appears. I really, really like this feature as it reduces the hassle of configuring your system to build Web applications to zero.

But I’m get a bit ahead of the game. Once I had my new Web service opened in the IDE, my first task was to create a store for my family data. When you create a new project, you’ll notice a folder titled "Data" in the list of project items. When I right-clicked the folder and selected "Add New Item..." from the context menu, this displayed the dialog shown below.

You can use XML or an ordinary text file as your data source for a Web application. As you can see, I chose a database – I named it, and then clicked the Add button. This created a brand new SQL Server Express database file for me in the project. Another great feature of Visual Studio 2005 in all its flavors is that you get built-in database support, without the muss or fuss of using an external program. Think of it as a one-stop shop. And Visual Web Developer 2005 Express Edition provides a wickedly simple designer for defining tables, views, stored procedures and other types of database objects. I created a new table named "person" and defined columns for the various attributes I want to record in my family history database.

With the skeleton of my database laid out, I moved to the Service.cs code file in my Web services application. Here I started by adding an attribute to the class definition.

Visual C#

[WebService(Namespace = "uri:family:geneology",Name = "Family History", 
Description = "Web service for gathering family history data." )]

Visual Basic

<WebService(Namespace:="uri:family:geneology", Name:="Family History", _ 
Description:="Web service for gathering family history data.")>

The WebServiceAttribute class (you can leave out "Attribute" in your code as I did) lets you give the Web service a unique internet identity using the namespace attribute. I also gave the Web service a name and a description.

Then I renamed the stub "Hello World" Web method to "FindPerson" and updated its WebMethodAttribute declaration to describe the task I intend for the Web method. In this case, the FindPerson method returns a list of people stored in my family history database based on some search criteria. After thinking a bit about how to return search results, I decided against using a DataSet (which is possible but not really a good idea if you want your Web service to get along well with non-.NET clients) and chose to model a Person object after the contents of my database and use that as my return type.

I added a class named "Person" to my project and defined a number of public properties that deliberately map to the columns in the person table of the database. Of course, I am not solely bound to mapping the attributes of the table data to the properties of the class; I will probably extend the class some more with behaviors as this application takes on more heft later on. Next, to make the class truly useful, I added a SerializableAttribute to the class declaration.

Visual C#

[Serializable()]
public class Person {
...
}

Visual Basic

<Serializable()> _
Public Class Person
...
End Class

This signals to the ASP.NET runtime that the class and all of its properties can be serialized (that is, converted to XML) either as an input to one of my Web methods or as a return value.

Returning to the Web method, I updated the method signature to return an array of Person objects (families do have members with the same first and last names, after all). I also added another Web method called "UpdatePerson" for adding a new person to my database or for updating an existing person. Again, I took advantage of the underlying serialization capabilities of ASP.NET by declaring a single input parameter of type Person.

Following good coding practice, I factored out the actual nuts and bolts work of querying and updating the database to three private helper methods in the Service.cs file. If you look over the code in the "SearchPerson", "AddPerson" and "UpdatePerson" methods you will find the application uses stored procedures to perform work against the database. Again, the database tools provided in Visual Studio Web Developer 2005 Express made this chore virtually painless.

That completes the tour of this application. Next time, I’ll show you the client application I’m going to use to manage querying and data entry for the Web service. In the meantime, I encourage you to download Visual Web Developer 2005 Express Edition and explore this Web service application for yourself.

Follow the Discussion

  • BenBen

    Hi Peter,

    i am new to developing and have read your article about using web services to input data into a database. i have been given a project in which i have been asked to create a web app which takes 2 parameters and inserts them into a database using a web service. i have found vast amounts of walkthroughs and tutorials to return data but non to insert.

    i was hoping you could assist me with resources or code snippets which will allow me to accomplish this task, as far as i understand your service provides this kind of functionality.

    Thanks for your time, any advice will be greatly recieved.

    Ben

    jazpa125@hotmail.com

  • MarkMark

    Thanks for this, you have taken a daunting task and simplified it for me. Much appreciated.

  • MikeMike

    Fantastic and Clear the best ive seen on WS.

Remove this comment

Remove this thread

close

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.