C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Read Data out of MS Access Database into text box


Posted Date: 01 Nov 2004    Resource Type: Articles    Category: .NET Framework
Author: Pete*Member Level: Gold    
Rating: 1 out of 5Points: 5



VB.NET Code
--------------------------
-AT THE TOP OF THE PAGE, INSERT THE FOLLOWING LINE-

Imports System.Data.OleDb
-----------------------------------------

'MAIN CODE'

Dim Sql As String
Dim Cmd As OleDbCommand
Dim Reader As OleDbDataReader
Dim Con = New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;data source=" & Database & "")

Sql = "SELECT * FROM [tbContacts] WHERE [Account Number]=1234"
Cmd = New OleDbCommand(Sql, Con)
Con.Open()

Reader = Cmd.ExecuteReader(CommandBehavior.CloseConnection)
If Reader.Read() Then
TxtCompany.Text = Reader("Company Name").ToString
TxtName.Text = Reader("Contact Name").ToString
TxtAddress.Text = Reader("Address").ToString
End If
Con.Close()

--------------------------
Quick explanation
--------------------------
First I'm Importing the Namespace that I want to use into the project.
Then I'm creating the MS Access database connection.
Next I'm creating the SQL query to get data out of my 'tbContacts' table in my MS access database.
Finaly I'm opening the connection to the database, executing the the SQL command/query, and then closing the database connection.
The command/query is simply to read the information that relates to Account Number '1234'. When read, the text boxes are populated.
--------------------------
Summary
--------------------------
Yes, I could make this code shorter quite easily but I like my code looking this way for ease of reading.

ENJOY

Pete* ;-) UK
--------------------------




Responses

Author: Scotty    20 Jan 2005Member Level: Bronze   Points : 0
I am so close - yet so far. You have helped me out greatly - however, I keep getting an error in your code:

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll

This happens at:

Reader = Cmd.ExecuteReader(CommandBehavior.CloseConnection)

Any suggestions? Please let me know, I am stuck on this one...


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Create a table in MS Access Database using SQL via VB.NET
Previous Resource: What is Obfuscation?
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use