0

Good day,

I'm using the OleDbDataAdapter in VB.NET to load query results into a data set.

However, when I step through the code it only loads the columns into the data set and not the rows in the query.

I did do a brief test by exporting that query result into a separate Access DB and then tried loading it and it worked correctly.

I am unsure whether or not the command for Fill supports loading of query results?

This is the code I am using to load up the Access table:

    Dim dsAccess As New DataSet
    Dim da As OleDb.OleDbDataAdapter

    Try
        If conAccess.State = ConnectionState.Open Then
            sql = "SELECT * FROM " & strTblName
            da = New OleDb.OleDbDataAdapter(sql, conAccess)


            da.Fill(dsAccess, strTblName)
        Else
            Return False
        End If
    Catch ex As System.Exception
        Return False
    Finally
        da = Nothing
    End Try

Note, I've edited this question to show the actual table structure of my "query".

enter image description here

That's the query I'm trying to perform the Select on.

The design view of the query:

enter image description here

Hopefully this helps in diagnosing this issue!

Community
  • 1
  • 1
Rahul Kishore
  • 380
  • 1
  • 8
  • 19
  • What you have shown looks ok. Are there rows in that table? Try the FILL() without passing the table name, does this change the result? – Steve Dec 23 '15 at 22:25
  • @Steve no luck. Same results. Just loads up the columns. I've checked and rechecked in Access..there is data in that query set. Has about 2292 records. Could that be an issue? – Rahul Kishore Dec 23 '15 at 22:43
  • I've only ever use DataAdapter.Fill by passing in a DataTable. If you pass a DataTable in does it work? – Sam Axe Dec 24 '15 at 00:23
  • Additionally, I've run across at least one scenario where the .Fill method refused to populate the rows of a DataTable unless I called `adapter.FillSchema(table, SchemaType.Source)` first before calling .Fill. – Sam Axe Dec 24 '15 at 00:25
  • @SamAxe, yeah my first hunch was to try a DataTable. No luck even then. I'll try using the `FillSchema` method and see if rows get populated. – Rahul Kishore Dec 26 '15 at 10:18

2 Answers2

1

It's not a good idea to have a try-catch without checking to see what the error message is e.g. MessageBox.Show(ex.Messasge). Have you tried this or taking the code completely out of the try-catch to see if an exception is thrown?

PLease try the following code with proper modifications to the connection string and table name. Hopefully the data is returned or an exception is thrown.

One more thing, if field names have spaces in them e.g. First Name you might need to do something like SELECT [First Name] ... and lastly if any fields are reserve words like Date you should wrap them in [].

Public Sub SimpleDemo()
    Dim dt As New DataTable
    Dim strTblName As String = "SomeTable"
    Using cn As New OleDb.OleDbConnection With
        {
            .ConnectionString = "Your connection string goes here"
        }
        Using cmd As New OleDb.OleDbCommand With
            {
                .Connection = cn,
                .CommandText = "SELECT * FROM " & strTblName
            }
            cn.Open()
            dt.Load(cmd.ExecuteReader)
        End Using
    End Using

    MessageBox.Show(dt.Rows.Count.ToString)
End Sub
Karen Payne
  • 4,341
  • 2
  • 14
  • 31
  • Hello Karen, thanks for your response. I did try executing the code outside of the try-catch, and I stepped through it. It did not throw any sort of errors. I also checked the `Err.Description` and `Err.Number` both were empty. I'll put your code in with my modifications and report back the results soon. – Rahul Kishore Dec 26 '15 at 10:16
  • Can you post your SQL statement and also don't use Err.Description or Err.Number, instead when using a try-catch use ex.Message. – Karen Payne Dec 26 '15 at 12:27
  • this is the SQL Statement I have `sql = SELECT * FROM EmailAddress` . I have used your code above and still the rows aren't filled into the `DataTable`. Only the columns are being populated. I'm starting to think there may be an issue with _how_ the table is constructed inside of Access. It is essentially a query...so maybe that could be an issue? Also, the code did not throw any exceptions (it worked fine). – Rahul Kishore Dec 28 '15 at 19:45
  • If using MS-Access 2007 or better and VS2013 then try the following code sample https://onedrive.live.com/redir?resid=A3D5A9A9A28080D1!879&authkey=!AKQ2iwYCIDkitkk&ithint=file%2czip. – Karen Payne Dec 28 '15 at 20:25
  • thanks for the project, I stepped through the code and when I replaced your DB with mine, and changed the Query accordingly to load my desired table...still same results (only the columns loaded up). I will however edit my question so you can see the table structure I have. – Rahul Kishore Dec 28 '15 at 21:03
  • I suggest removing the space from the table name and the Type Rep field then retry. – Karen Payne Dec 28 '15 at 22:25
  • the problem seems to be with the way the `LIKE` is implemented. I will amend this and get back to you. Thanks for pointing removing some of those columns out...wouldn't have thought of it. – Rahul Kishore Dec 28 '15 at 22:44
  • Best to look at https://msdn.microsoft.com/en-us/library/bb208897%28v=office.12%29.aspx?f=255&MSPPError=-2147217396 – Karen Payne Dec 28 '15 at 23:32
  • Karen, you can check out my answer below. It worked for me. – Rahul Kishore Dec 29 '15 at 22:33
0

Okay so after much research I finally found a way to load up the table. I'm posting this here so that someone else who runs into this has something to work with!

Anyways what I found out was that when you use Microsoft Access, the OleDbAdapter doesn't like the fact that the Like statement uses the * wildcard character. I found out that when using the OleDbAdapter you have to use the % operator. A simple Replace does the job.

So basically what I did was, I found the actual query behind the View I had. I got the View Definition of it (which is the pesky query that encompasses the Like and from there I just did a simple Replace).

Here's the code (posted this to help everyone else save some time):

                Dim dtMeta As DataTable = conAccess.GetSchema("Tables")

                If drTableType.Length > 0 Then
                    If drTableType(0).Item("TABLE_TYPE") = "VIEW" Then
                        dtMeta = Nothing
                        dtMeta = conAccess.GetSchema("Views")
                        drTable = dtMeta.Select("TABLE_NAME='" & strTblName & "'")
                        If drTable.Length > 0 Then
                            strOriginalQuery = drTable(0).Item("VIEW_DEFINITION").ToString()
                            strModifiedQuery = strOriginalQuery.Replace("*", "%")

                            Dim myCommand As OleDbCommand = New OleDbCommand()
                            myCommand.CommandText = strModifiedQuery
                            da = New OleDbDataAdapter(myCommand.CommandText, conAccess)
                            da.Fill(dsAccess, strTblName)
                        End If
                    Else
                        da.Fill(dsAccess, strTblName)
                    End If
                End If

The GetSchema method was employed to get the name of the tables out, and then further down I employed the method again to retrieve the Views collection, and from here I was able to get the ViewDefinition which had the actual query behind.

Rahul Kishore
  • 380
  • 1
  • 8
  • 19
  • 1
    Did not see that it was a view from the code, if I had I would had directed you to a MSDN code example I wrote here https://code.msdn.microsoft.com/Execute-SQL-statements-aa3015b5 – Karen Payne Dec 29 '15 at 22:36