Enterprise Library 3.1 -- Error in code -- Cannot implicitly convert type 'System.Data.IDbConnection'

Topics: Data Access Application Block
Apr 3, 2008 at 5:39 AM
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

private Database daabDB = null;
private IDbTransaction dbTransaction = null;
private IDbConnection dbConnection = null;

public DataSet ExecuteSQLForReading(string sql)
{
DataSet retDataSet = null;
try
{
OpenConnection();

System.Data.Common.DbCommand cmd = daabDB.GetSqlStringCommand(sql);

if (dbConnection != null)
cmd.Connection = this.Connection; //Error here --> Cannot implicitly convert type //'System.Data.IDbConnection' to 'System.Data.Common.DbConnection'. An explicit conversion exists (are you missing a //cast?)

if (dbTransaction != null)
cmd.Transaction = dbTransaction; //Error here -->Cannot implicitly convert type 'System.Data.IDbConnection' //to 'System.Data.Common.DbConnection'. An explicit conversion exists (are you missing a cast?)

retDataSet = daabDB.ExecuteDataSet(CommandType.Text, sql);
//CloseConnection();
}
catch (Exception ex)
{
//CloseConnection();
throw ex;
}
return retDataSet;
}


When i am compiling the code i am getting error like "Cannot implicitly convert type 'System.Data.IDbConnection' to 'System.Data.Common.DbConnection'. An explicit conversion exists (are you missing a cast?)". Please help me in the same. Thanks in advance

Arun
Apr 4, 2008 at 10:02 PM
Hi Arun,

You've declared your variables with the wrong type, as indicated by the compiler. Use DbConnection and DbTransaction instead of IDbConnection and IDbTransaction.

Fernando