Home > Software design >  C# Console logging Program SqlObject instead of key:value pairs
C# Console logging Program SqlObject instead of key:value pairs

Time:10-02

im trying to loop through my list but im getting Program SqlObject instead of the objects values im new to c# so this is very confusing.

I think i should be getting something like 0:{id:value,date:value,Quantity:value} ,1:{id:value,date:value,Quantity:value} but instead im getting Program SqlObject Program SqlObject

here is the console output:

0: Add New Date

1: View Current Dates

2: Delete Date

3: Update Date



Program SqlObject
Program SqlObject
Program SqlObject

Program.cs

using Microsoft.Data.Sqlite;
    using System.Collections.Generic;







internal class Program
{


    struct SqlObject
    {


        public int id { get; set; }
        public string date { get; set; }
        public int quantity { get; set; }
    }


    private static void Main(string[] args)
    {
        




            string inputCommand;
            string globalDateInput;
            int globalQuantityInput;
            while (true)
            {



                Console.WriteLine("\n\n0: Add New Date");
                Console.WriteLine("\n1: View Current Dates");
                Console.WriteLine("\n2: Delete Date");
                Console.WriteLine("\n3: Update Date");



                inputCommand = Console.ReadLine();


                CreateDatabase();
                CheckInput();

            }


            static void CreateDatabase()
            {
                using (var connection = new SqliteConnection(@"Data Source=C:\Databases\HabitTrackerV3.db"))
                {
                    connection.Open();

                    var command = connection.CreateCommand();

                    command.CommandText = "CREATE TABLE IF NOT EXISTS drinking_water (id INTEGER PRIMARY KEY AUTOINCREMENT,date Date, Quantity INTEGER)";
                    command.ExecuteNonQuery();


                    connection.Close();


                }
            }



            void ViewDates()
            {

                using (var connection = new SqliteConnection(@"Data Source=C:\Databases\HabitTrackerV3.db"))
                {
                   
                connection.Open();


                Console.WriteLine("Ran");
                var command = connection.CreateCommand();
                command.CommandText = $"SELECT * FROM drinking_water ";

                using (var reader = command.ExecuteReader())

                {
                    Console.WriteLine("looped");
                    while (reader.Read())
                    {

                        int id = (reader.GetInt32(0));
                        string Date = (reader.GetString(1));
                        int Quantity = (reader.GetInt32(2));

                        List<SqlObject> sqlList = new List<SqlObject>();
                        sqlList.Add(new SqlObject {id = id,date = Date,quantity=Quantity });
                        sqlList.ForEach(i => Console.WriteLine(i));

                    }
                }

                   


                    connection.Close();


                }
            }


        void AddDate()
        {
            DateInputCommand();
            using (var connection = new SqliteConnection(@"Data Source=C:\Databases\HabitTrackerV3.db"))
            {
                connection.Open();

                var command = connection.CreateCommand();

                command.CommandText = $"INSERT INTO drinking_water(date,Quantity) VALUES('{globalDateInput}' ,{globalQuantityInput})";
                command.ExecuteNonQuery();


                connection.Close();


            }


        }


        void DateInputCommand()
        {
            Console.WriteLine("\n\n What Date in the format dd-mm-yyyy");
            globalDateInput = Console.ReadLine();
            Console.WriteLine("How many cups of water did you drink?");
            globalQuantityInput = int.Parse(Console.ReadLine());




        }

        void CheckInput()
        {
            switch (inputCommand)
            {
                case "0":
                    AddDate();
                    break;
                case "1":
                    ViewDates();
                    break;


            }
        }
    }
}

CodePudding user response:

You could override the ToString() method in your type.

class SqlObject
{


    public int id { get; set; }
    public string date { get; set; }
    public int quantity { get; set; }
    public override string ToString()
    {
        return $"Id: {id} date: {date}";
    }
}

CodePudding user response:

The way you call it Console.WriteLine call implicitly ToString method of object type, so you need to be more explicit there.

So as mentioned one option is to override ToString method.

Other solution might be just to define more elaborate message in Console.WriteLine:

Console.WriteLine($"Fetched {i.Id} with date {i.date}")

or something along these lines.

CodePudding user response:

only change this line :

sqlList.ForEach(i => Console.WriteLine(i));

to:

 sqlList.ForEach(i => 
Console.WriteLine("id: "   i.id  ",date: " i.date ", Quantity: " i.quantity));
  • Related