Ryan Liu wrote:
Hi,
I thought the following code is tedious but pretty safe, unexpected
this.dataTable.DefaultView
itself throw an System.NullReferenceException?
if(this.dataTable!= null
&& this.dataTable.DefaultView != null
&& this.dataTable.DefaultView.Count 0)
{
.....
}
In which condition will this property throw an exception?
Checking the DefaultView getter code in reflector:
public DataView DefaultView
{
get
{
DataView view1 = this.defaultView;
if (view1 == null)
{
if (this.dataSet != null)
{
view1 =
this.dataSet.DefaultViewManager.CreateDataView(thi s);
}
else
{
new DataView(this, true).SetIndex2("",
DataViewRowState.CurrentRows, null, true);
}
view1 = Interlocked.CompareExchange<DataView>(ref
this.defaultView, view1, null);
if (view1 == null)
{
view1 = this.defaultView;
}
}
return view1;
}
}
it's because no View could be created which for example could be
because the datatable is completely empty (no columns)
FB
--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website:
http://www.llblgen.com
My .NET blog:
http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------