My program will not build due to an error related to the section of code below shown inbetween the following symbol.


The error which i recieve is the following:
Object reference not set to an instance of an object.

any help would be appreciated, thanks again

Imports System.Data

Public Class Navigation
Dim inc As Integer
Dim maxrows As Integer
Dim con As OleDb.OleDbConnection
Dim ds As DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String


Private Sub Navigation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


~~~~~~~~~~~~~~~~~~~~~~~~

con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=""C:\AddressBook.mdb"";"

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

con.Open()
MsgBox("A Connection to the Database is now open")
sql = "SELECT * FROM tblContacts"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "AddressBook")

con.Close()
MsgBox("The Connection to the Database is now Closed")
maxrows = ds.Tables("Address Book").Rows.Count
MsgBox("Rows.count")
inc = -1


End Sub
End Class

Recommended Answers

All 8 Replies

Do you have your Data Adapter and Data Connection configured correctly?

Try to write your connection string without the inner "" tha u have. They are not needed. So It would be like that:

"Provider=Microsoft.Jet.OLEDB.4.0;Password=;User ID=Admin;Data Source=C:\AddressBook.mdb;"

Also you have an extra whitespace in the line

maxrows = ds.Tables("Address Book").Rows.Count

It might be a copy - paste flaw from code editor to here but check it anyway

Hope i helped

Thanks my friend, have tried both but still recieve the same error:

Object reference not set to an instance of an object.

I dont understand as i have the same code working on another form but only once a button is pressed

Try to write your connection string without the inner "" tha u have. They are not needed. So It would be like that:

"Provider=Microsoft.Jet.OLEDB.4.0;Password=;User ID=Admin;Data Source=C:\AddressBook.mdb;"

Also you have an extra whitespace in the line

maxrows = ds.Tables("Address Book").Rows.Count

It might be a copy - paste flaw from code editor to here but check it anyway

Hope i helped

Do you have your Data Adapter and Data Connection configured correctly?

I thought it was configured using this line of code

Public Class Navigation
Dim inc As Integer
Dim maxrows As Integer
Dim con As OleDb.OleDbConnection
Dim ds As DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String

"Object reference not set to an instance of an object."
The error is raised when we assume without confirming in code that something exists,like newrow=row(1)
if row(1) was not there in the database the above error would be raised.
u will have to check at each step that data u r trying to access is there in the first place.
i.e. "SELECT * FROM tblContacts" would raise that error if tblContacts was not there in the data source in the specified connection string.similarly for other objects u are using.
it would be better if u put the whole code in the "try, catch" block and study ,what the exception is.

Hi i am relatively new to VB.net, infact very new, i have checked in my table that tblContacts exist and this is the case. I have another form where by i click a button and run almost identical code but with no problems. I really done understand, please give me some help in more detail.

Thanks


"Object reference not set to an instance of an object."
The error is raised when we assume without confirming in code that something exists,like newrow=row(1)
if row(1) was not there in the database the above error would be raised.
u will have to check at each step that data u r trying to access is there in the first place.
i.e. "SELECT * FROM tblContacts" would raise that error if tblContacts was not there in the data source in the specified connection string.similarly for other objects u are using.
it would be better if u put the whole code in the "try, catch" block and study ,what the exception is.

the "Object reference not set to an instance of an object" Message Raise when you trying to give a value to an object not created correctely so I think the dataSet should declare like this:
dim ds as new DataSet
try it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.