Input string was not in a correct format.

B

bazzer

i m trying to compare a time i have stored in a session variable, to a
time of day:
If (Session("theShowTime") < 18.0) Then ....

basically i want to know if the time is before or after 6pm.
'theShowTime' is in short-time format from a microsoft access
table(18:00).

but i get the error:
Input string was not in a correct format.

how should i write the statement correctly?
 
R

Ray Booysen

bazzer said:
i m trying to compare a time i have stored in a session variable, to a
time of day:
If (Session("theShowTime") < 18.0) Then ....

basically i want to know if the time is before or after 6pm.
'theShowTime' is in short-time format from a microsoft access
table(18:00).

but i get the error:
Input string was not in a correct format.

how should i write the statement correctly?
Convert.ToDateTime(Session("theShowTime")) will convert it to a date
time object.

When retrieving an item from the session object, the resulting object is
that, a System.Object. You need to cast it to the proper type.
 
B

bazzer

ok. now i have:

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ListBox1.SelectedIndexChanged

Session("theShowTime") = ListBox1.SelectedItem.Text

Dim theTime As DateTime =
Convert.ToDateTime(Session("theShowTime"))

If (theTime < "18.0") Then
Session("theRate") = "dayTime"
Else
Session("theRate") = "nightTime"
End If


Response.Redirect("http://localhost/CinemaBookingSystem/WebForm11.aspx")
End Sub

but i get the error:
Cast from string "18.0" to type 'Date' is not valid.

i maybe should also mention that on the previous webform when i created
Session("theShowTime"), i got it from a listbox, which was populated
from my access database. but in the datasource properties of the
listbox, i have the DataTextFormatString property set to {0:t}, so that
the listbox would show just the times and not DateTimes.
 
R

Ray Booysen

Your if statement is comparing theTime (a DateTime Object) to "18.0"
which is of type string. You cannot compare a string to a DateTime object.
 
B

bazzer

ya thanks. i just realised that. declared a variable as a DateTime, and
then just used that in the comparison and it works fine now.

Dim aTime As DateTime = "18:00"

If (theTime < aTime) Then ...

thanks Ray.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top