Showing both a start and end date

dougancil

Well-known member
Joined
Jun 29, 2010
Messages
50
Programming Experience
Beginner
I have the following code:

VB.NET:
Public Class Form1
    Dim ReturnValue As Object = Nothing
    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connectionString As String
        Dim cnn As SqlConnection
        Dim myCommand As SqlCommand
        Dim dteReturnValue As DateTime = Nothing
        'the connection string to the SQL server'
        connectionString = "Data Source=10.2.1.41;Initial Catalog=MDR;uid=xxxxx;password=xxxxx"
        cnn = New SqlConnection(connectionString)
        cnn.Open()
        'the SQL query'
        Try
            myCommand = New SqlCommand("select payrolldate from payroll where payrolldate <= getdate ()")
            myCommand.Connection = cnn
            ReturnValue = myCommand.ExecuteScalar()
            If ReturnValue IsNot Nothing Then
                dteReturnValue = Convert.ToDateTime(ReturnValue)
                dteReturnValue = dteReturnValue.AddDays(1)
            End If
        Catch exp As SqlException
        End Try
        cnn.Close()
        Dim ButtonDialogResult As DialogResult
        ButtonDialogResult = MessageBox.Show(String.Format("The next date available to you is {0}", dteReturnValue.ToShortDateString()), "Payroll", MessageBoxButtons.OK)
        If ButtonDialogResult = Windows.Forms.DialogResult.OK Then
            'pass 'Return Value' to SQL query that will be here'
            Button2.Enabled = True
            Button1.Enabled = False
        End If
    End Sub

and as you can see, I am showing users the next available date open to them is determined with the value dteReturnValue.ToShortDateString .. but what I'd like to do is show the user both the start date and the end date, which will always be +7 days from the field in my database. Can someone offer me a way to show this date, thank you.

Doug
 
Matt,

Thank you for that, but what if I wanted to show both of those values in a messagebox?

For example now when that messagebox, it shows "The next date available to you is "dd/mm/yy"
but what I'd like is my messagebox to show "The next date available to you is "dd/mm/yy" to "dd/mm/yy" with this string : dteReturnValue.AddDays(7).ToShortDateString() adding the second value.

Can you tell me how I could do that?

Thanks

Doug
 
VB.NET:
        Dim startDate As Date = Date.Today
        MessageBox.Show(String.Format("The next date available to youe is {0} to {1}", _
                                      startDate.ToShortDateString, _
                                      startDate.AddDays(7).ToShortDateString))
 
Matt,

Thank you for that but with your code for the startdate, it would be todays date. In my code the variable is the ReturnValue which is derived from a sql query to a database. So essentially what I'm showing the user is the start date is the next available day (according to my SQL query) and then 7 days later is the "end date."
 
Matt,

Nevermind... that was my fault. I got it. Thank you.

Here is the final code:

VB.NET:
  Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connectionString As String
        Dim cnn As SqlConnection
        Dim myCommand As SqlCommand
        Dim dteReturnValue As DateTime = Nothing
        'the connection string to the SQL server'
        connectionString = "Data Source=10.2.1.41;Initial Catalog=MDR;uid=xxxxx;password=xxxxxxxx"
        cnn = New SqlConnection(connectionString)
        cnn.Open()
        'the SQL query'
        Try
            myCommand = New SqlCommand("select payrolldate from payroll where payrolldate <= getdate ()")
            myCommand.Connection = cnn
            ReturnValue = myCommand.ExecuteScalar()
            If ReturnValue IsNot Nothing Then
                dteReturnValue = Convert.ToDateTime(ReturnValue)
                dteReturnValue = dteReturnValue.AddDays(1)
            End If
        Catch exp As SqlException
        End Try
        cnn.Close()
        Dim ButtonDialogResult As DialogResult
        ButtonDialogResult = MessageBox.Show(String.Format("The next pay period available to you is {0} to {1}", dteReturnValue.ToShortDateString(), dteReturnValue.AddDays(7).ToShortDateString), "Payroll", MessageBoxButtons.OK)
        If ButtonDialogResult = Windows.Forms.DialogResult.OK Then
            'pass 'Return Value' to SQL query that will be here'
            Button2.Enabled = True
            Button1.Enabled = False
        End If
    End Sub
 
Back
Top