Washington State University



-5787000Working with Dates and DataTablesImports System.DataPartial Class NewFor2018_StudyAidCalendars Inherits System.Web.UI.Page 'this program created to give some ideas of the usefulness of the timespan variable and date calculations. The webpage is a first prototype of an app that can be used by students to track the number of hours they need to (and have) studied for their upcoming exams. May students do not keep track of their exam commitments, and specifically know what is the best use of their study time on any given day. Exam prep priorities will change as classes differ in their difficulty, and each student has their own strategy to prepare for exams. So this program must evolve to become more useful. This program is just a partial solution used for teaching. 'some suggested upgrades '1. Make the program data based '2. Add another column to give a textual feedback depending on progress to go - for example if > 70% of hours still needd to attain goal the message should read something like "hurry up". Alternatively of ther are just < 20% of hours left to hit the study goal the message could read "one more study session needed almost done '3. A gauge or red/yellow/green indicant or picture can be shown to alert the student about progress to goal. Red would mean danger, green would mean - reached the study goal. '4. The system could recommend to the student what is the best usage of their time (after identifying which exam is coming quickest, and seeing where the study performance to goal is in the danger zone. '5. Can you think of other improvements? Public Shared tblExams As New DataTable#Region "Save Record" Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'when the program user has a new exam to study for they use this procedure to add a row to a datatable, so that it can be tracked. Please refer to other programs and documentation to learn about data table (array) concepts and how to use datatables. the content in this program (create the table with columns, add rows of data, updat rows of data This next line creates a new blank row that has a database schema that matches the data table that was created. So the datatable knows what columns it has and can be manipulated. If txtExamDate.Text = Nothing OrElse txtHoursRequired.Text = Nothing OrElse txtName.Text = Nothing OrElse IsNumeric(txtHoursRequired.Text) = False Then Response.Write("check all data entry") Exit Sub End If Dim dr As DataRow = tblExams.NewRow 'we want the date to be somewhat formatted nicely so we store it as string, but we need to calculate and display how many days it is in the future so we also convert it to a date format to include in a timespan calculation. Next the date is pulled from teh textbox and converted to a variable with a date datatype. Dim dateExam As Date = DateTime.Parse(txtExamDate.Text) 'This is the timespan variable that can hold the results of the calculation between two dates. Here the number of days in teh future the exam is from today is calculated. In a line below you can see the ts.totaldays function is used to format the calculated result into the number of days including decimal, portions of a day (e.g. 5.2 days based on time of day) Dim ts As TimeSpan = dateExam - Today() 'now take the values from the form and put them into different columns of the new blank row dr.Item("ExamName") = txtName.Text 'verify that this next field is a string not a date. The datatype of the column is a string. To explicity use a north american month/day/year formatting we can take the value out of teh textbox, convert it to a date variable (see line 16 above). Here we format the date the way we prefer using the .tostring formatting dr.Item("ExamDate") = dateExam.ToString("MM/dd/yy") 'this is another very helpful formatting option of .the tostring built-in function. We can pull out the day name from the date dr.Item("DayofWeek") = dateExam.ToString("ddd") 'this next field is a decimal column that we store a value such as 6.2 days until the exam dr.Item("DaysUntilExam") = ts.TotalDays 'since the exam is a new row in the table the number hours required and remaining are both the same. the next procedure is used to update the number of hours remaining after recording some hours studying dr.Item("TotalHoursRequired") = Convert.ToDecimal(txtHoursRequired.Text) dr.Item("NumberHoursLeft") = Convert.ToDecimal(txtHoursRequired.Text) 'now that the necessary fields are filled in, we add the new row of data to the datatable and then display the data in a gridview control tblExams.Rows.Add(dr) GridView1.DataSource = tblExams GridView1.DataBind() 'view #2 will be used to update the # hours studied so far for the exam. We add the test name to a radiobuttonlist on the second view. rblExams.Items.Add(txtName.Text) End Sub#End Region#Region "Update exam progress" 'Here we update the one row of data that needs to be updated.0 Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 'the ExamDate column is stored in a string variable (so it could be formatted nicely) so we need to convert it to a date format so it can be included in a timespan operation and the column updated. 'Meaning for these next two lines: take the value from the examDate column, convert it to a datetime format and assign it to a date variable. (Convert.todatetime works for date or datetime variables). Next create a timespan variable and set its value to the result of teh calculation exam date - today. the result of this calculation can be formatted using the .totaldays formatting into a number such as 5.5 days. If rblExams.SelectedIndex = -1 OrElse txtHours.Text = Nothing OrElse IsNumeric(txtHours.Text) = False Then Response.Write("Check data entry") Exit Sub End If Dim dtExam As Date = Convert.ToDateTime(tblExams.Rows(rblExams.SelectedIndex).Item("ExamDate")) Dim ts As TimeSpan = dtExam - Today() Dim decProgress As Decimal With tblExams.Rows(rblExams.SelectedIndex) .Item("DaysUntilExam") = ts.TotalDays 'this next column started out as 0 hours completed, and now is incremented to record the number of hours studied. .Item("HoursCompleted") += Convert.ToDecimal(txtHours.Text) 'this next formula just keeps track of how many hours are left to study. we can use the value in the Hours complted column becuase we just updated it. .Item("NumberHoursLeft") = .Item("TotalHoursRequired") - .Item("HoursCompleted") 'we use a decimal variable to peform a division, then format that result into a string to format it nicely. The Progress column is a string becuase divisions always give you 20 decimal places. If we convert to string we can specify how many decimal places are displayed. decProgress = .Item("HoursCompleted") / .Item("TotalHoursRequired") .Item("Progress") = decProgress.ToString("P1") End With 'now show the updated data for prpgress to exam. GridView2.DataSource = tblExams GridView2.DataBind() End Sub#End Region#Region "Create table" Private Sub NewFor2018_StudyAidCalendars_Init(sender As Object, e As EventArgs) Handles Me.Init 'Create the columns for the datatable. Notice that the Exam date and Progress columns are set to the datatype string. This is done only to improve the formatting of the column values in the gridview. The date datatype wants to add the time (here not needed or captured) and the progress field is a calculation, specifically a division. There is no easy way to restrict the formatting to 2 decimal places when you have a number (please suggest solutions you may discover) so the calculation is performed and then stored as a string using the string formating (.tostring("P1") If tblExams.Columns.Count > 0 Then Exit Sub 'ensure the datatable is creaed only once With tblExams.Columns .Add("ExamID", GetType(Integer)) .Add("ExamName", GetType(String)) .Add("ExamDate", GetType(String)) .Add("DayofWeek", GetType(String)) .Add("DaysUntilExam", GetType(Decimal)) .Add("TotalHoursRequired", GetType(Decimal)) .Add("HoursCompleted", GetType(Decimal)) .Add("NumberHoursLeft", GetType(Decimal)) .Add("Progress", GetType(String)) .Add("ActualGrade", GetType(String)) End With 'the exam ID is set to an auto-number field using this code With tblExams.Columns("ExamID") .AutoIncrement = True .AutoIncrementSeed = 1 .AutoIncrementStep = 1 End With 'the HoursCompleted column is a running total so it needs to be set to 0 so that it can be incremented using a += formula. With tblExams .Columns("HoursCompleted").DefaultValue = 0 End With End Sub#End Region#Region "Delete an exam" Protected Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click 'This procedure deletes a row from the table. First make sure an exam was selected in the radiobutton list. If rblExams.SelectedIndex = -1 Then Response.Write("Select an exam to delete") Exit Sub End If 'first remove the row from the table. The code reads table.rows(row to delete).delete that row. The .delete function deletes the row. The table(rblExams.SelectedIndex) specifies which row to delete. Its great that both the radiobuttonlist and the rows collection of the table both have an index to identify their number. The index of the row or item in the radiobuttonlist (or any list control) starts at 0. tblExams.Rows(rblExams.SelectedIndex).Delete() 'now that we have deleted the row from the table, we delete the item from the list collection of the list control (e.g. delete the item from the radiobuttonlist) rblExams.Items.RemoveAt(rblExams.SelectedIndex) 'while we delete the row from the datatable, the gridview is not updated until you bind it again (after the row is deleted). GridView1.DataSource = tblExams GridView2.DataSource = tblExams GridView1.DataBind() GridView2.DataBind() End Sub#End Region#Region "Utilities" 'These next two procedures are used to clear the data entry controls Protected Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click txtHours.Text = Nothing rblExams.SelectedIndex = -1 End Sub Protected Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click txtName.Text = Nothing txtExamDate.Text = Nothing txtHoursRequired.Text = Nothing End Sub 'these next two procedures allow the human to switch between sections (iews) of the program Protected Sub LinkButton1_Click(sender As Object, e As EventArgs) Handles LinkButton1.Click MultiView1.ActiveViewIndex = 0 End Sub Protected Sub LinkButton2_Click(sender As Object, e As EventArgs) Handles LinkButton2.Click MultiView1.ActiveViewIndex = 1 End Sub#End RegionEnd Class ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download