locked
Connect with access RRS feed

  • Question

  • Hi all!

    I'm new in vb programming.

    So, i need just 1 example to how to connect a asp.net net web form site to access db(accdb) 

    Supposing i have 1 table in db called client, 

    if i create a textfield, a button to submit, and a button to show records, how to connect, to do this?

    Thanks for help.

    I'm using vs 2012 ultimate and access 2010

    Monday, October 27, 2014 5:05 PM

Answers

  • Dim Table_ As String = "Table1"
    Dim query As String = "SELECT * FROM " & Table_
    Dim MDBConnString_ As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=TestDatabase.mdb;"
    Dim ds As New DataSet
    Dim cnn As OleDbConnection = New OleDbConnection(MDBConnString_)
    cnn.Open()
    Dim cmd As New OleDbCommand(query, cnn)
    Dim da As New OleDbDataAdapter(cmd)
    da.Fill(ds, Table_)
    cnn.Close()
    Dim t1 As DataTable = ds.Tables(Table_)
    
    DataGridView1.DataSource = t1

    • Proposed as answer by Deric Ferreira Monday, October 27, 2014 6:34 PM
    • Marked as answer by BSBAwq Friday, October 31, 2014 1:02 PM
    Monday, October 27, 2014 6:34 PM
    • Proposed as answer by Deric Ferreira Monday, October 27, 2014 6:34 PM
    • Marked as answer by BSBAwq Friday, October 31, 2014 1:02 PM
    Monday, October 27, 2014 6:04 PM
  • Sadly, these links didn't open here in my job. Ill take a look when i'm in home.

    So, i did try you code and get the folowing erros:

    Error 1 Type 'OleDbConnection' is not defined.
    Error 2 Type 'OleDbCommand' is not defined.
    Error 3 Type 'OleDbDataAdapter' is not defined.

    ______________________

    I clicked on the errors link and solved this problem with "Imports System.Data.OleDb"

    Now, the problem is:

    My database is accdb, and when i run the aplication it says that this format is unrecognized(or not recognized, it's in portuguese).

    How i handle this?

    • Edited by BSBAwq Tuesday, October 28, 2014 12:04 PM
    • Marked as answer by BSBAwq Thursday, October 30, 2014 11:46 AM
    Tuesday, October 28, 2014 11:41 AM
  • Can't use the Jet OLEDB Provider for .accdb files. You need to use ACE:

    http://www.connectionstrings.com/access/


    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Marked as answer by BSBAwq Friday, October 31, 2014 1:02 PM
    Tuesday, October 28, 2014 12:19 PM

All replies

    • Proposed as answer by Deric Ferreira Monday, October 27, 2014 6:34 PM
    • Marked as answer by BSBAwq Friday, October 31, 2014 1:02 PM
    Monday, October 27, 2014 6:04 PM
  • Dim Table_ As String = "Table1"
    Dim query As String = "SELECT * FROM " & Table_
    Dim MDBConnString_ As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=TestDatabase.mdb;"
    Dim ds As New DataSet
    Dim cnn As OleDbConnection = New OleDbConnection(MDBConnString_)
    cnn.Open()
    Dim cmd As New OleDbCommand(query, cnn)
    Dim da As New OleDbDataAdapter(cmd)
    da.Fill(ds, Table_)
    cnn.Close()
    Dim t1 As DataTable = ds.Tables(Table_)
    
    DataGridView1.DataSource = t1

    • Proposed as answer by Deric Ferreira Monday, October 27, 2014 6:34 PM
    • Marked as answer by BSBAwq Friday, October 31, 2014 1:02 PM
    Monday, October 27, 2014 6:34 PM
  • Sadly, these links didn't open here in my job. Ill take a look when i'm in home.

    So, i did try you code and get the folowing erros:

    Error 1 Type 'OleDbConnection' is not defined.
    Error 2 Type 'OleDbCommand' is not defined.
    Error 3 Type 'OleDbDataAdapter' is not defined.

    ______________________

    I clicked on the errors link and solved this problem with "Imports System.Data.OleDb"

    Now, the problem is:

    My database is accdb, and when i run the aplication it says that this format is unrecognized(or not recognized, it's in portuguese).

    How i handle this?

    • Edited by BSBAwq Tuesday, October 28, 2014 12:04 PM
    • Marked as answer by BSBAwq Thursday, October 30, 2014 11:46 AM
    Tuesday, October 28, 2014 11:41 AM
  • Can't use the Jet OLEDB Provider for .accdb files. You need to use ACE:

    http://www.connectionstrings.com/access/


    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Marked as answer by BSBAwq Friday, October 31, 2014 1:02 PM
    Tuesday, October 28, 2014 12:19 PM
  • Just fix your ConnectionString, for eg.:

    Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;
    Jet OLEDB:Database Password
    =MyDbPassword;

    References: https://www.connectionstrings.com/access/

    • Proposed as answer by Deric Ferreira Tuesday, October 28, 2014 12:33 PM
    Tuesday, October 28, 2014 12:33 PM
  • I create a db in mdb extension, now it's works. Now, im with problems to show the data, because i don't have "DataGridView" just have "Gridview" in toolbox.

    Error 1 Value of type 'System.Data.DataTable' cannot be converted to 'System.Web.UI.WebControls.Gridview'

    Tuesday, October 28, 2014 12:43 PM
  • There is no DataGridView in ASP.NET. See the documentation below for the GridView:

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basedataboundcontrol.datasource(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-3

    The appropriate forum for data access with ASP.NET would be the below link:

    http://forums.asp.net/24.aspx/1?Web+Forms+Data+Controls

    or for Microsoft Access:

    http://forums.asp.net/55.aspx/1?Access+Databases+and+AccessDataSource+Control


    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Proposed as answer by Deric Ferreira Tuesday, October 28, 2014 4:09 PM
    • Unproposed as answer by Deric Ferreira Thursday, October 30, 2014 12:16 PM
    Tuesday, October 28, 2014 4:06 PM
  • Please, do not forget to mark the answers if were usefull.
    Tuesday, October 28, 2014 4:10 PM
  • Dim Table_ As String = "Table1"
    Dim query As String = "SELECT * FROM " & Table_
    Dim MDBConnString_ As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=TestDatabase.mdb;"
    Dim ds As New DataSet
    Dim cnn As OleDbConnection = New OleDbConnection(MDBConnString_)
    cnn.Open()
    Dim cmd As New OleDbCommand(query, cnn)
    Dim da As New OleDbDataAdapter(cmd)
    da.Fill(ds, Table_)
    cnn.Close()
    Dim t1 As DataTable = ds.Tables(Table_)
    BSBAwq, the code above is just an example, then I removed the line was causing your problem because it was just illustrative (to load datatable into gridview). All your questions were answered. Why don't you mark them, please? It helps other users in the future!

    Thursday, October 30, 2014 12:18 PM
  • Sure, man.

    It was really useful.

    I have done diferent, like this:

     <div runat="server" id="Div1" />
          <br />
          <asp:ListBox ID="ListBox1" runat="server" DataSourceID="SqlDataSource2" DataTextField="DES" DataValueField="DES"></asp:ListBox>
          <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" ProviderName="<%$ ConnectionStrings:DBConnectionString.ProviderName %>" SelectCommand="SELECT [DES] FROM [RESPOSTAS]"></asp:SqlDataSource>
    
    but that way i can't insert or delete files, i'm working on this now.
    Friday, October 31, 2014 1:05 PM
  • Dim Table_ As String = "Table1"
    Dim query As String = "SELECT * FROM " & Table_
    Dim MDBConnString_ As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=TestDatabase.mdb;"
    Dim ds As New DataSet
    Dim cnn As OleDbConnection = New OleDbConnection(MDBConnString_)
    cnn.Open()
    Dim cmd As New OleDbCommand(query, cnn)
    Dim da As New OleDbDataAdapter(cmd)
    da.Fill(ds, Table_)
    cnn.Close()
    Dim t1 As DataTable = ds.Tables(Table_)
    BSBAwq, the code above is just an example, then I removed the line was causing your problem because it was just illustrative (to load datatable into gridview). All your questions were answered. Why don't you mark them, please? It helps other users in the future!

    That way the result is only table name, don't show the records.
    Friday, October 31, 2014 1:06 PM