I don't really know how to ask it in the title, but I will try my best on explaining it here..
First, I tried to look at THIS.. The thing is, I cannot follow its statement. It dances in my head.
Here's what I am aiming for.
get the following IDs of the next rows WHERE fieldFoo = foo
Flow of the program (MS Access & VB.Net):
DGV's are not connected to the database, I just use them for Display of data that they will save/Update.
Dim query As String = "SELECT TOP 1 * FROM piglets WHERE CurrentLocation=@cl AND Week=@week AND SowOrder=@sow ORDER BY ID ASC"
- User inserts a number, software checks if the data is there. If its there, it will add it to the DGV.
- User needs to save the
UPDATE
query. This works perfectly but only with ONE row. - Since it gets only one, it only gets an ID, which is what I basically user as their identity.
What I have tried:
- I added a list of integer in which it will contain the ID each time a user looks up for a data, add it to the DGV, look again, add to DGV. But it only returns the same item, that's why when I try to save, it only updates that one item.
- Re-run the sub, but its an error, can't figure out why.
Here is the code for looking up the data
Using cmd As New OleDbCommand(query, con)
With cmd.Parameters
.AddWithValue("@cl", comboFrom.Text)
.AddWithValue("@week", mtWeek.Text)
.AddWithValue("@sow", mtSow.Text)
End With
Dim dr As OleDbDataReader = cmd.ExecuteReader()
If dr.HasRows() Then
If dr.Read() Then
Do
Dim loc As String
loc = dr("CurrentLocation").ToString()
// id = dr("ID")
tempIdList.Add(dr("ID"))
If loc = comboFrom.Text Then
isContinue = True
End If
Loop While dr.Read
End If
Else
isContinue = False
MessageBox.Show("Piglet look up error.", "MR Livestock", MessageBoxButtons.OK, MessageBoxIcon.Error)
clear()
End If
End Using
This is how I save
Dim query As String = "UPDATE Piglets SET Mortality=@mortality, CurrentLocation=@me" & _
" WHERE ID=@id"
// For Each i In tempIdList
For Each row As DataGridViewRow In dgvMortality.Rows
Using cmd As New OleDbCommand(query, con)
With cmd.Parameters
.AddWithValue("@mortality", row.Cells("mortality").Value)
.AddWithValue("@me", "Mortality")
.AddWithValue("@id", tempIdList.Item(id))
End With
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
id = id + 1 // I was hoping here to have the IDs, but I forgot that the query gets only one, so if the are two items, they are the same.
End Using
Next
// Next
The result that I want is for the user to be able to add multiple entries to the DGV, then can save it all at once using the ID that (maybe pre-saved in a list) is from the database. For now, I can save only one, since it gets the TOP 1 only, even if I entered as many data as I want.