Reputation: 11
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);
}
}
Upvotes: 1
Views: 1406
Reputation: 9577
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.
Upvotes: 1