Looping through one specific column in SQLDataReader.

Myat_Kaung

Member
Joined
Aug 22, 2022
Messages
6
Programming Experience
Beginner
Loop through specific column:
while (rdr.Read())
                {
                    string column = rdr[1].ToString();

                    int columnValue = Convert.ToInt32(column);
    
    for (--)

So The data I want to retrieve has two columns. I want to get the second column and the values in this column are in integer value. After that I want to loop through this second column and inside this loop, I will put a condition like if one of the value in second column > 10, then output something. Could anyone help me with that pls?
 
Solution
Why would you need two loops? The data reader will read the data sequentially. If that's what you want to do then use a while loop and process each row one by one. If you don't want to process the data sequentially then don't use a while loop in the first place. Create a DataTable and Load the data reader into it and then use a for or foreach loop. You don't need both. I could be wrong but it seems that you only need the while loop, exactly as has already been explained and you have apparently disregarded. It seems like you need something like this:
C#:
while (reader.Read())
{
    var column1 = reader.GetInt32(1);
    
    if (column1 > 10)
    {
        var column0 = reader.GetString(0)...
You don't need that for loop that you were starting. Your current while loop will already go through all the rows one at a time. All you just need to to is check to see if columnValue is greater than 10, then do your output.

This isn't boding well. In your other thread you said you knew how to handle stuff when you do a query and the results were a particular value and the only help you needed back then was how to call a stored procedure instead of a query. In this case you are doing a query, but somehow you have forgotten how to handle the results when the results value is greater than 10.
 
thank you @Skydiver for the information. Yes what I think is SQLDataReader will give me a list for row 1. So in that case I have to create a for loop. This is what I think. Anyway, thank you.
 
Why would you need two loops? The data reader will read the data sequentially. If that's what you want to do then use a while loop and process each row one by one. If you don't want to process the data sequentially then don't use a while loop in the first place. Create a DataTable and Load the data reader into it and then use a for or foreach loop. You don't need both. I could be wrong but it seems that you only need the while loop, exactly as has already been explained and you have apparently disregarded. It seems like you need something like this:
C#:
while (reader.Read())
{
    var column1 = reader.GetInt32(1);
    
    if (column1 > 10)
    {
        var column0 = reader.GetString(0);
        
        // Use column0 here.
    }
}
It's rather silly getting the field value as an object reference, converting that to a string and then converting that to an int. If the data is 32-bit numbers then it should be stored as that type, so get the data as that type from the get-go. The only other thing to consider is that, if the column is nullable, you'll have to check for that first. The data reader has a method for that too.
 
Solution
Why would you need two loops? The data reader will read the data sequentially. If that's what you want to do then use a while loop and process each row one by one. If you don't want to process the data sequentially then don't use a while loop in the first place. Create a DataTable and Load the data reader into it and then use a for or foreach loop. You don't need both. I could be wrong but it seems that you only need the while loop, exactly as has already been explained and you have apparently disregarded. It seems like you need something like this:
C#:
while (reader.Read())
{
    var column1 = reader.GetInt32(1);
   
    if (column1 > 10)
    {
        var column0 = reader.GetString(0);
       
        // Use column0 here.
    }
}
It's rather silly getting the field value as an object reference, converting that to a string and then converting that to an int. If the data is 32-bit numbers then it should be stored as that type, so get the data as that type from the get-go. The only other thing to consider is that, if the column is nullable, you'll have to check for that first. The data reader has a method for that too.
Thank you so much. It helps me a lot.
 
Back
Top Bottom