0

I have the following code on my C# WinForm, which builds a query and passes it and a parameter to a function that queries the PostgreSQL database:

DataTable dt = ((DataTable)dgvCategories.DataSource);

List<string> validationInfoResult = new List<string>();
for (intRowIndex = 0; intRowIndex <= dt.Rows.Count - 1; intRowIndex++)
{
    strNewValueCat = dt.Rows[intRowIndex][1].ToString();
    db getValidationInfo = new db();

    strGetValidationInfoQuery = "SELECT validationdb, validationtable, validationfield, validationfield2, validationvalue2" +
                                " FROM masterfiles.categories" +
                                " WHERE category = @category";
    validationInfoResult = getValidationInfo.GetValidationInfo(strGetValidationInfoQuery, strNewValueCat);
}

As you can see, I'm iterating through a DGV's DataTable, grabbing a value from a particular cell in the current row and passing that as a parmaeter to the db query. I need to return values from 5 fields and my intention is to put the query result into a List<> and then get each of the List<> items into their own variable for use in the form.

My db class function looks like this:

public List<string> GetValidationInfo(string strQuery, string strCategory)
{
    List<string> strResult = new List<string>();

    using (NpgsqlConnection conn = new NpgsqlConnection(connString))
    using (NpgsqlCommand cmd = new NpgsqlCommand(strQuery, conn))
    {
        cmd.Parameters.Add(new NpgsqlParameter("category", strCategory));
        conn.Open();

        using (NpgsqlDataReader reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                strResult.Add(reader.GetString(0));
                Console.WriteLine("reader: " + reader.GetString(0));
            }
        }
    }

    return strResult;
}

I built the above code based on this SO question's answer.

When I run it, Console.WriteLine() only ever outputs the first query field, validationdb and then moves on to the next DataTable row in the calling for loop. Stepping through the code at runtime, after the first time it hits Console.WriteLine() it goes back to while (reader.Read()) and then steps out of the while loop and returns to the calling function.

What is wrong in my loop that it's only putting the first query field result into the List<>?

marky
  • 4,878
  • 17
  • 59
  • 103

2 Answers2

0

Following code would output all fields:

while (reader.Read())
{
    // strResult.Add(reader.GetString(0));
    Console.WriteLine("Column 1: " + reader.GetString(0));
    Console.WriteLine("Column 2: " + reader.GetString(1));
    Console.WriteLine("Column 3: " + reader.GetString(2));
    Console.WriteLine("Column 4: " + reader.GetString(3));
    Console.WriteLine("Column 5: " + reader.GetString(4));
}

So you need to modify your output List<string> to accommodate 5 strings per item.

Nenad
  • 24,809
  • 11
  • 75
  • 93
-1

The problem is GetString(0) which fetches only the first column of a row (index 0). One way of getting around this would be by looping through the columns. From the docs, FieldCount can be used to fetch the number of columns:

while (reader.Read())
{
    for(int i = 0; i < reader.FieldCount; i++)
    {
        strResult.Add(reader.GetString(i));
        Console.WriteLine("reader: " + reader.GetString(i));
    }
}
Anoop R Desai
  • 712
  • 5
  • 18