0

As mentioned above, I'm using LINQ Expression Holes to create XML from a SQL Server database.

I'm currently calling a stored procedure which joins two tables. Some columns have null values because of this.

Let's say that column Z can sometimes be null. I should be able to use the call IsZNull to check for a null value in the column, and use the return value only if the column is not null.

Here is what I have:

Dim MyTableAdapter As MyDataSetTableAdapters.ListMyDataTableAdapter = New MyDataSetTableAdapters.ListMyDataTableAdapter
MyTableAdapter.Fill(MyDataSet.MyData)

Dim MyXMLData = <MyDataTable>

<%= From MyTable In MyDataSet.MyData _
                             Select <MyRow>
                                        <Name><%= MyTable.Name.Trim %></Name>
                                        <ID><%= MyTable.ID %></ID>
                                        <%= IIf(MyTable.IsZNULL, <Z>""</Z>, <Z><%= MyTable.Z %></Z>) %>
                                    </MyRow> %> _ 
                      </MyDataTable>

    Return MyXMLData

I'm expecting that if MyTable.IsZNull returns true, then MyTable.Z won't be returned, but that the XML element is simply empty.

But instead, it appears that MyTable.Z is being returned anyway, causing this exception:

(within the DataSet designer)

The value for column 'Z' in table 'ListMyData' is DBNull

Stack trace:

at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Xml.Linq.XContainer.AddContentSkipNotify(Object content)
at System.Xml.Linq.XContainer.Add(Object content)

Why is the value of column Z being returned, despite it being in an IIf statement? Is the syntax of my IIf statement incorrect, or is the column value returned ahead of time, regardless of result of the IIF statement?

If I replace the expression hole for column Z with XML literals in the IIF statement (While keeping the check for IsZNull) it seems to behave properly. So it seems that the check works, but that the value of Z is being returned anyway.

I've tested out using XSL If statements as well. Same behavior.

Bugs
  • 4,491
  • 9
  • 32
  • 41
5unnyr4y3
  • 85
  • 1
  • 2
  • 11
  • I was able to get this code to work with <%= If(Not MyTable.IsZNull, <%= MyTable.Z %>, "") %> But my question still stand - why didn't IIf work? – 5unnyr4y3 Mar 17 '17 at 15:22
  • See [Performance difference between IIf() and If](http://stackoverflow.com/questions/28377/performance-difference-between-iif-and-if) – Bugs Mar 17 '17 at 16:53

0 Answers0