locked
Linq to SQL..How to return values for a single date in datetime format in VB .Net RRS feed

  • Question

  • User1277446335 posted

    So I am trying the following to select a long date, but becase the value is stored in SQL database as DateTime I am unable to return any results unless minutes and seconds match perfectly...I then tried to use 2 calendars and have the same issue with the DateTime format..Any advice would be greatly appreciated

     

        Protected Sub btnSearchByDate_Click(sender As Object, e As System.EventArgsHandles btnSearchByDate.Click
            Dim db As New VisitorsDataContext
            Dim ipAddress = From ip In db.tblTrackEvents
                            Where ip.te_date = Calendar1.SelectedDate
                            Select ip
            Try
                GridView3.DataSource = ipAddress
                GridView3.DataBind()
            Catch ex As Exception
                MsgBox(ErrorToString)
            End Try
        End Sub

     

    Wednesday, November 30, 2011 8:05 AM

Answers

  • User617492218 posted

    well, you'll never get a match, since you're trying to see 

    ip.te_date = Calendar1.SelectedDate

    what you can try to do is this: - sorry i write C#, dont know VB

    DateTime date = DateTime.Parse(Calendar1.SelectedDate);
    
    string newDate = date.ToString("MM/dd/yyyy");  // now you wont have the time in there
    
    var ipAddress = db.tblTrackEvents.Where(ip => ip.te_date.StartsWith(newDate)).ToList();

    i'm guessing you're trying to pull back multiple values from the table??? and what are you trying to relate, b/c if you have a Calendar, and they pick the date, the time will be in there. are you just trying to pull back items based on the date? if you are, the above code should work. if not, please explain in more detail what you are trying to accomplish

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 30, 2011 9:58 AM