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 --------------------------
|
| Author: Scotty 20 Jan 2005 | Member 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...
|