Wrox Programmer Forums
|
BOOK: Visual Basic 2010 Programmer's Reference
This is the forum to discuss the Wrox book Visual Basic 2010 Programmer's Reference by Rod Stephens; ISBN: 9780470499832
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Visual Basic 2010 Programmer's Reference section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 26th, 2011, 03:16 PM
Registered User
 
Join Date: Nov 2011
Posts: 9
Thanks: 2
Thanked 0 Times in 0 Posts
Default DataRelations within Dataviews

I have two tables in a dataset. On table contains Sales Orders and how long they were at a production station. The second table contains an entry for each possible prodcution station and the exceptable time for each part to reside at said station. The two tables have a column name 'StatusCode' and a relation has been established. This is a many to one relation.

I would like to use a Dataview on the repair order table to show any repair orders that have exceeded the time allotment found in the child table. AeroPartsDataSet.Tables("StationTimeTable")

I attempted this by using the dataview.RowFilter (see code below)


Code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        vOrderStatus = AeroPartsDataSet.Tables("SalesOrderTable").DefaultView
        vOrderStatus.Sort = "PartNumber ASC"
       vOrderStatus.RowFilter = "[Days At Station] > 80"
        grvExcelData.DataSource = vOrderStatus 'AeroPartsDataSet.Tables("SalesOrderTable") 'bind Datasoure to Datagridview
        grvExcelData.Update()

End Sub
This code works to show a repair at a stations over 80 days. However I have not been able to figure out how to replace the "80" with the value in the child table, containing the exceptable time.

The child table is called "AeroPartsDataSet.Tables("StationTimeTable")"
The coulmn holding the exceptable time is "Time"


Thanks in advance for any help
 
Old November 26th, 2011, 04:04 PM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

I would probably change the data source to be the result of a query. You should be able to use something like:

Code:
SELECT * FROM SalesOrderTable, StationTimeTable WHERE
SalesOrderTable.StatusCode = StationTimeTable.StatusCode AND
SalesOrderTable.[Days At Station] > StationTimeTable.Time
Or something along those lines. The DataView won't automatically link between tables because the result isn't a table but I usually find this sort of query easier to understand.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
The Following User Says Thank You to Rod Stephens For This Useful Post:
RickBritt (November 26th, 2011)
 
Old November 26th, 2011, 07:57 PM
Registered User
 
Join Date: Nov 2011
Posts: 9
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Mr Stephens;
Thanks for the reply, but as a newbie learning on the fly I am at a loss for how to use the suggested select command to copy data from the current data sets to new data set.
The code below was used to test the creation of the table and it's ability to hold data and bind.
I used the "Select * from ......." in ADO file reading of a spreadsheet(s) to create the current dataset/tables but I am at a loss for how to use it to create new table from a dataset.

Code:
   Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Dim TblOrderStatus As DataTable = New DataTable("TblOrderSatus")
       
        TblOrderStatus = AeroPartsDataSet.Tables("SalesOrderTable").Copy
        

        grvExcelData.DataSource = TblOrderStatus 'AeroPartsDataSet.Tables("SalesOrderTable") 'bind Datasoure to Datagridview
        grvExcelData.Update()

    End Sub
End Class
Banging head on wall - wall starting to complain
 
Old November 27th, 2011, 11:13 AM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Take a look at this example:
How to: Bind Data to the Windows Forms DataGridView Control

It shows how to execute a query and display the results in a DataGridView.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
The Following User Says Thank You to Rod Stephens For This Useful Post:
RickBritt (November 27th, 2011)
 
Old November 27th, 2011, 02:46 PM
Registered User
 
Join Date: Nov 2011
Posts: 9
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Rod;
Thanks for the link. This is similar to code to load the spreadsheet

