Why empty cell throws an error during SQL stored procedure execution

后端 未结 4 844
再見小時候
再見小時候 2021-01-28 01:50
SELECT
    CAST ([content_html] AS XML).query(\'/root/Physicians/specialty/a\') AS [Specialty1]
    , CAST ([content_html] AS XML).query(\'/root/Physicians/specialty2/a\         


        
相关标签:
4条回答
  • 2021-01-28 02:34

    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>
    
    0 讨论(0)
  • 2021-01-28 02:38

    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... %>
    
    0 讨论(0)
  • 2021-01-28 02:46

    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 ...
    
    0 讨论(0)
  • 2021-01-28 02:51

    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.

    0 讨论(0)
提交回复
热议问题