0

I want to select mysql column names and values and display them for each value as rows in a gridview table. Here is an illustration

+------+-------+------+-------+-------+
| id   |amt1   |amt2  |amt3   |amt4   |
+------+-------+------+-------+-------+
|  1   | 300   | 500  | 460   | 780   |
+------+-------+------+-------+-------+
|  2   | 450   | 230  | 320   | 410   |
+------+-------+------+-------+-------+
|  3   | 350   | 540  | 430   | 310   |
+------+-------+------+-------+-------+

I want this mysql table to be displayed as this in the gridview table

+-------------+--------+
|particulars  | amount |
+-------------+--------+
|  amt1       | 300    |
+-------------+--------+
|  amt2       | 500    |
+-------------+--------+
|  amt3       | 460    |
+-------------+--------+
|  amt4       | 780    |
+-------------+--------+

where id = 1.

halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

0

Just run 2 of those queries, the first will give you column names, the second will give you the row for specific id you looking for. Once you get this data it should be pretty simple to create the table from this data using System.Data.SqlClient.SqlConnection.

columnNamesQuery = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'YourTableName'";

dataQuery = "SELECT * FROM YourTableName WHERE id = 1";
Felix Av
  • 1,254
  • 1
  • 14
  • 22
0
select 'amt1' particulars, amt1 amount from t1 where id=1 
union 
select 'amt2' particulars, amt2 amount from t1 where id=1 
union 
select 'amt3' particulars, amt3 amount from t1 where id=1
union 
select 'amt4' particulars, amt4 amount from t1 where id=1 
splash58
  • 26,043
  • 3
  • 22
  • 34