Solved

C# - Casting error

Posted on 2013-05-21
2
1,025 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 30

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

615 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