Problem with DataAdapter Fill method, Ithink

  • Thread starter Thread starter Joyce
  • Start date Start date
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
 
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
 
Joyce,

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

Kerry Moorman
 
Chris, (is top posting ok here?)

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

--Joyce Perry
 
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
 
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
 
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
 
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
 
Back
Top