Hi all I have written the following to display the integer values from database to the gridview column as follows
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblTotalReplies" runat="server" Text='<%#Eval("TotalReplies")==System.DBNull ? "0" : Convert.ToInt16(Eval("TotalReplies")).ToString() %>'></asp:Label> Replies</li>
</ItemTemplate>
</asp:TemplateField>
But I am getting an error as System.DBNull' is a 'type', which is not valid in the given context
can some one help me how can I display 0
when I am having null value in database. I don't want to bind using rowdatabound
or some other
Compare with DBNull.Value
<asp:Label ID="lblTotalReplies" runat="server"
Text='<%#Eval("TotalReplies") ==System.DBNull.Value ? "0" :
Convert.ToInt16(Eval("TotalReplies")).ToString() %>'>
</asp:Label> Replies</li>
DBNull is the type, while the Value property
Represents the sole instance of the DBNull class.
It's true. It is a type. Maybe you're looking for the static Value
field on it?
Eval("TotalReplies")==System.DBNull.Value
Represents the sole instance of the
DBNull
class.
Ravi's comment was pointing out that if your SQL currently looks like this:
SELECT TotalReplies, ...
Then you could change it to:
SELECT ISNULL(TotalReplies,0) as TotalReplies, ...
I'd usually use COALESCE
rather than ISNULL
, it would look identical in this case.
The System.DBNull
type has a static field Value
to which you should compare:
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblTotalReplies" runat="server" Text='<%#Eval("TotalReplies") == System.DBNull.Value ? "0" : Convert.ToInt16(Eval("TotalReplies")).ToString() %>'></asp:Label> Replies</li>
</ItemTemplate>
</asp:TemplateField>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.