Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27087

Query a data table

$
0
0
Hello:

I am trying to query a vb.net datatable. In this case, the data table is dt4 and is created like this...

HTML Code:

        ' Look at LUII Specific Data Dump
        ' Get the latest dated excel file for LUII Specific Data!
        Dim ds4 As String = "\\stylmark.com\fs-styl\Home\is\STANDERSON\PSE Analysis\LUII_9-1-12 - 9-10-13_Avante Data.xlsx"

        ' Select query for the (first ds1 History) table
        Dim ss4 As String = "SELECT [SO Nbr], [Product Line], [Config#Rev], [MODEL NUMB], [ENGINEERIN], [STYLE OF B], [MOUNTING], [BACK PANEL], " & _
                                  "[WIRING TYP], [POWER CORD], [SIZE], [VOLTAGE], [SPECIAL WI], [ANO/PAINT], [192] FROM [Sheet1$] " & _
                            "WHERE [ENGINEERIN] = '" & "Y" & "' " & _
                            "AND [Product Line] = '" & "LUII" & "' " & _
                            "ORDER BY [SO Nbr] "       
        ' Create data table (dt4) in memory
        Dim dt4 As New DataTable

        ' Manually add fields (columns)
        dt4.Columns.Add("SO Nbr", GetType([Double]))
        dt4.Columns.Add("Prod Line", GetType([String]))
        dt4.Columns.Add("Config#Rev", GetType([String]))
        dt4.Columns.Add("Model Numb", GetType([Int32]))
        dt4.Columns.Add("Engineerin", GetType([String]))
        dt4.Columns.Add("STYL OF B", GetType([String]))
        dt4.Columns.Add("MOUNTING", GetType([String]))
        dt4.Columns.Add("BACK PANEL", GetType([String]))
        dt4.Columns.Add("WIRING TYP", GetType([String]))
        dt4.Columns.Add("POWER CORD", GetType([String]))
        dt4.Columns.Add("SIZE", GetType([String]))
        dt4.Columns.Add("VOLTAGE", GetType([String]))
        dt4.Columns.Add("SPECIAL WI", GetType([String]))
        dt4.Columns.Add("ANO/PAINT", GetType([String]))
        dt4.Columns.Add("192", GetType([String]))

        ' Connect to Excel spreadsheet and read in data
        dt4 = GetExcelMethod(ds4, ss4)

    Private Function GetExcelMethod(ByVal ds As String, ByVal ss As String) As DataTable
        ' Connects to the Excel document

        ' Create a new datatable (dt here, dt1, dt2 or dt4 in main program)
        Dim dt As New DataTable()

        ' Create OLEDB string
        Dim csbuilder As New OleDbConnectionStringBuilder()

        ' Connect to Excel
        csbuilder.Provider = "Microsoft.ACE.OLEDB.12.0"
        csbuilder.DataSource = ds
        csbuilder.Add("Extended Properties", "Excel 12.0 Xml;HDR=YES")

        ' Open Excel document
        Using connection As New OleDb.OleDbConnection(csbuilder.ConnectionString)
            connection.Open()
            Dim selectSql As String = ss

            Debug.WriteLine(selectSql)

            ' Use adapter to fill the datatable (dt here, dt1 or dt2, dt4 in main program)
            Using adapter As New OleDbDataAdapter(selectSql, connection)

                adapter.Fill(dt)

            End Using
            connection.Close()

        End Using

        ' Return data table to (dt here, dt1, dt2 or dt4 in main program)
        Return dt

    End Function

My goal is to filter on this table to get the data I am after, in the same way perhaps a person would query a SQL table, but the table only exists in memory, manually built off of excel data...

So here's my code, and something just is not right...

HTML Code:

                      Dim expr As String = "SO Nbr = '" & strSONbr2 & "'"
                        Dim foundRows() As DataRow
                        foundRows = dt4.Select(expr)
                        For i As Integer = 0 To foundRows.GetUpperBound(0)
                            Try
                                MessageBox.Show(foundRows(i)(0))

                            Catch ex As Exception

                            End Try

                        Next

The error I get with it is Missing operand after 'Nbr' operator.

Is there a way to just do a SELECT STATEMENT on data tables?

Thanks in advance for the help!

Viewing all articles
Browse latest Browse all 27087

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>