Hey all

I have a mdb file. I need to use this file as a datasource to display in the gridview.
Can anyone suggest me how to use a LINQ to add to that gridview.
here the purpose of gridview is simply to display some rows.......


Regards,
Prasanth.

Dani AI

Generated

A quick reality check and a practical approach.

s suggestion to use DataContext with an Access connection string is misleading: DataContext (LINQ to SQL) targets SQL Server and is not a drop‑in for Jet/ACE. For an Access (.mdb/.accdb) file the simplest, reliable pattern is to read the file via System.Data.OleDb into a DataTable, bind that DataTable to the GridView for display, and — only if query/projection is needed — use LINQ to DataSet (DataTable.AsEnumerable()) to filter or shape rows before binding. Use using blocks to dispose connections and check for empty results before calling CopyToDataTable().

Example C# pattern (Page_Load):

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        var path = Server.MapPath("~/App_Data/mydb.mdb");
        var cs = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Persist Security Info=False;";
        using (var conn = new OleDbConnection(cs))
        using (var cmd = new OleDbCommand("SELECT * FROM MyTable", conn))
        using (var da = new OleDbDataAdapter(cmd))
        {
            var dt = new DataTable();
            da.Fill(dt);

            // optional LINQ
            var q = dt.AsEnumerable()
                      .Where(r => r.Field<string>("Status") == "Active");

            GridView1.DataSource = q.Any() ? q.CopyToDataTable() : dt;
            GridView1.DataBind();
        }
    }
}

Important notes:

  • Use Microsoft.ACE.OLEDB.12.0 for newer systems; Jet.OLEDB.4.0 is 32‑bit only. Set the app to x86 or install the ACE redistributable if running 64‑bit. See the LINQ to SQL overview and DataTable LINQ docs for details: and DataTableExtensions.AsEnumerable.
  • For simple display, binding the DataTable directly is fastest. For larger datasets use server-side filtering in the SQL (SELECT ... WHERE ...) to avoid loading everything into memory.
  • ’s LinqDataSource approach is usable only when the data source exposed to the control is an IEnumerable/data context that actually provides the records; for Access that usually means an intermediate method that returns a DataTable/IEnumerable<DataRow> or a custom data access class.

Recommended Answers

All 5 Replies

define DataContext with connection string , write a queyr and bind result with gridview like below :

DataContext dc = new DataContext("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Microsoft Access Database\\Microsoft Data Access\\test1.mdb");

Var data = from t in dc.your table name
           Select t;
Gridview1.DataSource = data;
Gridview1.DataBind();

Hey all

I have a mdb file. I need to use this file as a datasource to display in the gridview.
Can anyone suggest me how to use a LINQ to add to that gridview.
here the purpose of gridview is simply to display some rows.......


Regards,
Prasanth.

Hey Rohand,
thanks for ur reply..
can u send me the code in C# pls...

regards,
Prasanth.


define DataContext with connection string , write a queyr and bind result with gridview like below :

DataContext dc = new DataContext("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Microsoft Access Database\\Microsoft Data Access\\test1.mdb");

Var data = from t in dc.your table name
           Select t;
Gridview1.DataSource = data;
Gridview1.DataBind();

always welcome... this is the C# code.. you can use it..

Hey Rohand,
thanks for ur reply..
can u send me the code in C# pls...

regards,
Prasanth.

To store the data in linq using this syntex

var database =
        from colomn in tables
        where condition
        select condition;

then
Bind the data using Linq Data Source.

protected void Page_Load(object sender, EventArgs e)
   	{
        GridView1.Visible = true;
        if (!IsPostBack)
        {
            GridView1.DataSource = LinqDataSource1;
            GridView1.DataBind();
        }
    	}

Hey ciint
thnk u soo much..
i got my problem solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.