Click here to Skip to main content
15,900,725 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 not 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

VB
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
Updated 19-Sep-13 2:04am
v2
Comments
ZurdoDev 19-Sep-13 7:53am    
What? int.Parse will return a integer. If you want to make sure they entered an integer use Int32.TryParse instead.
Innocent910 19-Sep-13 8:04am    
now if user enter a,b,c,0 whatever not 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
Innocent910 19-Sep-13 8:04am    
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;

check the ASCII value of input
 
Share this answer
 
Something like this perhaps?

C#
int choice;
if(int.TryParse(Console.ReadLine, out choice)){
    switch (choice)
            {
                case 1:
                    Console.WriteLine("\n");
                    conn.InsertData();
                    Console.WriteLine("\n");
                    conn.GetData();
                    break;

}
else{
    Console.WriteLine("PLease enter an integer value!");
}


Int.TryParse[^]
 
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