Why empty cell throws an error during SQL stored procedure execution

SELECT
    CAST ([content_html] AS XML).query('/root/Physicians/specialty/a') AS [Specialty1]
    , CAST ([content_html] AS XML).query('/root/Physicians/specialty2/a') AS [Specialty2]
    , CAST ([content_html] AS XML).query('/root/Physicians/specialty3/a') AS [Specialty3]
    , CAST ([content_html] AS XML).query('/root/Physicians/specialty4/a') AS [Specialty4]
    , CAST ([content_html] AS XML).query('/root/Physicians/specialty5/a') AS [Specialty5]
    , CAST ([content_html] AS XML).query('/root/Physicians/specialty6/a') AS [Specialty6]
FROM
    [db].[dbo].[content]
WHERE
    [folder_id] = '188'
    AND
    (content_status = 'A')
ORDER BY
    [content_title]

ASP.net repeater (partial):

<asp:Label ID="lblSpec1" runat="server"><%# Eval("Specialty1").ToString() + DisplayMultipleValues(Eval("Specialty2").ToString()) + DisplayMultipleValues(Eval("Specialty3").ToString()) + DisplayMultipleValues(Eval("Specialty4").ToString()) + DisplayMultipleValues(Eval("Specialty5").ToString()) + DisplayMultipleValues(Eval("Specialty6").ToString()) %></asp:Label>

C# (code that handles multiple entries and adds a ,):

public string DisplayMultipleValues(string strValue)
{
    return (NonBlankValueOf(strValue));
}
public string NonBlankValueOf(string source)
{
    return (string.IsNullOrEmpty(source)) ? "" : ", " + source;
}

When I run the query in SQL it works fine (If there is not value for the column, then it is an empty cell) but running it through code behind, gives me the following error:

Server Error in '/' Application.

DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Specialty3'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Web.HttpException: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Specialty3'.

Source Error: 


Line 96:                                     </div>
Line 97:                                     <div class="optionRight">
Line 98:                                         <asp:Label ID="lblSpec1" runat="server"><%# Eval("Specialty1").ToString() + DisplayMultipleValues(Eval("Specialty2").ToString()) + DisplayMultipleValues(Eval("Specialty3").ToString()) + DisplayMultipleValues(Eval("Specialty4").ToString()) + DisplayMultipleValues(Eval("Specialty5").ToString()) + DisplayMultipleValues(Eval("Specialty6").ToString()) %></asp:Label>
Line 99:                                     </div>
Line 100:                                </div>

Source File: c:er.aspx    Line: 98 

Stack Trace: 


[HttpException (0x80004005): DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Specialty3'.]
   System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName) +12523742
   System.Web.UI.DataBinder.Eval(Object container, String[] expressionParts) +142
   ASP.find_provider_aspx.__DataBind__control22(Object sender, EventArgs e) in c:er.aspx:98
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +304
   System.Web.UI.Control.DataBindChildren() +12746711
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +321
   System.Web.UI.Control.DataBindChildren() +12746711
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +321
   System.Web.UI.WebControls.Repeater.CreateItem(Int32 itemIndex, ListItemType itemType, Boolean dataBind, Object dataItem) +183
   System.Web.UI.WebControls.Repeater.CreateControlHierarchy(Boolean useDataSource) +659
   System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e) +164
   find_provider.Search() in c:er.aspx.cs:294
   find_provider.Page_Load(Object sender, EventArgs e) in c:er.aspx.cs:37
   System.Web.UI.Control.LoadRecursive() +71
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3178

What is the best way to resolve it?

I tried the following:

<asp:Label ID="lblspec1" runat="server"><%# If(Eval("Specialty1").ToString() Is DBNull.Value, "", Eval("Specialty1").ToString()) + If(Eval("Specialty2").ToString() Is DBNull.Value, "", DisplayOffices(Eval("Specialty2").ToString())) + If(Eval("Specialty3").ToString() Is DBNull.Value, "", DisplayOffices(Eval("Specialty3").ToString())) + If(Eval("Specialty4").ToString() Is DBNull.Value, "", DisplayOffices(Eval("Specialty4").ToString())) + If(Eval("Specialty5").ToString() Is DBNull.Value, "", DisplayOffices(Eval("Specialty5").ToString())) + If(Eval("Specialty6").ToString() Is DBNull.Value, "", DisplayOffices(Eval("Specialty6").ToString())) %></asp:Label>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can add something like this to the code behind of the page:

protected object MyEval(string expression)
{
    object o = null;
    try
    {
        o = DataBinder.Eval(this.GetDataItem(), expression);
    }
    catch
    {
        o = System.String.Empty;
    }
    return o;
}

and then replace all Evals with “MyEval”:

<asp:Label ID="lblSpec1" runat="server"><%# MyEval("Specialty1").ToString() + DisplayMultiplMyEvalues(MyEval("Specialty2").ToString()) + DisplayMultiplMyEvalues(MyEval("Specialty3").ToString()) + DisplayMultiplMyEvalues(MyEval("Specialty4").ToString()) + DisplayMultiplMyEvalues(MyEval("Specialty5").ToString()) + DisplayMultiplMyEvalues(MyEval("Specialty6").ToString()) %></asp:Label>

Method 2

In case your problem is caused by NULL values, then you can solve your problem with sth like:

public string DisplayMultipleValues(object strValue)
{
   if (strValue == null)
      return "";
   else
      return (NonBlankValueOf(strValue.ToString()));
}

Your Eval statements should look like this:

<%# Eval("Specialty1") == null ? "" : Eval("Specialty1").ToString() + 
    DisplayMultipleValues(Eval("Specialty2")) + etc ...

Method 3

As Gerbenny stated to say in the comments. DBNull won’t be picked up by String.IsNullOrEmpty. DBNull is handled differently. to put it in code terms.

DBNull.Value != null

So basically You need to check if an Item is DBNull before trying to display or preforming any actions on it.

Essentially

    <%# Eval("Specialty1") != DBNull.Value ? Eval("Specialty1").ToString() : String.Empty + DisplayMultipleValues(Eval("Specialty2") != DBNull.Value ? Eval("Specialty2").ToString() : String.Empty)
 + etc... %>

Method 4

The stack trace and error message you provided state exactly why it isn’t working. It says

DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Specialty3'.

which is coming out of

...
System.Web.UI.DataBinder.Eval(Object container, String[] expressionParts) +142
...

which means it is erroring where you are doing Eval("Specialty3").

What all this boils down to is that the data set coming back from your database doesn’t contain a column named Specialty3. It could be from a misspelling, from not updating a proc or for a number of other reasons.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x