Link to home
Start Free TrialLog in
Avatar of ITPOL
ITPOLFlag for United Kingdom of Great Britain and Northern Ireland

asked on

TypedDataSet Linq query fails with dbnull error

Hi I am having problems figuring out the correct LINQ syntax.

I have the following dataset, where DtHistory.NotesId can be null if there are no notes associatd with it.

User generated image
I am using the following code to query the dataset;

Dim HistoryQuery = From o In ds.DtHistory
                           Join ol In ds.DtCompany On o.CompId Equals ol.DtCompany_Id
                           Join nt In ds.DtNotes On o.NotesId Equals nt.DtNotes_Id
                           Order By o.DateAccessed Descending
                           Select New With { _
                                  o.DtHistory_Id, _
                                  ol.PayrollRef, _
                                  ol.Name, _
                                  o.DateAccessed,
                                  nt.Note _
                                    }

For Each res In HistoryQuery

            Dim lvi As ListViewItem = ListView1.Items.Add(res.PayrollRef)
            lvi.SubItems.Add(res.Name)
            lvi.SubItems.Add(res.DateAccessed)
            
Next

Open in new window


But it throws the exception;  "The value for column 'NotesId' in table 'DtHistory' is DBNull.".

I have read various suggestions like using ".DefaultIfEmpty()"  and the generated property ".IsNoteNull" but I cannot get them to work in my Linq query.

All i want to do is retrieve all items from the history tab with the associated values from the other two tables (if not dbnull)

Anyone able to help?
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Does this work:
Dim HistoryQuery = From o In ds.DtHistory
                           Join ol In ds.DtCompany On o.CompId Equals ol.DtCompany_Id
                           LEFT join nt In ds.DtNotes On o.NotesId Equals nt.DtNotes_Id
                           Order By o.DateAccessed Descending
                           Select New With { _
                                  o.DtHistory_Id, _
                                  ol.PayrollRef, _
                                  ol.Name, _
                                  o.DateAccessed,
                                  nt.Note _
                                    }
Avatar of ITPOL

ASKER

Just tried it, but no.

"Left" doesn't seem to be a linq keyword
Ah well, worth a try.  It is how one handles that relationship in SQL.
Hi ITPOL;

Seeming that the value of Note can be null/Nothing test it for Nothing and assign it a value if it is Nothing otherwise use the value. Try the code below.
Dim HistoryQuery = From o In ds.DtHistory
                   Join ol In ds.DtCompany On o.CompId Equals ol.DtCompany_Id
                   Join nt In ds.DtNotes On o.NotesId Equals nt.DtNotes_Id
                   Order By o.DateAccessed Descending
                   Select New With { _
                          o.DtHistory_Id, _
                          ol.PayrollRef, _
                          ol.Name, _
                          o.DateAccessed,
                          .Note = If(nt.Note Is Nothing, " ", nt.Note) _
                   }

Open in new window

Avatar of ITPOL

ASKER

That was one of the things I tried actually.  Unfortunately it still throws the exception.

From what I've read its to do with the way typed datasets are generated.

User generated image
Avatar of ITPOL

ASKER

Same problem here; but I cant see how to replicate this into this version of linq


http://stackoverflow.com/questions/881225/linq-to-dataset-dbnull-problem-null-reference-exception

Avatar of ITPOL

ASKER

and here
Try changing this line

.Note = If(nt.Note Is Nothing, " ", nt.Note) _

to this

.Note = If(nt.Note Is Nothing, 0, nt.Note) _
What I think may be happening is that the object itself is null so try this if the other did not work.

.Net = If(nt Is Nothing, " ", nt.Note) _
Avatar of ITPOL

ASKER

Thanks Fernando,

What I think may be happening is that the object itself is null so try this if the other did not work.

.Net = If(nt Is Nothing, " ", nt.Note) _

Afraid this didn't work either.  I also tried replacing the blank with 0.
Avatar of ITPOL

ASKER

Its double-bank holiday here in the UK, so I wont look at this again until tuesday.  Thanks for your help so far.
Where o.NotesId.HasValue = True
Avatar of ITPOL

ASKER

Where o.NotesId.HasValue = True

Won't this filter my results to only rows that have a value?  in any case there was no "hasvalue" property on NotesId
In your code snippet you never actually use the nt.Note - so do you need the DtNotes table in the query?  If not then leave that out and see if that makes a difference.
Avatar of ITPOL

ASKER

haha, Well spotted but yes, I do.  

I want to show it on the form if there is a note associated with the record.
OK, thought that might be the case.

At which line of code do you actually get the exception with the DBNull and when?
Avatar of ITPOL

ASKER

Here;

For Each res In HistoryQuery
The first time it meets it or at a later point when looping.  (It might not be relevant but, then again, it might help in localising the problem).
Avatar of ITPOL

ASKER

Sorry I should have said.  It's as soon as it starts. i.e. the very first loop.
Is NotesID set to be nullable in your dataset designer?
ASKER CERTIFIED SOLUTION
Avatar of ITPOL
ITPOL
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ITPOL

ASKER

Solution was how I got round the issue, unfortunately couldn't find an actual answer