1

I'm writing a calorie counter tool for my girlfriend...

But I've run in to an issue I really cant figure out...

I have place in my script where I would wanna take some information out from my db...

The specific place in the script thats teasing, is the one that will take out data from a row called "antal" from a table called "items".

One place in my script, I call "navn" from items, and it works perfect...

This other place, it gives me the error that there isn't any data in the row!

My code looks so far like this:

Imports System.Data.OleDb

Private Sub ins_kat_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ins_kat.SelectedIndexChanged
Dim Con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Privat\MCC\mccdb.mdb")
Con.Open()
Dim command As OleDbCommand = New OleDbCommand("SELECT * FROM items WHERE kat = '" & ins_kat.Text & "' ORDER BY id DESC", Con)
Dim read As OleDbDataReader = command.ExecuteReader()
ins_mad.Items.Clear()
While read.Read()
ins_mad.Items.Add(read.Item("navn")) '<---- This place works!!!
End While
Con.Close()
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

Dim ialt_c_counter As String
Dim ialt_c_counter_conv As Decimal
Dim ialt_c_counter_conv2 As Decimal
Dim ialt_counter As String
Dim ialt_final As Decimal


Dim Con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Privat\MCC\mccdb.mdb")
Con.Open()
Dim command1 As OleDbCommand = New OleDbCommand("SELECT * FROM food WHERE dag = '" & stat_dag.Text & "' AND maaned = '" & stat_maaned1.Text & "' AND aar = '" & stat_aar1.Text & "'", Con)

Dim read1 As OleDbDataReader = command1.ExecuteReader()

While read1.Read()
ialt_c_counter = read1.Item("antal")
ialt_c_counter_conv = System.Convert.ToDecimal(ialt_c_counter)

Dim commandX As OleDbCommand = New OleDbCommand("SELECT * FROM items", Con)
Dim reader As OleDbDataReader = commandX.ExecuteReader()

ialt_counter = reader.Item("antal") '<--- THIS ONE is giving me the error!!
ialt_c_counter_conv2 = System.Convert.ToDecimal(ialt_counter)

ialt_final = (ialt_c_counter_conv / 100 * ialt_c_counter_conv2) + ialt_final

End While

Con.Close()
MsgBox(ialt_final)
End Sub
End Class

By now, I want a MsgBox telling me the result of my algorithm, but I don't even get that far cause of the error message...

0

1 Answer 1

1

I guess the error you see is

InvalidOperationException: Invalid attempt to read when no data is present.


You never call Read() on the OleDbDataReader called reader.

Note how you use a While loop to call Read() for read and read1, but not reader (bad variable names, btw).

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

3 Comments

Yes, that is exactly the error i get!! Yes I'm doing a while loop two of the places, and that's intended.. I don't do it on reader, because it only has ONE element that I want to get, for every time my read1 loop loops..
The last sql string looks like this in my example: "SELECT * FROM items", but I want it to go "SELECT * FROM items WHERE navn = '" & a_variable_name & "', so thats why..
Use a While loop or not, you have to call Read() at least once on reader...

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.