Henry Penton
Henry Penton

Reputation: 97

Using dates between VB and SQL

I am building a school project which is a room booking service. I have reached the part of the programming where I need to figure out how to store bookings. I know how to pull from a database using VB and I've laid it out like that when I try to pull booking information from the database.

    strBookingQuery = "SELECT * FROM bookings WHERE Date = '" & InputBookingDate & "'"
    Dim Cmd As New MySqlCommand(strBookingQuery, Newconnection)
    Newconnection.ConnectionString = strServerString
    Newconnection.Open()
    reader = Cmd.ExecuteReader()
    reader.Read()

    StartPeriod = reader.GetInt16(1)
    Length = reader.GetInt16(2)
    UserID = reader.GetInt16(3)
    RoomID = reader.GetInt16(4)
    Newconnection.Close()

But when it gets to StartPeriod = reader.GetInt16(1) it says Invalid attempt to access a field before calling Read(), is this because MySQL stores dates in a different way? Or something else? Any help would be greatly appreciated :)

Upvotes: 1

Views: 124

Answers (1)

bonCodigo
bonCodigo

Reputation: 14361

How do you expect GetInt16 to read a date?

Reader.GetInt16() gets the value of the specified column as a 16-bit signed integer. No conversions are performed; therefore, the data retrieved must already be an Int16 or coercible to an Int16.

Given that OP confirmed there's no issue with startPeriod, the possible issue could be:

Invalid attempt to access field before calling read()

Upvotes: 1

Related Questions