Reflection with SqlConnection

  • Thread starter Thread starter WebDevHokie
  • Start date Start date
W

WebDevHokie

I am using reflection to create a data access component. I have the
following code:

SqlConnection o_Temp = new SqlConnection();
System.Data.IDbConnection o_Connection;
o_Connection = o_Temp as IDbConnection;

This works correctly but it does not use reflection. Now I am looking to
use this code:


System.Reflection.Assembly o_Assembly =
System.Reflection.Assembly.LoadWithPartialName("System.Data");
object o_Temp2 =
o_Assembly.CreateInstance("System.Data.SqlClient.SqlConnection");
System.Data.IDbConnection o_Connection;
o_Connection = o_Temp2 as IDbConnection;

This does not work as o_Connection is null when it attempts to cast. Any
idea why this would be?

Thanks in advance!
 
I copied your code and it works fine here. Look for another problem past the
line that is doing the cast.

Kathleen
 
FWIW, this machine has both the frameworks on it.

But I don't think I can help you here. Before you burn a support call, maybe
repair the .NET your working in, if its on a single machine.

Kathleen
 
Back
Top