We help IT Professionals succeed at work.

VB.NET Linq To SQL Data not showing in DataGrid (Winforms)

Working on a simple form.  At least I thought.  Need to capture payments for an accounting system and I'm trying to use link for the first time.  Following "Linq in Action" to help me.  

1.  I added my Linq to SQL class (easy enough)
2.  Added the table I needed to the dbml (easy enough)

I go to write code from the examples I see and my grid is totally empty.

'Get Data from database
Dim db As New PurchaseOrderDataContext
Dim query = From aPO In db.PurchaseOrders
                      Select aPO
'Set results to Grid
Me.dgPayments.DataSource = query

Open in new window


I haven't gotten to the ability to add debits and credits via LINQ yet.  Any idea why I'm not seeing any data?
Comment
Watch Question

Retired
Distinguished Expert 2017
Commented:
Hi CipherIS;

The first thing to remember is that Linq queries like the one in your question does not execute immediately therefore your variable query only holes a quiry to be executed on the SQL Server and does not yet hold the results of that query. To get the query to execute you need to iterate over the collection or use one of the methods which will cause it to iterate through the collection such as ToList. Try your query like the following and you should see some results.

'
Get Data from database
Dim db As New PurchaseOrderDataContext

Dim query = (From aPO In db.PurchaseOrders
             Select aPO).ToList()

'Set results to Grid
Me.dgPayments.DataSource = query

Open in new window

Author

Commented:
AWESOME!   Thank you.
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Not a problem CipherIS, glad to help.

There is a free tool that you can download written by the person who wrote C# in a Nutshell. They also have a paid version but seeming you are programming in VB it will not be worth it to purchase it because the only thing that comes with it is intelliSense but it is only available for C# but not for the VB language. It also has lots of examples and it is possible to download more, this is available for both languages without the paid version.

LinqPad

Author

Commented:
I actually do program in C#.  This just happens to be a VB.NET app.  I write in both languages among others.  Basically, I write whatever I'm paid to write in.