Solved

C# - Casting error

Posted on 2013-05-21
2
892 Views
Last Modified: 2013-05-22
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
Comment
Question by:mani_sai
2 Comments
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 39185821
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
 
LVL 8

Author Closing Comment

by:mani_sai
ID: 39189170
Thanks
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

821 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