• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[C#] a little help

Status
Not open for further replies.
Level 6
Joined
Sep 13, 2013
Messages
155
I've started learning C# lately. Got an exercise from my teacher and I'll be happy if someone give me some insight about how to write a program like this:

so at start there is this menu:
1-insert
2-update
3-delete
4-select
5-exit

by pressing 1, the page is cleared and this menu appears:

1-insert
2-back

and by pressing 1, this one appears:

Name
ID
City Code

as for 4, the "select" button, this menu appears:

1-search by name
2-search by ID
3-back

so basically the program is supposed to make a table using SQL and you can insert name, ID and city code to the table, be able to delete them and select them (search for them) by their names and IDs. Thanks in advance and sorry for bad English.
 
Level 6
Joined
Sep 13, 2013
Messages
155
well here is what i did :

First i made two classes one called mydb (which is for database) and the other is the program itself.

Code:
using System.Data.SqlClient;


namespace Menu
{

class mydb

{

public SqlConnection cnn;
public SqlDataAdapter da;

public mydb(string cnnstr)
{

cnn = new SqlConnection(cnnstr);
da = new SqlDataAdapter("" , cnn);
}
public System.Data.DataTable fill(string cmdstr)
{
da.SelectCommand.CommandText = cmdstr;
System.Data.DataTable tb = new System.Data.DataTable();
da.Fill(tb);
return tb;
}
public void show_tbl(System.Data.DataTable tb)
{
for (int i = 0; i < tb.Rows.Count; i++)
{
for (int j = 0; j < tb.Columns.Count; j++)
{
Console.Write(tb.Rows[i][j].ToString());
Console.Write(" ");
}
Console.WriteLine();
}
}
}
}

Code:
using System.Data.SqlClient;

namespace Menu
{
    class Program
    {
        static void Main(string[] args)
        {


            print_main_menu();
            string menucode = Console.ReadLine();


            switch (menucode)
            {
                case "1":
                    print_insert_menu();
                    break;
                case "2":
                    break;
                case "3":
                    update_menu();
                    break;
                case "4":
                    select_menu();
                    break;
                case "5":
                    break;

            }
        }

        static void print_main_menu()
        {
            Console.Clear();
            Console.WriteLine("1-insert");
            Console.WriteLine("2-del");
            Console.WriteLine("3-update");
            Console.WriteLine("4-select");
            Console.WriteLine("5-exit");
            Console.ReadLine();
        }
        static void print_insert_menu()
        {
            Console.Clear();
            Console.WriteLine("1-insert");
            Console.WriteLine("2-back");
           string insertcode = Console.ReadLine();
           switch (insertcode)
           {
               case "1":
                   break;
               case "2":
                   print_main_menu();
                   break;
           }
        }

Not sure if I'm at the right direction tho.
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
Note that print_main_menu has a blocking read call at the end, which will conflict with the read call right after in main.

I would put the main switch in print_main_menu, instead of in main. That way it actually controls the flow of the program.
For example, right now if you call it from print_insert_menu, it just prints the text to the console, but the logic isn't actually there, so control returns to main, exits the switch, and the program ends.
You can think of it as every menu being a method that can be called from any other menu to switch control.

Also the fact that you call the methods "print" is pretty weird, because they do in fact hold flow logic too, not just printing, but that's not really important.

On a side note, please use [code=c#] instead of [code].
 
Status
Not open for further replies.
Top