4
Answers

Handling null values in Eval()

Marius Vasile

Marius Vasile

1y
620
1

I have a Datalist which depending on condition can be populated from 3 queries. The problem I have is that one query generates one additional field which the other two doesn't and when I run the query I have the following error

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

I tried to handle this with

Text='<%#(Eval("MediuMunca") == null ? "-" : Eval("MediuMunca"))%>'

Text='<%#Eval("MediuMunca") == DBNull.Value ? "-" : Eval("MediuMunca")%>'
Markup

but is not working. What am I doing wrong?

Answers (4)
2
Prasad Raveendran

Prasad Raveendran

233 8.3k 1.9m 1y

The error you're encountering is likely due to the fact that the field "MediuMunca" doesn't exist in the DataRowView for some of the queries bound to the DataList.

When you try to access a field that doesn't exist in the DataRowView, it throws an exception. To handle this situation, you're on the right track with the use of the ternary operator to check for null or DBNull.Value. However, the issue might be arising because the column "MediuMunca" is not present in all the DataRows from different queries.

To mitigate this issue, you can modify the binding logic in the following way:

<div class="row no-gutters">
    <asp:TextBox ID="txtMediuMunca" class="form-control form-control-sm"
        Style="font-size: 12px; color: black; resize: none; font-weight: 500;" runat="server" TextMode="MultiLine" Rows="10"
        ClientIDMode="Static" Text='<%# CheckNull(Eval("MediuMunca")) %>' />
</div>

 

In your code-behind or in the code-behind file associated with your ASP.NET page, define the CheckNull method:

protected string CheckNull(object value)
{
    if (value == null || value == DBNull.Value || string.IsNullOrWhiteSpace(value.ToString()))
    {
        return "-";
    }
    return value.ToString();
}

This CheckNull method checks whether the value is null, DBNull.Value, or an empty string and replaces it with a dash ("-") in those cases. This should prevent the "does not contain a property" error from occurring when the "MediuMunca" field is not present in certain DataRows.

Make sure to modify this method according to the exact logic you need for handling DBNull and null values in your application.

Additionally, ensure that the field "MediuMunca" is available in the DataRows from all the queries you're binding to the DataList. If it's not present in some queries, you may need to modify your SQL queries or adjust your data retrieval logic to ensure consistency in the fields across all DataRows.

Accepted
3
Jignesh Kumar

Jignesh Kumar

29 39.5k 2.9m 1y

Hi,

this error says - 'DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'MediuMunca'.'

Your select statement does not have column 'MediuMunca'. please check your select statement if its not there then add this column then it should work.

3
Vishal Yelve

Vishal Yelve

106 17.2k 633.7k 1y

Hi Marisu,

do refer below link, hope this helps you 

http://csharpdotnetfreak.blogspot.com/2009/08/handle-gridview-eval-null-itemtemplate.html

2
Marius Vasile

Marius Vasile

574 2k 162.4k 1y

Same error, my view is

<div class="row no-gutters">
    <asp:TextBox ID="txtMediuMunca" class="form-control form-control-sm"
        Style="font-size: 12px; color: black; resize: none; font-weight: 500;" runat="server" TextMode="MultiLine" Rows="10"
        ClientIDMode="Static" Text='<%#CheckNull(Eval("MediuMunca"))%>' />
</div>

I tried with this hidden and is working but without is not