1.11M Members

unable to connect

 
0
 

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
 
0
 

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

 
0
 

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

 
0
 

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

 
0
 

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
 
0
 

"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.

 
0
 

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.

 
0
 

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

 
0
 

so did it worK?

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article