Solved

C# - Casting error

Posted on 2013-05-21
2
852 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now