By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,471 Members | 722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,471 IT Pros & Developers. It's quick & easy.

retrieving data from access into vb.net application

P: 6
Hi All, Im working on a project and need to retreive data from my access database and display it on my vb.net windows application can anyone help me? i have tried this code but does not seem to work?
Expand|Select|Wrap|Line Numbers
  1. Imports System.Data.OleDb
  2. Public Class Form1 Inherits System.Windows.Forms.Form
  3. Dim cn As OleDbConnection
  4. Dim cmd As OleDbCommand
  5. Dim dr As OleDbDataReader
  6. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e as _
  7. System.EventArgs) Handles MyBase.Load
  8. End Sub
  9.  
  10. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
  11. System.EventArgs) Handles Button1.Click
  12. Try
  13. cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;_
  14. Data Source=C:\emp.mdb;")
  15.  
  16. cn.Open()
  17. cmd = New OleDbCommand("select * from table1", cn)
  18. dr = cmd.ExecuteReader
  19. While dr.Read()
  20. TextBox1.Text = dr(0)
  21. TextBox2.Text = dr(1)
  22. TextBox3.Text = dr(2)
  23.  
  24. End While
  25. Catch
  26. End Try
  27. dr.Close()
  28. cn.Close()
  29. End Sub
  30. End Class
  31.  
Mar 19 '07 #1
Share this Question
Share on Google+
2 Replies


P: 6
Hi All just thought id let you know i managed to get the code to retreive information for a access database and display it on a datagrid using a button:


(make sure you change the database location and table name)

Imports System.Data.OleDb
Imports System.Data

'then writs this as private:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  2. Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\db1.mdb"
  3. Dim myConnection As OleDbConnection = New OleDbConnection
  4. myConnection.ConnectionString = connString
  5. ' create a data adapter 
  6. Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select * from st_timetable", myConnection)
  7.  
  8. Dim ds As DataSet = New DataSet
  9. da.Fill(ds, "st_timetable")
  10. DataGridView1.DataSource = ds.DefaultViewManager
  11. End Sub
  12.  
Mar 20 '07 #2

MMcCarthy
Expert Mod 10K+
P: 14,534
Great work and thanks for posting the solution.

Mary
Mar 22 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.