locked
Querying a datasource object RRS feed

  • Question

  • Hi

    A quick general question. I have a datasource component on my form that was generated when i used a wizard to create a datasource to a control grid. That component is called SQLDatasource1

    i can assign this to a grid by using

    Grid1.Datasource = SQLDatasource1.Fill()

    Is there a method that i can use to iterate / loop through the dataset without needing to assign it to a control? I am trying to populate a textbox with the value of a data return as the return data is a single row. I can do it using a BLL DAL class by iterating through the List(of) and using a For Each like this

    Private ed_bll As MY.BLL.GetProjectList = New MY.BLL.GetProjectList
    Private ProjRets As List(Of MY.BLL.GetProjectList_details) = ed_bll.SelectGetProjectList("")
    
    For Each ret As MY.BLL.GetProjectList_details In ProjRets
       Dim GetName As String = ret._name
       TxtName.Text = GetName
       Exit For
    Next

    The BLL DAL classes take time to set up and i have a lot to do. Using the component reduces my code time right down to a few lines..

    I'd like to do something similar if possible with a Datasource component. Is this possible?

    Thanks


    I am here to ask questions and learn from others. Cor Ligthert DO NOT REPLY TO ANY OF MY POSTS!!

    Tuesday, November 24, 2020 7:12 PM

Answers

  • Managed to work out a solution

    Dim ds As SqlDataSource = TryCast(SqlDataSource1, SqlDataSource)
    Dim src As DataApi.ITable = SqlDataSource1.Result("usp_MY_GetJobList")
    For Each row As IRow In src
       MsgBox(row.Item(1).ToString)
    Next


    I am here to ask questions and learn from others. Cor Ligthert DO NOT REPLY TO ANY OF MY POSTS!!

    • Marked as answer by NachoShaw Tuesday, November 24, 2020 8:29 PM
    Tuesday, November 24, 2020 8:29 PM

All replies

  • Managed to work out a solution

    Dim ds As SqlDataSource = TryCast(SqlDataSource1, SqlDataSource)
    Dim src As DataApi.ITable = SqlDataSource1.Result("usp_MY_GetJobList")
    For Each row As IRow In src
       MsgBox(row.Item(1).ToString)
    Next


    I am here to ask questions and learn from others. Cor Ligthert DO NOT REPLY TO ANY OF MY POSTS!!

    • Marked as answer by NachoShaw Tuesday, November 24, 2020 8:29 PM
    Tuesday, November 24, 2020 8:29 PM
  • Hello,

    The only time I use TableAdapters is to answer forum questions, I understand them but have moved on to better methods.

    Anyways here is a code sample using MS-Access as the data source, when dealing with TableAdapters the database does not matter other than how the SQL is done and in some cases a new primary key is handled.

    In this form has the code sample in this project.

    Pretty dull form :-)

    Public Class Form2
        Private Sub IterateButton_Click(sender As Object, e As EventArgs) Handles IterateButton.Click
    
            Dim customersDataTable As Database1DataSet.CustomersDataTable = Database1DataSet.Customers
            Dim rows As DataRowCollection = customersDataTable.Rows
    
            For Each row as DataRow In rows.Cast(of DataRow)
                Console.WriteLine($"ID: {row.Field(of Integer)("Identifier")} Name: {row.Field(of string)("CompanyName")}")
            Next
    
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    My GitHub code samples
    GitHub page

    Check out:  the new Microsoft Q&A forums

    Tuesday, November 24, 2020 8:49 PM
  • Hey Karen

    thanks for the response. When you say 'moved on to better methods', what would your suggestion be for a better method? I am only testing this method out to see if it speeds up my progress. I have been using a set of classes BLL DAL to get my data which is really efficient to use but takes a while to set up. This particular method is a DevExpress method. it doesnt 'exactly' add a TableAdapter or BindingSource to the form and instead, has everything built in like a Query editor, string constructor.

    Im always willing to look at newer / better methods :)

    Thanks


    I am here to ask questions and learn from others. Cor Ligthert DO NOT REPLY TO ANY OF MY POSTS!!

    Tuesday, November 24, 2020 8:58 PM
  • As stated I've only used TableAdapter to reply to forum questions.

    From when .NET first arrived I used a connection and command objects to return information into a DataSet, DataTable or used a DataReader. Never used Table or Data Adapters.

    About five years ago I moved on to Entity Framework 6 while about four years ago (guessing here) moved to Entity Framework Core but Entity Framework Core is not easy for VB.NET while Entity Framework 6 is easy e.g. you add a new item under data and select code first with existing database which results in a DbContext and one class for each table.

    Some of my code samples for Entity Framework 6

    https://github.com/karenpayneoregon/EntityFrameworkSolutionStructureWindowsForms_VisualBasic

    https://github.com/karenpayneoregon/EF-Sort-VisualBasic

    I have the majority of my Entity Framework and Entity Framework Core code samples in C# so the above is all I have for VB.

    Note that Entity Framework 6 does not support MS-Access while Entity Framework Core does and all major data providers.

    Simple read operation for relational data.


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    My GitHub code samples
    GitHub page

    Check out:  the new Microsoft Q&A forums

    • Proposed as answer by KHURRAM RAHIM Tuesday, November 24, 2020 9:35 PM
    Tuesday, November 24, 2020 9:31 PM