473,383 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

connecting to a Access Database in a Class

Im trying to connect to an Access database using a class. I think I have been
able to establish the connection but when trying to test it by pulling data
from the database im getting the error:

"An unhandled exception of type 'System.NullReferenceException' occurred in
DB.exe

Additional information: Object reference not set to an instance of an object."

The code that I currently have is:
----------------------
Public Class DB
Private myConnection As New OleDb.OleDbConnection
Private myDataAdapter As New OleDb.OleDbDataAdapter
Private dsPeople As Data.DataSet
Private tblPeople As Data.DataTable
Private intPosition As Integer = 0
Sub New(ByVal Path As String)
Dim strSelect As String

Try
myConnection.ConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Path & ";Persist Security
Info=False"
myConnection.Open()
strSelect = ("SELECT FirstName, LastName, HomePhone, WorkPhone,
CellPhone, Email, MailingAddress, City, State, Zip, Comment, ID FROM People")
myDataAdapter = New OleDb.OleDbDataAdapter(strSelect,
myConnection)
Dim intSize As Integer
dsPeople = New DataSet
intSize = myDataAdapter.Fill(dsPeople)
tblPeople = dsPeople.Tables(0)
Catch ex As Exception
End Try
End Sub
Public Sub Assign()
FirstName = dsPeople.Tables(0).Rows(intPosition).Item(0).tostr ing
'(Problem Line)
End Sub
Property FirstName() As String
Get

Return FirstName
End Get
Set(ByVal Value As String)

FirstName = "bob"
End Set
End Property
End Class
---------------------

When I instanciate the object I get the message on the line indicated.

This has me stumped as I'm sure it's a simple problem.

Thanks for any help

Mole

Nov 21 '05 #1
7 1308
=?Utf-8?B?bW9sZW1vb3Jl?= <mo*******@discussions.microsoft.com> wrote in
news:72**********************************@microsof t.com:
Private myConnection As New OleDb.OleDbConnection
Private myDataAdapter As New OleDb.OleDbDataAdapter
Private dsPeople As Data.DataSet
Private tblPeople As Data.DataTable
Private intPosition As Integer = 0
Sub New(ByVal Path As String)
Dim strSelect As String

Try
myConnection.ConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Path & ";Persist
Security Info=False"
myConnection.Open()


You didn't create anew isntance of a connection -

Private myConnection As New OleDb.OleDbConnection = new
OleDB.oleDBConnection

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 21 '05 #2
I tried what you suggested and got the same resulting error message. The code
that I provided works in the Form_Load of another program that I've wrote.
What I'm trying to do is take the code and put it into a class (which I'll
later modify to be used with different databases). What I'm trying to do now
is simply get the same fuctionality as I have with the code in the windows
application.

If the solution you provided is the only thing I have wrong then perhaps I
am implementing it wrong.

Thanks for the timely reply and help

Mole

"Lucas Tam" wrote:
=?Utf-8?B?bW9sZW1vb3Jl?= <mo*******@discussions.microsoft.com> wrote in
news:72**********************************@microsof t.com:
Private myConnection As New OleDb.OleDbConnection
Private myDataAdapter As New OleDb.OleDbDataAdapter
Private dsPeople As Data.DataSet
Private tblPeople As Data.DataTable
Private intPosition As Integer = 0
Sub New(ByVal Path As String)
Dim strSelect As String

Try
myConnection.ConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Path & ";Persist
Security Info=False"
myConnection.Open()


You didn't create anew isntance of a connection -

Private myConnection As New OleDb.OleDbConnection = new
OleDB.oleDBConnection

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 21 '05 #3
Your FirstName Property is not set up correctly. Set the property after the
Datatable is filled: me.FirstName = DT.rows(0).item("FirstName")

Private _FirstName as string
Property FirstName() As String
Get
Return _FirstName
End Get
Set(ByVal Value As String)
_FirstName = Value
End Set
End Property
"molemoore" wrote:
Im trying to connect to an Access database using a class. I think I have been
able to establish the connection but when trying to test it by pulling data
from the database im getting the error:

"An unhandled exception of type 'System.NullReferenceException' occurred in
DB.exe

Additional information: Object reference not set to an instance of an object."

The code that I currently have is:
----------------------
Public Class DB
Private myConnection As New OleDb.OleDbConnection
Private myDataAdapter As New OleDb.OleDbDataAdapter
Private dsPeople As Data.DataSet
Private tblPeople As Data.DataTable
Private intPosition As Integer = 0
Sub New(ByVal Path As String)
Dim strSelect As String

