sam kokane sam kokane - 1 month ago 15
ASP.NET (C#) Question

How to get Column values stored in session datatable?

I have stored

DataTable
in
Session
as

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


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

Let's say I have
DataTable
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

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();