Code:
 Private Sub sReadSpreadSheet(ByVal TxtFilePath)

        TxtFilePath = Strings.LCase(TxtFilePath)

        If (Strings.Right(TxtFilePath, 3) = "xls" Or Strings.Right(TxtFilePath, 4) = "xlsx") Then

            Dim conn As OleDbConnection
            Dim cmd As OleDbCommand
            Dim da As OleDbDataAdapter
            Dim query As String
            Dim connString As String = ""
            ''Connection String to Excel Workbook
            If Strings.Right(TxtFilePath, 3) = "xls" Then
                connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & TxtFilePath & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=2"""
            Else
                connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & TxtFilePath & ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=2"""
            End If
            query = "SELECT [SalesOrderNumber],[ItemNumber],[PartNumber],[StatusCode],[Transdate],[DateClosedyymmdd]  FROM [StationData$] ORDER BY [SalesOrderNumber], [PartNumber]"
            'Create the connection object
            conn = New OleDbConnection(connString)
            'Open connection
            cmd = New OleDbCommand(query, conn)
            da = New OleDbDataAdapter(cmd)
            AeroPartsDataSet = New DataSet()
            Dim SalesOrderTable As DataTable = AeroPartsDataSet.Tables.Add("SalesOrderTable")
            'Dim ordersTable As DataTable = customerOrders.Tables.Add("Orders")
            If conn.State = ConnectionState.Closed Then
                Try
                    conn.Open()
                    Try
                        da.Fill(SalesOrderTable)
                        da.Dispose() 'close data adapter
                        conn.Close() 'close connect to Spreadsheet
                        conn.Dispose() 'do cleanup of connection enviroment
                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Critical)
                    End Try
                Catch ex As Exception
                    MsgBox(ex.Message, MsgBoxStyle.Critical)
                End Try

            End If
            'Create the command object
            query = "SELECT [Time],[Position Code],[DESCRIPTION]  FROM [StatusCodes$]"

            'Create the connection object
            conn = New OleDbConnection(connString)
            'Open connection
            cmd = New OleDbCommand(query, conn)
            da = New OleDbDataAdapter(cmd)
            Dim StationTimeTable As DataTable = AeroPartsDataSet.Tables.Add("StationTimeTable")
            If conn.State = ConnectionState.Closed Then
                Try
                    conn.Open()
                    Try
                        da.Fill(StationTimeTable)
                        da.Dispose() 'close data adapter
                        conn.Close() 'close connect to Spreadsheet
                        conn.Dispose() 'do cleanup of connection enviroment
                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Critical)
                    End Try
                Catch ex As Exception
                    MsgBox(ex.Message, MsgBoxStyle.Critical)
                End Try
            End If
            Dim keys(1) As DataColumn
            keys(0) = StationTimeTable.Columns("StatusCode")
            '' Set the PrimaryKeys property to the array.
            StationTimeTable.PrimaryKey = keys

            'Set Relation Between SalesOrderTable and StationTimeTable
            Dim obj_ParentClmn, obj_ChildClmn As DataColumn

            'Get the reference of columns to create a relation between.
            obj_ParentClmn = AeroPartsDataSet.Tables("SalesOrderTable").Columns("StatusCode")
            obj_ChildClmn = AeroPartsDataSet.Tables("StationTimeTable").Columns("Position Code")

            'Creates a relation object, Parameters required are
            'New Relation Name, Object of Parent & Child column respectively.
            Dim AeroPartsRelation = New DataRelation("OrdetsToTimeRelation", obj_ChildClmn, obj_ParentClmn, False)

            'Adding Relation to the dataset that holds the tables.
            AeroPartsDataSet.Relations.Add(AeroPartsRelation)

        Else
            MsgBox("Please select an excel file first", , "No File Found")
        End If

    End Sub
In the link they use a connection string. I have been unable to find a sample of a connection string that connects to datatables in memory.

Even though I still want to know the answer to the lead question for the thread. In an effort to complete the task I have tried a work around where I loop through "SalesOrderTable" checking one record at a time to seek if the [Time At Station] in the row exceeds the [StationTimeTable.Columns("StatusCode")] standard.

Code:
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Dim TblOrderStatus As DataTable = New DataTable("TblOrderSatus")
        TblOrderStatus = AeroPartsDataSet.Tables("SalesOrderTable").Clone
        Dim RowCount As Integer = AeroPartsDataSet.Tables("SalesOrderTable").Rows.Count
        Dim nCurrentRec As Integer = 0 'first record in datatable
        Dim s As String
        Dim f As Decimal
        Dim foundRow As DataRow

        For Each row As DataRow In AeroPartsDataSet.Tables("SalesOrderTable").Rows

            s = row.Item("StatusCode")
            foundRow = AeroPartsDataSet.Tables("StationTimeTable").Rows.Find(s)
            f = foundRow("Time")
            If row.Item("Days At Station") > f Then
                'merge data into TblOrderStatus
            End If
        Next row
        grvExcelData.DataSource = TblOrderStatus 'AeroPartsDataSet.Tables("SalesOrderTable") 'bind Datasoure to Datagridview
        grvExcelData.Update()

    End Sub
End Class
However now I am getting a "No primary key" error in the line;
foundRow = AeroPartsDataSet.Tables("StationTimeTable").Rows.F ind(s)
Even though I though I set up a key in the first block of code.
Code:
           Dim keys(1) As DataColumn
            keys(0) = StationTimeTable.Columns("StatusCode")
            '' Set the PrimaryKeys property to the array.
            StationTimeTable.PrimaryKey = keys
