Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1112
  • Last Modified:

C# - Casting error

Hello Experts,
I am trying to get SQLConnection from Entityconnection:

http://justgeeks.blogspot.com/2009/11/getting-sqlconnection-from.html

I have this namespace:
using System.Data.EntityClient;

I am getting this error on Casting:


 private string GetADOConnectionString()
        {
            EcommerceDataConnection ctx = new EcommerceDataConnection();
            System.Data.Common.DbConnection dbConn= ctx.Database.Connection;
            EntityConnection ec = (EntityConnection)dbConn;  
            System.Data.SqlClient.SqlConnection sc = (System.Data.SqlClient.SqlConnection)ec.StoreConnection;
            string adoConnStr = sc.ConnectionString;
            return adoConnStr;

        }


-------------------

Unable to cast object of type 'System.Data.SqlClient.SqlConnection' to type 'System.Data.EntityClient.EntityConnection'.


Error:
Capture-Error.PNG
0
mani_sai
Asked:
mani_sai
1 Solution
 
anarki_jimbelCommented:
Your code is quite different from the source you are referring to.

I don't know API for  EcommerceDataConnection . But the source of an error is obvious:
you are trying to cast DBConnection to EntityConnection which is wrong:

EntityConnection ec = (EntityConnection)dbConn;  

Open in new window


May be something like:

            EcommerceDataConnection ctx = new EcommerceDataConnection();
            System.Data.Common.DbConnection dbConn= ctx.Database.Connection;
            System.Data.SqlClient.SqlConnection sc = (System.Data.SqlClient.SqlConnection)dbConn;

Open in new window

0
 
mani_saiAuthor Commented:
Thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now