1

Pretty much stuck on this for the past few days. I wouldn't normally post here but what I'm trying to come up with just searching on my own doesn't work. I want to query PostgreSQL and come up with multiple records that each have multiple fields (indicated by my SELECT statement). Since I don't know the # of records returned I figured some sort of while loop was best. I just cant seem get all my values as a list and then throw that list into a table, adding rows as needed.

NpgsqlConnection pgconn = new NpgsqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
pgconn.Open();

NpgsqlCommand command = new NpgsqlCommand("SELECT line, oper, subst_a, from_loc, to_loc, area " + 
                                          "FROM ab_basedata.superpipes_ihs " +
                                          "WHERE gdm_approv = '" + lic_num_lbl + "'", pgconn);

List<List<string>> pipes = new List<List<string>> { };
NpgsqlDataReader dr = command.ExecuteReader();

while (dr.Read())
{
    pipes.Add("Line: " + dr.GetValue(0) + " " + dr.GetValue(1) + " " + dr.GetValue(2) + " " + dr.GetValue(3) + " " + dr.GetValue(4) + " " + dr.GetValue(5) + " Office");

    foreach (List<string> pip in pipes)
    {
        TableRow row = new TableRow();
        TableCell cell1 = new TableCell();
        cell1.Text = string.Join(" ", pipes);
        row.Cells.Add(cell1);
        docTable.Rows.Add(row);
    }
}

1 Answer 1

1

You could try recoding the lines after you create the command like this...

List<List<string>> pipes = new List<List<string>>();
NpgsqlDataReader dr = command.ExecuteReader();

while (dr.Read())
{
    List<string> pip = new List<string>();

    pip.Add("Line:");

    for (int i = 0; i < dr.FieldCount; i++)
        pip.Add(dr.GetString(i));

    pip.Add("Office");

    TableRow row = new TableRow();
    TableCell cell1 = new TableCell();
    cell1.Text = string.Join(" ", pip);
    row.Cells.Add(cell1);
    docTable.Rows.Add(row); 

    pipes.Add(pip);
}

// close DB resources if finished with them
dr.close();
pgconn.close();

I'm assuming here that you really do want to stuff all the data into one cell, rather than a cell for each item. If you don't need pipes elsewhere in your code, then it can be removed.

1
  • That worked, thanks. Although it doesn't look pretty, I might try some formatting or maybe break it up column wise, just not sure how in depth I want to get with this info since it's more of an after thought when the user pulls up pipeline information.
    – NastyNast
    Commented May 24, 2013 at 16:09

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.