Dynamic Cast in C#

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.
LVL 42
Kyle AbrahamsSenior .Net DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Is there any reason not to load directly into a data table instead of a datareader??
Kyle AbrahamsSenior .Net DeveloperAuthor Commented:
how so?
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.