C#/C# Question

Advertisement


Question
Hello Srini,
I am currently learning C# on my own. I made a C# Console database program that interacts with SQL server 2000 database. The program is simple, just add new student, delete,update,search and count records. I have made a main menu with choice(1-7) and I use the following statement to accept the choice from the user:

int choice = int.Parse(Console.ReadLine());

the problem here is that as long as I input numeric digits; the program works fine but when I mistakenly put a letter(a-z) as an input; the program gives an Format Exception. I tried a lot to get rid of this problem, but in vain.
I will really appreciate if you could assist me in accomplishing this.

Thanks
Jehan

Answer
Hi

before doing this "int.Parse(Console.ReadLine())"

use typeof function to find whether its a Integer if not you can pop different message

Here is the microsoft site gives you some sample

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclr...

Happy Programming!!

-Srini  

C#

All Answers


Answers by Expert:


Ask Experts

Volunteer


Srini Nagarajan

Expertise

can answer any kind of questions in ASP.NET, C#, VB.NET, ASP, SharePoint 2007, Coldfusion, Powerbuilder 7.00 / 8.00, JAVA servlets, MS SQL 2000 / MSSQL7, Sybase

Experience

Contact me if you need any custom development on ASP.NET, ASP, Coldfusion, Powerbuilder

©2012 About.com, a part of The New York Times Company. All rights reserved.