sam kokane sam kokane - 11 months ago 79
ASP.NET (C#) Question

How to get Column values stored in session datatable?

I have stored


DataTable dt = new DataTable();
dt = (DataTable)Session["data"];

How do I get column values stored in
? Moreover I want to pass column name dynamically.

Let's say I have
in which I have columns as Column 0, Column 1, Column 2. So if user selected Column 0, my input column will be Column 0. With that Column name i want store related column values in Database. So, How will be my c# code and SQL query ?

I do have read related questions but not getting exact idea.

Answer Source

You can use dt.Columns["Column 0"] to get the column by name.

DataColumn selectedColumn = dt.Columns["Column 0"];
List<object> allValues = dt.AsEnumerable().Select(r => r[selectedColumn]).ToList();