Kenny
Kenny

Reputation: 1054

If today's date minus db date is less than 48 hours, do something but I am getting an error. Any ideas?

Sorry again, experts for the bother again.but

We have a db date called orderdate.

If today's date - orderdate is less than 2 days (or 48 hours), disable Cancel Order button so user cannot cancel his or her order.

When I tried running the following code, I get Input string not in the format

Orderdate is of type datetime. However, we would like to display the date in the format of MM/dd/yyyy. Example: 6/4/2013, not 06/04/2013.

Can you please look at my code and tell me what I am doing wrong?

 If dr1.Read() Then
  Dim odate As String = DateTime.Parse(dr1("orderDates").ToString()).ToShortDateString()

  Dim cancelBtn As New Button()
  Dim dates As String = DateTime.Parse(Now().ToString()).ToShortDateString()
   If (sdate - dates) <2 Then
       cancelBtn.Enabled = False
   Else
       cancelBtn.Enabled = True
   End If
 End If

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    ' Create cancel training button
    Dim cancelBtn As New Button()
    cancelBtn.Style.Add("width", "105px")
    cancelBtn.Style.Add("padding", "5px")
    cancelBtn.Style.Add("margin", "5px")
    'cancelBtn.Enabled = False
    cancelBtn.Text = "Cancel training"
    If e.Row.RowIndex > "-1" Then
        ' Change tracking ID to link
        'Dim track As [String] = e.Row.Cells(4).Text
        'If track <> "&nbsp;" AndAlso track.Length > 0 Then
        '    Dim trackLink As New Literal()
        '    trackLink.Text = "<a style='color: blue;' href='" + track + "'/>Track</a>"
        '    e.Row.Cells(4).Controls.Add(trackLink)
        'End If

        ' Add buttons to column
        Dim oid As [String] = e.Row.Cells(0).Text
        Dim invoiceLink As New Literal()
        invoiceLink.Text = "<a style='color: blue;' href='Invoice.aspx?oid=" + oid + "'/>" + oid + "</a>"
        e.Row.Cells(0).Controls.Add(invoiceLink)
        e.Row.Cells(e.Row.Cells.Count - 1).Controls.Add(cancelBtn)

        ' Pass order id & row to on-click event
        'cancelBtn.Click += new EventHandler(this.cancelBtn_Click);
        'cancelBtn.CommandArgument = e.Row.RowIndex + "-" + oid
    End If
End Sub

Upvotes: 0

Views: 414

Answers (3)

just.another.programmer
just.another.programmer

Reputation: 8805

EDIT: Regarding the logic, your DB field orderDates sounds like it refers to the day the order was created. That date will always be in the past, so we would be interested in Today - orderDates.

However, based on your comments it seems orderDates refers to the day the order will ship. That date must be more than 2 days in the future for the user to cancel his order, so we're interested in orderDates - Today.

I don't see where you run the order date logic inside GridView1_RowDataBound.

Private Function MoreThanTwoDaysUntilShip() As Boolean
    'logic to open dr1
    If dr1.Read() Then
        Dim shipDate As Date = DateTime.Parse(dr1("orderDates").ToString())
        Dim today As Date = Date.Now

        Return shipDate.Subtract(today).TotalDays > 2
    End If

    Return False
End Function

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    ' Create cancel training button
    Dim cancelBtn As New Button()
    cancelBtn.Style.Add("width", "105px")
    cancelBtn.Style.Add("padding", "5px")
    cancelBtn.Style.Add("margin", "5px")

    'add this
    cancelBtn.Enabled = MoreThanTwoDaysUntilShip()


    'etc
End Sub

Upvotes: 0

sgeddes
sgeddes

Reputation: 62851

I'm not sure why you'd want to convert your date fields into strings. I would recommend leaving those as datetime objects for your comparison. You can always manipulate the display of the dates in your presentation logic.

This is some working code leaving as dates using Subtract and TotalDays:

Dim cancelBtn As New Button()
Dim odate As DateTime = DateTime.Parse(dr1("orderDates").ToString())
Dim dates As DateTime = DateTime.Now()

If dates.Subtract(odate).TotalDays >= 2 Then
    cancelBtn.Enabled = False
Else
    cancelBtn.Enabled = True
End If

You could also consolidate the If statement to a single line:

cancelBtn.Enabled = dates.Subtract(odate).TotalDays < 2

Upvotes: 2

Joshua
Joshua

Reputation: 43317

DateTime.Parse(Now().ToString()).ToShortDateString()

Replace with

Today

And otherwise, don't manipulate dates in strings.

Upvotes: 0

Related Questions