I am creating a PDF thru an ASP.NET Core MVC controller using a DataTable and NewRow. However I would like to create a multiple rows in one column coming from SQL Server
Information | Person Information |
---|---|
Name | Person Name |
Address | Person Address |
Contact No. | Person Contact No |
On the left side it should be the column but I always get only one item of data:
Information | Person Information |
---|---|
Contact No. | Person Contact No |
The two rows are skipped and didn't get displayed.
string person_name = string.Empty; //Imagine that I put some data here coming from SQL Server
string person_address = string.Empty;
string person_contact = string.Empty;
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Information");
dataTable.Columns.Add("Person Information");
DataRow row = dataTable.NewRow();
row[0] = "Name";
row[0] = "Address";
row[0] = "Contact No";
row[1] = person_name;
row[1] = person_address;
row[1] = person_contact;
I expected that the result should be the first table but after the pdf downloaded and showed. The second table showed as the result. How can I achieve something like the first table?
PS: I'm very new about creating DataTable in ASP.NET Core 5 MVC, any ideas will be appreciated. TIA
CodePudding user response:
You currently overwrite the values. Try this.
// Create a new row for each item of data
DataRow nameRow = dataTable.NewRow();
nameRow[0] = "Name";
nameRow[1] = person_name;
dataTable.Rows.Add(nameRow);
DataRow addressRow = dataTable.NewRow();
addressRow[0] = "Address";
addressRow[1] = person_address;
dataTable.Rows.Add(addressRow);
DataRow contactRow = dataTable.NewRow();
contactRow[0] = "Contact No";
contactRow[1] = person_contact;
dataTable.Rows.Add(contactRow);