Connecting Access DB to VB .net DataGrid

S

Sebastian

I'm trying to connect a query I made in the Access
database to populate a grid.I'm using the following code
and I get a error:

Specified cast is not valid.

on the first line below. Any Ideas?


Dim DataAdp As OleDbDataAdapter = New OleDbDataAdapter
("GetInvoices", DBvar)
DataAdp.SelectCommand.CommandType =
CommandType.StoredProcedure
If Not IsDBNull(DataAdp) Then

Try
DataAdp.Fill(ds, "Invoices")
Catch ex As Exception
MsgBox(ex.Message,
MsgBoxStyle.Critical, "Error:")
End Try
GrdInvoices.DataSource = dataset.Tables
("Invoices")
End If

ts.MappingName = dataset.Tables
("Invoices").TableName
 
A

Armin Zingler

Sebastian said:
I'm trying to connect a query I made in the Access
database to populate a grid.I'm using the following code
and I get a error:

Specified cast is not valid.

on the first line below. Any Ideas?


Dim DataAdp As OleDbDataAdapter = New OleDbDataAdapter
("GetInvoices", DBvar)

I've no clue, but.. what is the type of DBVar? It must be a string or an
System.Data.OleDb.OleDbConnection.
DataAdp.SelectCommand.CommandType =
CommandType.StoredProcedure
If Not IsDBNull(DataAdp) Then

Using IsDBNull on a DataAdapter doesn't make sense at all. A DataAdapter is
an object reading and storing records from and into a database.

Try
DataAdp.Fill(ds, "Invoices")
Catch ex As Exception
MsgBox(ex.Message,
MsgBoxStyle.Critical, "Error:")
End Try
GrdInvoices.DataSource = dataset.Tables
("Invoices")

You are reading into "ds", but attaching "dataset"(.tables)?
End If

ts.MappingName = dataset.Tables
("Invoices").TableName


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
S

Sebastian

Actually I'm using ds everywere but I tryed to change it
here to better describe what I was doing (I missed that
last one). However I am useing a variable ds everywhere
which is a type dataset.

DBVar is global connection to the access database.Its used
as follows.
Public DBvar As New ADODB.Connection()
DBvar.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=PalData.mdb")

Is the connection the problem? How do I need to change it
to get it to work with datagrids? Do I need any special
references?

Thanks,
 
C

Cor Ligthert

Hi Sebastian,

The simplest way to test this is first remove that SP.
Test it with a normal SQL txt string, when it than it working it is easy to
change that again for an SP.

The same as Armin wrote do I think that that DBnul dataadapter is the error

What has a DBnull.value to do with a dataadapter?

The commandpart can have no reference, however that is not a DBnull.value
but a Nothing (while the dataadapter is Something).

Cor
 
A

Armin Zingler

Sebastian said:
Actually I'm using ds everywere but I tryed to change it
here to better describe what I was doing (I missed that
last one). However I am useing a variable ds everywhere
which is a type dataset.

DBVar is global connection to the access database.Its used
as follows.
Public DBvar As New ADODB.Connection()
DBvar.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=PalData.mdb")

Is the connection the problem? How do I need to change it
to get it to work with datagrids? Do I need any special
references?

You are mixing two (totally) different technologies: ADO and ADO.Net. You
are establishing an ADO connection, but you need an ADO.Net connection
(System.Data.OleDb.OleDbConnection).


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
C

Cor Ligthert

Hi Sebastian,

Armin saw that better than I however to fullfil your question

dim DBVar as new
OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=\somepath\mydb.mdb;User Id=admin;Password=;" )

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top