Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Dynamic Cast in C#

Posted on 2006-03-22
Medium Priority
Last Modified: 2010-05-18
Hi All,

I have a dynamic SQL query which can return a variable number of columns.

What I'm trying to do is dynamically create the datatable based on the datareader.  
source is an actual SqlDataReader so that cast is valid (in this case it can be other things but for right now I'm just concentrating on this case).

// create datatable.
  DataTable dt = new DataTable();

// add the columns based on the number of columns in the datareader
                for (int i = 0; i < ((SqlDataReader)source).FieldCount; i++)
                    dt.Columns.Add(i.ToString(), ((SqlDataReader)source)[i].GetType());

                while (((SqlDataReader)source).Read())
//create new datarow
                    DataRow r = dt.NewRow();

//trying to assign the datareader value to the row value.  This is where the dynamic cast comes in.
//I won't know the actual type until runtime, as I said values and columns can change.
                    for (int j = 0; j < dt.Columns.Count; j++)
                      r[j] =  (Type.GetType((((SqlDataReader)source)[j].GetType().ToString())) ((SqlDataReader)source)[j] );

//can now work on datatable.
Question by:Kyle Abrahams
  • 2
LVL 35

Expert Comment

ID: 16259113
Is there any reason not to load directly into a data table instead of a datareader??
LVL 41

Author Comment

by:Kyle Abrahams
ID: 16263973
how so?
LVL 35

Accepted Solution

mrichmon earned 2000 total points
ID: 16264227
Well without seeing the code that loads into the SqlDataReader I can't give you specifics, but something like:

SqlConnection nwindConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind");

SqlCommand selectCMD = new SqlCommand("SELECT CustomerID, CompanyName FROM Customers", nwindConn);
selectCMD.CommandTimeout = 30;

SqlDataAdapter custDA = new SqlDataAdapter();
custDA.SelectCommand = selectCMD;


DataSet custDS = new DataSet();
custDA.Fill(custDS, "Customers");


Then you now have


and that is how to access your data table.

Now there are more .NET 2 methods to do this and even better is using a single line or two of code using the Enterprise Library.

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question