Pregunta

I've got the given code :

file.ascx.cs

protected void Page_Load(object sender, EventArgs e)
{
    SqlDataSource.SelectCommand = @"SELECT reu.duree_minutes as DUREE
                                            FROM z_reunions reu"
    List.DataSourceID = "SqlDataSource";
    List.DataBind();
}

file.ascx

<asp:SqlDataSource ID="SqlDataSource" runat="server"></asp:SqlDataSource>
<asp:ListView ID="List" runat="server">

    ...
    <%if (Convert.IsDBNull("DUREE"))
      { %>
         <asp:Label ID="Label6" runat="server" Text='NULL' ></asp:Label>
    <%} else { %>
         <asp:Label ID="Label8" runat="server" Text='<%# Eval("DUREE").GetType() %>'></asp:Label>
    <%} %>

And I've got the following output :

System.DBNull
System.DBNull
System.DBNull
System.DBNull
System.Int32
System.Int32

But I expect it to be :

NULL
NULL
NULL
NULL
System.Int32
System.Int32

Am I wrong ? Or is there a strange behavior ?

¿Fue útil?

Solución

Try this

<%# Eval("DUREE") == DBNull.Value ? <%# Eval("DUREE").GetType().ToString() %> : "NULL" %>

or just

<%# Eval("DUREE") == DBNull.Value ? "System.DBNull" : "NULL" %>

Instead using <%# Eval("DUREE").GetType().ToString() %> and "NULL" you can add html

See here about using Eval in if-statement eval in if statement?

You can use your code with 2 labels and use Visible property with Eval

<asp:SqlDataSource ID="SqlDataSource" runat="server"></asp:SqlDataSource>
<asp:ListView ID="List" runat="server">

    ...
    <asp:Label ID="Label6" runat="server" Text='NULL' Visible='<%# Eval("DUREE") == DBNull.Value %>'></asp:Label>
    <asp:Label ID="Label8" runat="server" Text='<%# Eval("DUREE").GetType() %>' Visible='<%# Eval("DUREE") != DBNull.Value %>'></asp:Label>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top