1,105,208 Community Members

unable to connect

Member Avatar
daletonge
Newbie Poster
7 posts since Nov 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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
Member Avatar
ShawnCplus
Code Monkey
1,564 posts since Apr 2005
Reputation Points: 456 [?]
Q&As Helped to Solve: 269 [?]
Skill Endorsements: 39 [?]
Team Colleague
 
0
 

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

Member Avatar
Proteus
Newbie Poster
5 posts since Jul 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
daletonge
Newbie Poster
7 posts since Nov 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
daletonge
Newbie Poster
7 posts since Nov 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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
Member Avatar
safalmittal
Newbie Poster
6 posts since May 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
daletonge
Newbie Poster
7 posts since Nov 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
saedjomaa
Newbie Poster
1 post since May 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
arjunsasidharan
Practically a Posting Shark
812 posts since Aug 2006
Reputation Points: 297 [?]
Q&As Helped to Solve: 20 [?]
Skill Endorsements: 0 [?]
 
0
 

so did it worK?

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