none
Using LINQ as an example from one dialog to another RRS feed

  • Question

  • Hello,

    I have this code snippet below.  See if anyone can figure this out.  Note that one of the lines is part of LINQ.

    Imports System
    
    Public Class Form1
        Private Sub TblNamesBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles TblNamesBindingNavigatorSaveItem.Click
            Me.Validate()
            Me.TblNamesBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.NamesDBTwoDataSet)
    
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'NamesDBTwoDataSet.tblNames' table. You can move, or remove it, as needed.
            Me.TblNamesTableAdapter.Fill(Me.NamesDBTwoDataSet.tblNames)
    
        End Sub
    
        Private Sub EmailAddressesDialogButton_Click(sender As Object, e As EventArgs) Handles EmailAddressesDialogButton.Click
            Form2.Show()
    
        End Sub
    End Class
    
    
    
    Public Class Form2
        Private da As New OleDb.OleDbDataAdapter
        Private dataset1 As DataSet
    
        Private Sub TblEmailAddressesBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles TblEmailAddressesBindingNavigatorSaveItem.Click
            Me.Validate()
            Me.TblEmailAddressesBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.NamesDBTwoDataSet)
    
        End Sub
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim filteredByNameID = From EMailLists In NamesDBTwoDataSet.tblEmailAddresses Join NameLists In NamesDBTwoDataSet.tblNames On EMailLists.NameID Equals NameLists.NameID Select EMailLists
    
            'method of operation is not implemented in those three lines below, for this causes a runtime error.
    
            '       InfoIDTextBox.DataBindings.Add("text", filteredByNameID, "InfoID")
            '       EmailAddressTextBox.DataBindings.Add("text", filteredByNameID, "EMailAddress")
            '      NameIDTextBox.DataBindings.Add("text", filteredByNameID, "NameID")
    
            ' Bind Label1 to username column of the logn table 
        End Sub
    
        Private Sub CloseDialogButton_Click(sender As Object, e As EventArgs) Handles CloseDialogButton.Click
            Close()
    
        End Sub
    End Class

    Anyone who is willing to improve this source code snippet is more than welcome.

    Regards,

    JohnDBCTX


    jp

    Friday, November 9, 2018 1:42 PM

All replies

  • Hello,

    Is filteredByNameID valid e.g. has data? If it has data what is the count?

    EDIT

    Here is a suggestion, create a SQL SELECT statement in your table e.g. pull up the data designer, right click, add the SQL then use it.

    Example (from the data dsigner), I created FillbyDemo

    FillByDemo

    SELECT        Products.ProductID, Categories.CategoryName, Products.ProductName, Products.UnitPrice
    FROM            Products INNER JOIN
                             Categories ON Products.CategoryID = Categories.CategoryID

    Code

    Public Class Form1
        Private Sub CategoriesBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) _
            Handles CategoriesBindingNavigatorSaveItem.Click
            Me.Validate()
            Me.CategoriesBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.NorthWindAzure2DataSet)
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.ProductsTableAdapter.FillByDemo(Me.NorthWindAzure2DataSet.Products)
            txtCategoryName.DataBindings.Add("Text", Me.ProductsBindingSource, "CategoryName")
            txtProductName.DataBindings.Add("Text", Me.ProductsBindingSource, "ProductName")
        End Sub
    End Class
    

    Note even thou there is code for update, I remove the button from the navigator.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Friday, November 9, 2018 2:17 PM
    Moderator
  • Anyone who is willing to improve this source code snippet is more than welcome.

    Myself, I would use Dapper using a custom type with the custom type loaded into a collection bound to the control. 

    https://visualstudiomagazine.com/articles/2018/03/19/dapper-orm.aspxhttps://www.codeproject.com/Articles/212274/A-Look-at-Dapper-NET

    http://www.vb-helper.com/howto_net_bind_collection_datagrid.html

    https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/auto-implemented-properties

    Friday, November 9, 2018 9:17 PM
  • Karen, can you explain this in further detail?

    Products.ProductID, Categories.CategoryName, Products.ProductName, Products.UnitPrice
    FROM            Products INNER JOIN
                             Categories ON Products.CategoryID = Categories.CategoryID

    Would this be stored in scalar, stored, or table direct?

    Regards,

    JohnDBCTX



    jp

    Saturday, November 10, 2018 6:00 AM
  • I may have done some more research regarding some sub topics.

    What if users want to create unbound text boxes before they populate them with a function similar to an Access DLookup function?

    For instance:

    Two forms:

    One form with one primary key, name ID text box can have many related foreign name ID text boxes on a second form as the name ID as the corresponding foreign key.   Therefore, a one-to-many form-to-form relationship.

    Regards,

    JohnDBCTX


    jp

    Saturday, November 10, 2018 10:02 AM