Schedule Program Using C#

If you don't have MS Outlook,  you may want to give this a try for simple scheduling. You can use it to write one line activities at any hour throughout your lifetime.  It also includes an alarm feature for notifying you of special events. Since it includes the source code, you can modify it to customize your scheduling needs.
 
 
The Scheduling program consists of three main classes. The Form (shown above),  the DatabaseController used to read and write scheduled events into the database and RowData which corresponds to data extracted from a row in the database. UML Design is shown below:
 
 
The Above Design was Reverse Engineered using WithClass 2000 UML Design Tool
 
There are many aspects of the C# .NET framework we could talk about in this code such as the use of the timer, listview manipulation, key capturing, calendar control, and edit box positioning. In this article, we will concentrate primarily on how to use ADO.NET to read and write the scheduling information. Below is a database snapshot of our calendar database:
 
 
The ADO.NET Library Framework provides a fairly simple means of reading and writing data to an access JET database. Reading from the database is performed in a few steps. First, you set up what's called an ADODataSetCommand Object. This object allows you to assign SQL commands to it in order to manipulate the database. The code for assigning a command to this object is shown below:
  1. private ADODataSetCommand AdoCmd = new ADODataSetCommand("SELECT * FROM Activities""Provider=Microsoft.JET.OLEDB.4.0;data source=calendar.mdb"); // create a new DataSet Command and connect to  
  2. // the Access database calendar.mdb  
  3. ...........  
  4. // set up a SQL Select command that filters on the date  
  5. AdoCmd.SelectCommand.CommandText = "SELECT * FROM Activities WHERE Date = #" + strDate + "#"
Here we have a DataSet Command Object in which we change the command to filter only Activity rows that are performed on a specific date. These are the rows we will display in our listview located on the left side of the form.  Once we have the DataSet Command Object setup, we can create a dataset and fill it:
  1. DataSet dtSet = new DataSet(); // create a new data set  
  2. AdoCmd.FillDataSet(dtSet, "Activities"); // Fill the DataSet using the DataSet Command Object 
The DataSet can now be used to extract information from the table. The table reference is gotten from the collection of tables in the dataset. Our database only has one table "Activities", so we can just get the first table, indexed at 0.  Once we have the reference to the DataTable, we can go through the Rows collection and extract each row of information from the table.  A Column of information in a row is extracted by dereferencing using the column name of the table (e.g.  dtRow["Activity"] ). You could also use the column index (e.g. dtRow[4]):
  1. DataTable dTable = dtSet.Tables[0]; // extract the first (and only) table from the dataset  
  2. // go through each row of the dataset and populate the listview  
  3. foreach(DataRow dtRow in dTable.Rows) {  
  4.     nCount = CalcRow(dtRow); // determine listview row to populate using the date and time information  
  5.     // populate the listview row from the "Activity" column in the database  
  6.     listView1.ListItems[nCount].SetSubItem(0, dtRow["Activity"].ToString());  
  7.     // Determine the Image to use in the listview based on whether the "IsAlarm" Column is set or not  
  8.     if (dtRow["IsAlarm"].ToString().ToBoolean() == true) {  
  9.         listView1.ListItems[nCount].ImageIndex = 1;  
  10.     } else {  
  11.         listView1.ListItems[nCount].ImageIndex = 0;  
  12.     }  
  13. }
Writing in ADO.NET is a bit more, well, complicated. To write (insert) a new row,  I used a simple method provided by the Rows Collection object called AddUpdate. Below is a snippet of code from the WriteOutActivities routine in the DatabaseController to show how this command is used:
  1. // separate dataset created for use in adding new rows  
  2. // This command is selected in order of ID, so we can easily get the maximum ID (Primary Key) to use for the next  
  3. // Available Row  
  4. AdoCmdAll.SelectCommand.CommandText = "SELECT * FROM Activities ORDER BY ID";  
  5. AdoCmdAll.FillDataSet(dtSetAll, "Activities");  
  6. ..........  
  7. int nMax = 0;  
  8. DataTable dTableAll = dtSetAll.Tables[0];  
  9. if (dTableAll.Rows.Count > 0) {  
  10.     // compute the maximum Row ID, by getting the ID of the last ordered row and adding 1  
  11.     nMax = dTableAll.Rows[dTableAll.Rows.Count - 1]["ID"].ToString().ToInt32() + 1;  
  12. }  
  13. if (nMax < 0) {  
  14.     nMax = 0;  
  15. }  
  16. // add to the Row Collection the new data row with maximum row id + 1 as the new primary key  
  17. dTableAll.Rows.AddUpdate(new object[] {  
  18.     nMax,  
  19.     strDate,  
  20.     Time2Integer(listView1.ListItems[index].Text),  
  21.     IsRowAM(index),  
  22.     listView1.ListItems[index].SubItems[0],  
  23.     (listView1.ListItems[index].ImageIndex == 1)  
  24. });  
  25. ........  
  26. // Must Call Update on the DataSet Command in order for the changes to take place  
  27. AdoCmd.Update(dtSetAll, "Activities"); 
First, the Maximum ID is calculated to get a brand new unique primary key.  The maximum row is determined by forcing the dataset to return the rows in order using ORDER BY ID in the SQL statement of the DataSet Command.  Then the row of data including the ID (Primary Key), date, time, am/pm flag, activity, and an alarm flag is added using the AddUpdate command. Finally, we need to call Update on the DataSet Command Object, AdoCmd to force the changes to take place from the dateset into the database.
 
Editing an existing row is done a bit differently.  Below we show how to update a row in the database that turns the alarm flag off in an activity. This is done by calling BeginEdit on a row,  changing the data in the row and then calling  EndEdit.  Afterwards, we call  Update on the ADODataSetCommand, to make sure the changes are saved in the database:
  1. public void DeleteAlarm(int nKey) {  
  2.     try {  
  3.         // find the row with the passed in primary key  
  4.         AdoCmd.SelectCommand.CommandText = "SELECT * FROM Activities WHERE ID = " + nKey.ToString();  
  5.         DataSet dtSet = new DataSet();  
  6.         AdoCmd.FillDataSet(dtSet, "Activities"); // fill the data set using the ADO Command Set  
  7.         DataTable dTable = dtSet.Tables[0];  
  8.         // go through each row meeting the SELECT criteria (should only be one row, since a primary key is unique)  
  9.         foreach(DataRow dtRow in dTable.Rows) {  
  10.             dtRow.BeginEdit(); // called before editing the column in the row  
  11.             dtRow["IsAlarm"] = false// edit the column for the alarm switch  
  12.             dtRow.EndEdit(); // finished the editing on the row  
  13.         }  
  14.         // Must call update for editing to take effect  
  15.         AdoCmd.Update(dtSet, "Activities");  
  16.     } catch (Exception exx) {  
  17.         System.Console.WriteLine(exx.Message.ToString());  
  18.     }  
That's really all there is to it.  I think you'll find it is a little more intuitive than the recordset concept in Visual C++. As far as the Scheduling program is concerned, look forward to future enhancements such as printing your daily schedule.  Happy Scheduling!