Loop through search results, and filter?

Wabster

Member
Joined
Oct 1, 2009
Messages
7
Programming Experience
Beginner
Hi,

I have a query that looks up some data on stock levels.
It returns about 15-20 rows to the myReader.

I have 15 text boxes (emtpy) that need populating with column B, based upon Column A of the query.

So if row 5 has Column A '1' then textbox2.text needs to equal column B of row 5.

I am not sure of the function I need to lookup to find out how to acheive this.

Any help will be greatly appreciated :)
 
First up, it sounds like you need random access to your data, so a DataReader isn't going to cut it. In that case you should populate a DataTable. You'll have to create a new DataTable first, then call its Load method and pass the DataReader.

Once you're there, I really don't know what you need to do because I don't really understand what you're trying to achieve. Can you provide a clearer explanation?
 
I will try to explain in more detail if this helps.

When I run the SQL query - I have the following data returned:

Row 0 = ItemID, WarehouseID, WarehouseItemID, Qty
Row 1 = ItemID, WarehouseID, WarehouseItemID, Qty
Row 2 = ItemID, WarehouseID, WarehouseItemID, Qty
Row 3 = ItemID, WarehouseID, WarehouseItemID, Qty
Row 4 = ItemID, WarehouseID, WarehouseItemID, Qty
Row 5 = ItemID, WarehouseID, WarehouseItemID, Qty
Row 6 = ItemID, WarehouseID, WarehouseItemID, Qty

I want the datareader to loop through the data, and

IF WarehouseID = '1234' THEN
Textbox1.Text = Qty
Elseif WarehouseID = '4321' THEN
Textbox2.Text = Qty
etc etc.

I want it too loop the process as many times as there are rows, to ensure that all QTY has been explored for what I require.

I assume I have to use the myReader.NextResult() command, but this doesnt seem to work in conjunction with the Loop properly?

Any good guide on DataTables if I need to use one?
Quiet a N00b and have this one project to finish.

Cheers
I only want to read data. I do not want to update/delete any data either :) If this helps
 
I have solved the problem.

Incase anybody else is having this issue then the following may help?

Each time the .Read is exeuted, the datareader goes onto the NEXT row. So just call a .Read inbetween a Do While loop.

My code looks like this:

VB.NET:
With myReader.Read()
If myReader.HasRows Then
Do While myReader.Read()
.........
.........
.........
Loop
Else
........
End With

Works a charm :)
 
If you fill a datatable you then dont have to loop thru every record looking for the one you want. The datatable has methods available for searching and filtering records.
 
Each time the .Read is exeuted, the datareader goes onto the NEXT row. So just call a .Read inbetween a Do While loop.

While reader.Read()
...
End While

Is neater -> reader.Read() returns false if there is nothin to read, ergo if there is nothing at all to read the while loop executes 0 times.

Clever eh?
 

Latest posts

Back
Top