Share via


Cannot create the instance of Abstract or interface 'syste..data.common.dbconnection

Question

Monday, June 6, 2011 5:59 AM

Hi all ,

Please help to solve the error given in the following code.  I am using MS access database and C# expression. The error message is showing 'Cannot create the instance of Abstract  or interface 'syste.data.common.dbconnection'. on the line

 DbConnection conn = new DbConnection(connectionString);

Pleae help with correct code

Databse connection.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
  </configSections>
  <connectionStrings>
    <add name="WindowsFormsApplication1.Properties.Settings.employeeConnectionString"
      connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\employee.mdb"
      providerName="System.Data.OleDb" />
  </connectionStrings>
</configuration>


Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.Common;


namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

      FillData();
    }
    private void FillData()
    {

      string connectionString = ConfigurationManager.ConnectionStrings["WindowsFormsApplication1.Properties.Settings.employeeConnectionString"].ConnectionString;
      //DbConnection conn = DbConnection(
      DbConnection conn = new DbConnection(connectionString);
      {
        conn.Open();
        using (SqlCommand cmd = new SqlCommand(" Select employee from employee", conn))
        {
          SqlDataReader reader = cmd.ExecuteReader();
          dataGridView1.DataSource = reader;
        }



      }


    }

  }
}

polachan

All replies (9)

Monday, June 6, 2011 6:57 AM ✅Answered | 1 vote

DBConnection is an abstract class hence you cannot create a instance of the same.

 

However since it inherits from IDbConnection you should be able to hold connection to various databases. Like this

 

DbConnection oleDBConnection = new OleDbConnection();

DbConnection sqlConnection = new SqlConnection();

if this answers please mark as answers, if it helps please mark as helpful

 

Regards, Krishnakant


Monday, June 6, 2011 7:15 AM ✅Answered

You might use OleDbConnection to connect to Ms Access database

public void ConnectToAccess()
{
  System.Data.OleDb.OleDbConnection conn = new 
    System.Data.OleDb.OleDbConnection();
  // TODO: Modify the connection string and include any
  // additional required properties for your database.
  conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
    @"Data source= C:\Documents and Settings\username\" +
    @"My Documents\AccessFile.mdb";
  try
  {
    conn.Open();
    // Insert code to process data.
  }
    catch (Exception ex)
  {
    MessageBox.Show("Failed to connect to data source");
  }
  finally
  {
    conn.Close();
  }
}
You can refer http://msdn.microsoft.com/en-us/library/5ybdbtte(v=vs.71).aspx

Malayalam SMS Website http://aspspider.info/smsmallu


Monday, June 6, 2011 3:36 PM ✅Answered

You can't bind to a datareader. You need to create a dataset or dataview:

      string connectionString = "Data Source=.;Initial Catalog=pubs;Integrated Security=True";
      string sql = "SELECT * FROM Authors";
      SqlConnection connection = new SqlConnection(connectionString);
      SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
      DataSet ds = new DataSet();
      connection.Open();
      dataadapter.Fill(ds, "Authors_table");
      connection.Close();
      dataGridView1.DataSource = ds;
      dataGridView1.DataMember = "Authors_table";

Adam

Ctrl+Z


Monday, June 6, 2011 11:11 AM

Thnaks for the reply . But my question how I can run

