Flash Components      Flash Menu      Flash Gallery      Flash Slideshow      FLV Player      Flash Form      MP3 Player      PhotoFlow      Flash CMS      3D Wall      Flash Scroller

Flash / AS

Silverlight

WPF

ASP.net / PHP

Photoshop

Forums

Blog

About

 


FlashComponents
  Galleries
  Slideshows
  Menus
  Design & Effects
  Audio & Video
  User Interface
  Templates

 

 

Using a Web Service - Page 6
       by kirupa  |  11 August 2006

In the previous page we started our explanation of what the code is all about. In this page we will finish what we started and tie up some loose ends.


protected void btnInput_Click(object sender, EventArgs e)
{
try
{
lblResult.Text = ws.GetMovieAtNumber(Convert.ToInt32(txtInput.Text));
}
catch
{
lblResult.Text = "Invalid Input";
}
}

The btnInput_Click method, as you can guess, is invoked when you click the btnInput button you defined in your design of the Default.aspx page. When invoked, the following try/catch block is executed:

try
{
lblResult.Text = ws.GetMovieAtNumber(Convert.ToInt32(txtInput.Text));
}
catch
{
lblResult.Text = "Invalid Input";
}

Try/Catch blocks are simple ways of avoiding errors that may crash your application. If whatever code inside the try block returns an error - an exception, the code in the catch block is executed.

You will see why I need to use a Try/Catch block when you look at the following line in greater detail:

lblResult.Text = ws.GetMovieAtNumber(Convert.ToInt32(txtInput.Text));

The above line takes whatever input you enter, ideally a number between 0 and 9, and sends that value to our ws object's GetMovieAtNumber method. Remember that our GetMovieAtNumber method takes an integer for its argument. The data returned by our input field will always be a string.

In order to go from a string to an integer, I use the Convert.ToInt32() method to convert a string into an integer. What if a user enters values that are not really numbers? For example, instead of inputting a value between 0 and 9, you submit a value like "foo". The Convert.ToInt32(), when faced with an input of foo, will return an exception that stops your program. Likewise, if the number entered is smaller than 0 and greater than 9, you will receive an Array out of Bounds exception that will also stop your program.

In order to avoid errors that completely stop the program from executing, try/catch blocks are used. When a non-numerical value or a value outside of the specified range is entered, an exception is still thrown. The difference is that, because of the try/catch block, the exception is caught and the code in the catch block is executed:

lblResult.Text = "Invalid Input";

The above line of code is very straightforward. It sets the text property of lblResult label to "Invalid Input" if an exception is caught.


Conclusion
Well, the last few sentences pretty much wrap up this tutorial. If you followed this tutorial from the very beginning on Page 1, you created the interface, added a Web Reference, inserted some code, and hopefully learned how all of the various pieces work when put together.

I hope the information helped. If you have any questions or comments, please don't hesitate to post them on the kirupa.com Forums. Just post your question and I, or our friendly forum helpers, will help answer it.

The following is a list of related tutorial and help resources that you may find useful:

How to use the Forums
New, Upcoming, and In-Progress Tutorials
How to Help out kirupa.com
Writing Tutorials
 
Cheers!
Kirupa Chinnathambi
kirupaBlog

 


1 | 2 | 3 | 4 | 5 | 6


kirupa.com's fast and reliable hosting provided by Media Temple. flash components
The Text Animation Component for Flash CS3
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.
Check out our high quality vector-based design packs! Flash Effect Components
flash menus, buttons and components Digicrafts Components
The best flash components ever! Entheos Flash Website Templates
Buy and sell FLAs at Ultrashock! Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com
Purchase & Download Flash Components flash components
Learn how to advertise on kirupa.com