Try
myConnection.ConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Path & ";Persist Security
Info=False"
myConnection.Open()
strSelect = ("SELECT FirstName, LastName, HomePhone, WorkPhone,
CellPhone, Email, MailingAddress, City, State, Zip, Comment, ID FROM People")
myDataAdapter = New OleDb.OleDbDataAdapter(strSelect,
myConnection)
Dim intSize As Integer
dsPeople = New DataSet
intSize = myDataAdapter.Fill(dsPeople)
tblPeople = dsPeople.Tables(0)
Catch ex As Exception
End Try
End Sub
Public Sub Assign()
FirstName = dsPeople.Tables(0).Rows(intPosition).Item(0).tostr ing
'(Problem Line)
End Sub
Property FirstName() As String
Get

Return FirstName
End Get
Set(ByVal Value As String)

FirstName = "bob"
End Set
End Property
End Class
---------------------

When I instanciate the object I get the message on the line indicated.

This has me stumped as I'm sure it's a simple problem.

Thanks for any help

Mole

Nov 21 '05 #4
Ok, I'll give that a shot and see if I can figure it out.

While I've got some attention :) I've got another problem: When I'm trying
to send the data from my dataset back to the original database it's not
updating the database correctly. Again, this is probably a simple problem
that's showing my lack of experience. The code I have is:

dsPeople.Tables(0).Rows(intPosition).EndEdit()
dsPeople.AcceptChanges()
myDataAdapter.Update(dsPeople)

Once again your help is greatly appreciated

Mole

"Charlie" wrote:
Your FirstName Property is not set up correctly. Set the property after the
Datatable is filled: me.FirstName = DT.rows(0).item("FirstName")

Private _FirstName as string
Property FirstName() As String
Get
Return _FirstName
End Get
Set(ByVal Value As String)
_FirstName = Value
End Set
End Property


Nov 21 '05 #5
To update a database, you need an Update statement, an Insert statement, or a
Delete statement. The most simple and straightforward way to handle that is
just to concatenate the statement, using the correct syntax and the values
entered by the user on the form. Then set up an oledb.oledbcommand, using
the statement and your connection as parameters. Make sure you have a "WHERE
PrimaryKey = ..." clause for any Update or Delete statement. Open the
connection. Use the ExecuteNonQuery method of the command object. Close the
connection. Check the database for the update. Use Try blocks to catch
errors.

www.charlesfarriersoftware.com

"molemoore" wrote:
Ok, I'll give that a shot and see if I can figure it out.

While I've got some attention :) I've got another problem: When I'm trying
to send the data from my dataset back to the original database it's not
updating the database correctly. Again, this is probably a simple problem
that's showing my lack of experience. The code I have is:

dsPeople.Tables(0).Rows(intPosition).EndEdit()
dsPeople.AcceptChanges()
myDataAdapter.Update(dsPeople)

Once again your help is greatly appreciated

Mole

"Charlie" wrote:
Your FirstName Property is not set up correctly. Set the property after the
Datatable is filled: me.FirstName = DT.rows(0).item("FirstName")

Private _FirstName as string
Property FirstName() As String
Get
Return _FirstName
End Get
Set(ByVal Value As String)
_FirstName = Value
End Set
End Property

Nov 21 '05 #6
Moolemoore,

Exactly as you wrotie is this showing your lack of experience.

:-)

This was in past the one of the most showed problems in the ADONET
newsgroup.

dsPeople.Tables(0).Rows(intPosition).EndEdit()
dsPeople.AcceptChanges()
What Acceptchanges does is telling that the updates are done and set all the
rowschanges to done and therefore will the next update update nothing.

The Acceptchanges is a part of the datataadaper.update when the original
dataset is used.
myDataAdapter.Update(dsPeople)


I hope this helps?

Cor
Nov 21 '05 #7
=?Utf-8?B?bW9sZW1vb3Jl?= <mo*******@discussions.microsoft.com> wrote in
news:71**********************************@microsof t.com:
I tried what you suggested and got the same resulting error message.


Not to mention, you didn't create new instances of any of your variables.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: John Morgan | last post by:
I have Enterprise Manager on my local machine. For the last twelve months it has been connecting without problem to my online SQL Server database provided by my ISP. Three weeks ago the ISP...
2
by: news | last post by:
We currently have our mySQL server on the same box as the Apache server. For security and load balancing, we're going to be moving the mySQL server to another box. We're already using a single...
3
by: Darren Harvey | last post by:
Hi I'm wanting to connect to an MS-Access MDB file from an OpenRoad program using ActiveX. Can someone tell me the files I need to use/register for Windows XP? Any other help or suggestions...
2
by: Jim | last post by:
I'm writing an Invoicing Windows app but I'm writing it to make the code as easy to maintain as possible. Basically, to get any records from my DB, I use two classes: one that sets up the SQL...
12
by: Ann Marinas | last post by:
Hi all, I would like to ask for some help regarding separating the asp.net webserver and the sql server. I have created an asp.net application for a certain company. Initially, we installed...
7
by: Alex | last post by:
Hi all, I've been writing in ColdFusion for about 6 years now, and now that we've installed a Sharepoint Portal Server I'm finding that I need to use ASP.Net to make database calls. I'm finding...
11
by: CM Manager via DotNetMonster.com | last post by:
I am very frustrated due to this exception error I am receiving. I've tried searching numerous user support groups, Microsoft Support Net, Google, etc. and haven't found exactly my situation....
5
by: Nathan Given | last post by:
Hello All, I've been struggling on and off for 3 months trying to connect to an DB2 database running on an IBM iSeries machine. I FINALLY figured out how to connect with Coldfusion (see...
2
by: Patrick F | last post by:
Hi, i have SQL Server 2005 and a database set that is called, myCompany the problem is that i cant connect from my page to it, here is from the web.config: ( i have got this connectionstring from...
2
by: gmccammon | last post by:
I am working on a class assignment where I have to connect to an MS Access database. The perl script is contained in a separate file *.pl I went into Control panel/ODBC/ and selected Microsoft...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.