Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
now if user enter a,b,c,0 whatever he want instead of operational choice 1,2,3,4.... then i want to show message invalid after geting user choice...how i can execute statement... hope u understand

C#
Console.WriteLine("Enter your Choice for Data Operation");
            Console.WriteLine("1. Insert Data into Table");
            Console.WriteLine("2. Update Data into Table");
            Console.WriteLine("3. Delete Data from Table");
            Console.WriteLine("4. Display All Data of Table\n");

            int choice = Convert.ToInt32(Console.ReadLine());

            switch (choice)
            {
                case 1:
                    Console.WriteLine("\n");
                    conn.InsertData();
                    Console.WriteLine("\n");
                    conn.GetData();
                    break;
Posted
Comments
ZurdoDev 19-Sep-13 8:18am    
Please don't repost. Use TryParse in case they don't enter a number.
Innocent910 19-Sep-13 8:19am    
how tell me again... plz dnt mind
Valery Possoz 19-Sep-13 8:32am    
http://www.codeproject.com/Questions/655899/check-at-input-type-of-user

1 solution

Try like this.

C#
try
{
   int choice = int.TryParse(Console.ReadLine());
 
   switch (choice)
   {
       case 1:
              Console.WriteLine("\n");
              conn.InsertData();
              Console.WriteLine("\n");
              conn.GetData();
              break;
       case 2: 
             //Do your implementation
              break;
       default:
	       Console.WriteLine("Other number");
	       break;
			
   }
}
catch(Exception ex)
{
     Console.WriteLine("Message"+ex.Message.ToString());
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900