using (SqlCommand cmd = new SqlCommand(" Select employee  from employee", conn)

    {
                    SqlDataReader reader = cmd.ExecuteReader();
                    dataGridView1.DataSource = reader;
     }

 

I wanto to poppulate sql statement in to my Gridview . Sqlcommand or DBcommand is not working. My code is as follows connection is success. How I can bring into gridview. Please help

 private void FillData()
        {
            DbConnection oleDBConnection = new OleDbConnection();
            oleDBConnection.ConnectionString = ConfigurationManager.ConnectionStrings["WindowsFormsApplication1.Properties.Settings.employeeConnectionString"].ConnectionString;
            oleDBConnection.Open();
           

           // string connectionString = ConfigurationManager.ConnectionStrings["WindowsFormsApplication1.Properties.Settings.employeeConnectionString"].ConnectionString;
            //using (SqlConnection conn = new SqlConnection(connectionString))
            //{
            //    conn.Open();
            //    using (SqlCommand cmd = new SqlCommand(" Select employee  from employee", conn))
            //    {
            //        SqlDataReader reader = cmd.ExecuteReader();
            //        dataGridView1.DataSource = reader;
            //    }
            //}

        }

polachan


Monday, June 6, 2011 11:15 AM

After you assign the datasource you also need to DataBing it.

dataGridView1.DataSource = reader;

then

dataGridView1.DataBind();

 

 

 

Regards, Krishnakant


Monday, June 6, 2011 12:20 PM

Please can you guide me what code, after the  code given below,  I need to give to get data in gridview .

DbConnection oleDBConnection = new OleDbConnection();
            oleDBConnection.ConnectionString = ConfigurationManager.ConnectionStrings["WindowsFormsApplication1.Properties.Settings.employeeConnectionString"].ConnectionString;
            oleDBConnection.Open();

Please can you give what code should be here....

dataGridView1.DataSource = reader;

then

dataGridView1.DataBind();

polachan


Monday, June 6, 2011 12:47 PM

below is the code that should be sufficient for your to bind the data to the grid.

  protected void Page_Load(object sender, EventArgs e)
  {

   using (SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ToString()))
   {
    sqlConnection.Open();
    SqlCommand command = new SqlCommand("Select * from Libraries", sqlConnection);
    SqlDataReader reader = command.ExecuteReader();

    GridView1.DataSource = reader;
    GridView1.DataBind();
   }
  }

<table id="x_GridView1" border="1" cellspacing="0" rules="all" style="border-collapse:collapse"><tbody><tr><th scope="col">LibraryId</th><th scope="col">LibraryName</th><th scope="col">LocationId</th><th scope="col">EstablishmentDate</th><th scope="col">PrimaryPOC</th><th scope="col">SecondaryPOC</th></tr><tr><td>2</td><td>Library
 Wing - A </td><td>1</td><td>3/16/2011 12:00:00 AM</td><td>1</td><td>1</td></tr><tr><td>7</td><td>Library Wing - B </td><td>1</td><td>3/22/2011 6:23:55 PM</td><td>1</td><td>1</td></tr><tr><td>8</td><td>Library Wing - C </td><td>1</td><td>3/22/2011 6:40:35 PM</td><td>1</td><td>1</td></tr><tr><td>10</td><td>Library
 Wing - A </td><td>1</td><td>3/16/2011 12:00:00 AM</td><td>1</td><td>1</td></tr><tr><td>11</td><td>Library Wing - B </td><td>1</td><td>3/22/2011 6:23:55 PM</td><td>1</td><td>1</td></tr><tr><td>12</td><td>Library Wing - C </td><td>1</td><td>3/22/2011 6:40:35
 PM</td><td>1</td><td>1</td></tr></tbody></table>

 This is the result that is display with my sample on my datasource.

Regards, Krishnakant

sorry the table is not displayed properly if you need to see then copy the html in a notepad and save it as a html.


Monday, June 6, 2011 1:00 PM

Sorry i realized later you need it using DBConnection. Here it is

 

    protected void Page_Load(object sender, EventArgs e)
    {

      using (DbConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ToString()))
      {
        conn.Open();

        DbCommand comm = conn.CreateCommand();
        comm.CommandText = "Select * from Libraries";
        DbDataReader reader = comm.ExecuteReader();
        GridView1.DataSource = reader;
        GridView1.DataBind();
      }
    }

Regards, Krishnakant


Monday, June 6, 2011 3:22 PM

Many thanks.

Sorry I forgot to mention about I am using datagridview

 using (DbConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WindowsFormsApplication1.Properties.Settings.employeeConnectionString"].ToString()))
            {
                conn.Open();

                DbCommand comm = conn.CreateCommand();
                comm.CommandText = "Select * from Employee";
                DbDataReader reader = comm.ExecuteReader();
                dataGridView1.DataSource = reader;
                dataGridView1.DataBind();

            }

Here  dataGridView1.DataBind() doesnot work . Please can you guide me with  correct code.

polachan