Search code examples
c#databasewinformsoledboledbdatareader

Stuck in loading data from database


In making this appointment calendar, I wanted to use access database to save and retrieve my appointments. However I have more than one property type (Strings, Ints, DateTime) and more than one type of boxes (ComboBox, ListBox, DateTimePicker) to display in the windows forms. I have managed to write my code for the database with the following code (part of it):

foreach(var appointment in listOfAppointments)
{
    OleDbCommand DbCommand = new OleDbCommand(
        "INSERT INTO NewAppointmentDatabase " + 
        "([Start], [Length], [DisplayableDescription], [OccursOnDate], [Location], [IsRecurring], [Frequency], [Occurence]) " + 
        "VALUES(@Start, @Length, @DisplayableDescription, @OccursOnDate, @Location, @IsRecurring, @Frequency, @Occurence)", 
        SaveAppntAccess);

    DbCommand.Connection = SaveAppntAccess;


    DbCommand.Parameters.AddWithValue("@Start", appointment.Start);  //is a short time in DateTime
    DbCommand.Parameters.AddWithValue("@Length", appointment.Length); //is an int
    DbCommand.Parameters.AddWithValue("@DisplayableDescription", appointment.DisplayableDescription); //is a long string
    DbCommand.Parameters.AddWithValue("@OccursOnDate", appointment.OccursOnDate(date)); //is a boolean with DateTime as argument
    DbCommand.Parameters.AddWithValue("@Location", appointment.Location); //is a string
    DbCommand.Parameters.AddWithValue("@IsRecurring", appointment.IsRecurring); //is a boolean with yes/no tickbox
    DbCommand.Parameters.AddWithValue("@Frequency", appointment.Frequency); //is a string
    DbCommand.Parameters.AddWithValue("@Occurence", appointment.Occurence); //is an int

I have to note that the word date in appointment.OccursOnDate(date) is reddened in visual studio which is kind of weird because the boolean parameter is inherited.

And then comes the tricky part: I want to load my data! But I want to take my values from the database and assign them to each property first, and then take those and display them in the ComboBoxes and TextBoxes and DateTimePickers.

The code goes like this (part of it):

if(LoadAppntAccess.State == ConnectionState.Open)
{
    OleDbCommand DbCommand = new OleDbCommand(
        "SELECT * FROM NewAppointmentDatabase", LoadAppntAccess);
    OleDbDataReader reader = null;
    DbCommand.Connection = LoadAppntAccess;
    reader = DbCommand.ExecuteReader();

    foreach (var appointment in listofAppointments)
    {
        while (reader.Read())
        {
            //code to complete
        }
    }
}

How will I assign the values from each field to each property? I was thinking something like this:

appointment.Start.Add(reader["Start"].ToString());
appointment.Length.Add((reader["Length"].ToString());
appointment.DisplayableDescription(reader["DisplayableDescritpion"].ToString());

But I get errors in all of those - what is the right syntax?

EDIT : I forgot to mention that "start" although it's assigned as DateTime, I used as a ShortTime value because I wanted a ComboBox with time and 30 minute intervals. So it's not exactly a Date. For OccursOnDate it was written as:

public bool OccursOnDate(DateTime date)
{
    return date.Date == date;
}

and to retrieve a date I used a DateTimePicker.

2nd edit for more info

My class looks like this:

public class Appointment : IAppointment
{
    public DateTime Start { get; set; }
    public int Length { get; set; }
    public string DisplayableDescription { get; set; }
    public bool OccursOnDate(DateTime date)
    {
        return date.Date == date;

    }

    //custom members
    public int ID { get; }
    public string Location { get; set; }
    public bool IsRecurring { get; set; }
    public string Frequency { get; set; }
    public int Occurence { get; set; }


    public Appointment()
    {

    }

but unfortunately it inherits the parameters from IAppointment which has this code.

int ID { get; }

    DateTime Start { get; }
    int Length { get; }
    string DisplayableDescription { get; }
    bool OccursOnDate(DateTime date);

    //custom members
    string Location { get; set; }
    bool IsRecurring { get; set; }
    string Frequency { get; set; }
    int Occurence { get; set; }

The custom members are my addition since I had to put some extra stuff according to the specs.

However I managed to find a syntax based on your answers below.

appointment.Start.((DateTime)reader["Start"]);
appointment.Length.((int)reader["Length"]);
appointment.DisplayableDescription.((string)reader["DisplayableDescritpion"]);
appointment.OccursOnDate((DateTime)reader["OccursOnDate"]);
appointment.Location.((string)reader["Location"]);
appointment.IsRecurring.((bool)reader["IsRecurring"]);
appointment.Frequency.((string)reader["Frequency"]);
appointment.Occurence.((int)reader["Occurence"]);

I still get this message: Identifier expected

Any clues?


Solution

  • From the info you have given i would guess something like this:

    appointment.Start = (DateTime)reader["Start"];
    appointment.Length = (int)reader["Length"];
    appointment.DisplayableDescription = (string)reader["DisplayableDescritpion"];
    

    This is just a simple example, we would need more info to give a better answer. If any of the columns can have a null value you need to handle that as well etc..