Problem with DataAdapter Fill method, Ithink

J

Joyce

Hi folks,

I am trying to run a simple program to test my data connection. The
database opens successfully but throws an exception at the
"objDataAdapter.Fill(objDataTabel)" line below. Am I doing something
thats obviously wrong here?

Thanks,
--Joyce

'---------------------------
Imports System.Data
Imports System.Data.Common
Imports System.Data.OleDb

Public Class Form1
Dim strConnection As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source= C:\Documents and Settings\Trilogy\" & _
"My Documents\Visual Studio 2005\Projects\dbBackStock.mdb"

Dim objConnection As New OleDbConnection(strConnection)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs)

Handles MyBase.Load


objConnection.Open()

If objConnection.State = ConnectionState.Open Then
Label1.Text = "Database is Open"
Else
Label1.Text = "Database is Closed"
End If
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As

System.EventArgs) Handles Button1.Click
Dim strSQL As String = "SELECT Description, Size FROM
tblProducts"
Dim objCommand As New OleDbCommand(strSQL, objConnection)
Dim objDataAdapter As New OleDbDataAdapter(objCommand)
Dim objDataTabel As New Data.DataTable("Products")
Dim objDataRow As DataRow

objDataAdapter.Fill(objDataTabel) '<----- Exception here


For Each objDataRow In objDataTabel.Rows
ListBox1.Items.Add(objDataRow.Item("Description") & " " &
_
objDataRow.Item("Size"))
Next

End Sub
End Class
 
G

Guest

Joyce said:
Hi folks,

I am trying to run a simple program to test my data connection. The
database opens successfully but throws an exception at the
"objDataAdapter.Fill(objDataTabel)" line below. Am I doing something
thats obviously wrong here?

Thanks,
--Joyce

'---------------------------
Imports System.Data
Imports System.Data.Common
Imports System.Data.OleDb

Public Class Form1
Dim strConnection As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source= C:\Documents and Settings\Trilogy\" & _
"My Documents\Visual Studio 2005\Projects\dbBackStock.mdb"

Dim objConnection As New OleDbConnection(strConnection)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs)

Handles MyBase.Load


objConnection.Open()

If objConnection.State = ConnectionState.Open Then
Label1.Text = "Database is Open"
Else
Label1.Text = "Database is Closed"
End If
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As

System.EventArgs) Handles Button1.Click
Dim strSQL As String = "SELECT Description, Size FROM
tblProducts"
Dim objCommand As New OleDbCommand(strSQL, objConnection)
Dim objDataAdapter As New OleDbDataAdapter(objCommand)
Dim objDataTabel As New Data.DataTable("Products")
Dim objDataRow As DataRow

objDataAdapter.Fill(objDataTabel) '<----- Exception here


For Each objDataRow In objDataTabel.Rows
ListBox1.Items.Add(objDataRow.Item("Description") & " " &
_
objDataRow.Item("Size"))
Next

End Sub
End Class

It'd be nice to know what your error is...

Chris
 
G

Guest

Joyce,

The dataadapter's Fill method requires a dataset as the argument, not a
datatable.

Kerry Moorman
 
J

Joyce

Chris, (is top posting ok here?)

The error I receive is "IErrorInfo.GetDescription failed with
E_FAIL(0x80004005)"

--Joyce Perry
 
J

Joyce

Kerry,

Ok I read some of the MSDN2 stuff and it appears you can use Fill with
DataTable but I am a newbie. (I used to humm with vb6 but that was 5
years ago)

How would you change my program so that the query results would show
up in the ListBox? Any help is gratefully appreciated as I've been
stuck here for days!

--Joyce Perry
 
G

Guest

Joyce,

I was wrong about the dataadapter's Fill method requiring a dataset. It will
also take a datatable, as you are doing in your code.

I don't see anything obviously wrong with your code. I wonder if either
Description or Size may be reserved words and need to be enclosed in square
brackets in your Select statement: Select [Description], [Size] From ...

Kerry Moorman
 
C

Cor Ligthert [MVP]

Joyce,

Your code is not the nicest, I would at least remove the code in that load
event completely.

However to see the error you can put first this in your code.
Dim objDataRow As DataRow
Try
objDataAdapter.Fill(objDataTabel) '<----- Exception here

Catch ex as Exception

messagebox.show(ex.toString)
End Try


Just to find what the error really is.

Cor
 
J

Joyce

On Mon, 16 Jan 2006 18:28:01 -0800, "Kerry Moorman"

Kerry! That was it!

I square-bracketted [Size] and it worked!!!!!!

Thank you SO much.

--Joyce
 

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