Solved

C# - Casting error

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
error on the asp.net page 1 33
DateTimepicker 4 33
free scanner TWAIN can be operated with a Web application 9 39
How can my static class become undefined?? 8 0
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

22 Experts available now in Live!

Get 1:1 Help Now