I get no error but appparently also no primary key.
Do you see the primary key problem?
Would adding an Order By to the Select string for the StationTimeTable creat the need Primary Key

Well I'm going to rem the code that should create the key and try Order By next. Let you know how it goes.

Any help on either question is of course appreciated.
 
Old November 28th, 2011, 12:40 AM
Registered User
 
Join Date: Nov 2011
Posts: 9
Thanks: 2
Thanked 0 Times in 0 Posts
Smile Work around Success

I found answers to last post.
The primary key worked when I use the column integer value (1) instead of column Name "Position Code"
Code:
 StationTimeTable.PrimaryKey = New DataColumn() {StationTimeTable.Columns(1)}
As far as creating a datatable to show orders at station over tager time, the code below did the job. I am not sure it is the fastest way of processing the data, 45 second to process 42008 record, but it gave me the ability to run a progress bar.

Hope code can help someone in the future.

Code:
   Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        ProgressBar1.Visible = True

        Dim TblOrderStatus As DataTable = New DataTable("TblOrderSatus")
        TblOrderStatus = AeroPartsDataSet.Tables("SalesOrderTable").Clone
        Dim RowCount As Integer = AeroPartsDataSet.Tables("SalesOrderTable").Rows.Count
        Dim nCurrentRec As Integer = 0 'first record in datatable
        Dim s As String
        Dim f As Decimal
        Dim dRowCount As Integer = AeroPartsDataSet.Tables("SalesOrderTable").Rows.Count
        Dim lap As Integer = 0
        Dim foundRow As DataRow
        Dim newOrderRow As DataRow = TblOrderStatus.NewRow

        For Each row As DataRow In AeroPartsDataSet.Tables("SalesOrderTable").Rows
            s = row.Item("StatusCode")
            foundRow = AeroPartsDataSet.Tables("StationTimeTable").Rows.Find(s)
            Try
                f = foundRow("Time")
            Catch ex As Exception
                'MsgBox("No StatusCode " + s + " Found in Station Time Table" + Chr(13) + "Alloted Time will be set to 10 day")
                f = 10
            End Try
            If f = 0 Then
                MsgBox("StatusCode " + s + " has a value of " + s.ToString + Chr(13) + "Alloted Time will be set to 10 day")
                f = 10
            End If
            If row.Item("Days At Station").ToString = "" Then
                'skip this record
            ElseIf row.Item("Days At Station") > f Then
                TblOrderStatus.ImportRow(row)
            End If
            lap = lap + 1
            ProgressBar1.Value() = (lap / dRowCount) * 100
            ProgressBar1.Update()
        Next row
        TblOrderStatus.AcceptChanges()
        ProgressBar1.Visible = False
        ProgressBar1.Value() = 0
        grvExcelData.DataSource = TblOrderStatus 'AeroPartsDataSet.Tables("SalesOrderTable") 'bind Datasoure to Datagridview
        grvExcelData.Update()
        lap = (TblOrderStatus.Rows.Count / RowCount) * 100
        RecordCountTextBox.Text = TblOrderStatus.Rows.Count.ToString + " or " + lap.ToString + "% " + " Order Transactions Time Over Target"

    End Sub
 
Old November 28th, 2011, 10:32 AM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

That's probably not the fastest method but it sounds like it works.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
 
Old November 28th, 2011, 03:37 PM
Registered User
 
Join Date: Nov 2011
Posts: 9
Thanks: 2
Thanked 0 Times in 0 Posts
Default Still would like to know

In the link they use a connection string. I have been unable to find a sample of a connection string that connects to datatables in memory.
 
Old November 30th, 2011, 02:10 PM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

I don't think you can make a connection in the same way to objects in memory, only to databases. The objects don't support exactly the same features including queries.

However, they do support some features that are similar but not exactly the same. For this problem I would suggest one of these solutions:

1. Query the data from the database when you build the initial DataSet so you don't need to do this after you load the data. If you build the DataSet in code instead of getting it from a database, then use one of the other solutions.

2. Make a DataView to represent the data and then use its RowFilter property as in:
Dim dv As New DataView(customers)
dv.RowFilter = "CustID = 4"
3. Make an array of Rows and fill it by using the table's Select method as in:
Dim rows() As DataRow = table.Select( _
"journalid = " + ddlJournalID.SelectedValue)
4. Use LINQ.

Here are a couple of discussions you can look at:

querying data from a dataset?

How we can use SQL query in DataSet Or DataTable
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Dataviews Louisa VB.NET 2002/2003 Basics 2 November 4th, 2004 09:20 AM
Dataviews Louisa VB.NET 2002/2003 Basics 1 October 15th, 2004 11:09 AM
Complex Dataviews Alaric ADO.NET 0 October 16th, 2003 07:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.