- Using a Web Service, Page 6

       by kirupa  |  11 August 2006In 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. 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. Got a question or just want to chat? Comment below or drop by our forums (they are actually the same thing!) where a bunch of the friendliest people you'll ever run into will be happy to help you out! When Kirupa isn’t busy writing about himself in 3rd person, he is practicing social distancing…even on his Twitter, Facebook, and LinkedIn profiles. Hit Subscribe to get cool tips, tricks, selfies, and more personally hand-delivered to your inbox.   1 | 2 | 3 | 4 | 5 | 6

This is a companion discussion topic for the original entry at