1

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 :)

1
  • since you have mentioned is this because MySQL stores dates in a different way.... assumed that StartPeriod is a date type column. Please comment if it's not, if so my answer isn't valid. Commented Jan 29, 2013 at 14:22

1 Answer 1

1

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()

Sign up to request clarification or add additional context in comments.

3 Comments

Ah apologies I forgot to include an earlier bit of code. Where it says InputBookingDate that's reading from a Date variable. Start Period, Length, UserID and RoomID are all just integers.
Please add that code as well. Further, can you confirm that you are only returning Start Period, Length, UserID and RoomID and they are in order? If not please show the entire query plus the columns you are pulling out.
InputBookingDate = DateTimePicker1.Value.Date ^ I did this to isolate it so it was just a date. GetBookingData() ^ That runs the function which is the code I previously showed. Public BookingDate() As Date Public StartPeriod As Integer Public Length As Integer Public UserID As Integer Public RoomID As Integer ^ These are the variables which they are being stored to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.