Viewing sexy dataitem null rowupdating Sins literally gets

February 8th, 2006, 04:33 AM


  • Container.DataItem - How to Deal with Null Numeric data

    im opening a database in 1.1 with a data reader

    Within a repeat I have data containers.


    some of the data is of the data type Numeric or Decimal

    now I want some of the Numeric Data contained to be NULL

    How can I stop this following error

    Operator is not valid for type 'Decimal' and type 'DBNull'.

    I want the Container.DataItem to show the number if its not null or show nothing if it is Null

    Any VB solutions will be great
  • February 8th, 2006, 06:57 AM


  • Create a function which take the DataItem as a parameter and checks if the value is null before returning a string representation of that value. Then call that function from the aspx form.

  • February 8th, 2006, 07:47 AM


  • Public Function IsDBNull(ByVal dbvalue) As Boolean
    Return dbvalue Is DBNull.Value
    End Function

    Public Function FixNull(ByVal dbvalue) As String
    If dbvalue Is DBNull.Value Then
    Return ""
    Return dbvalue.ToString
    End If
    End Function

    Just want to know if you can do this inline ?

  • February 8th, 2006, 07:56 AM


  • Luckily I'm working on a project now where I'm using that exact same method. Unfortunately it's in C# but you should get the idea of what I mean from below.

    In your aspx form you would have something like so:


    <asp:Repeater id="rptList" runat="server"> <ItemTemplate> <%# GetDataItemValue (Container.DataItem, "fieldName"); %><br> </ItemTemplate> </asp:Repeater>
    and in you class you will have the following method:


    protected string GetDataItemValue (object dataItem, string fieldName) { if (dataItem != null) { DataRowView cls = (DataRowView)dataItem; if (!cls.Row.IsNull(fieldName)) return cls[fieldName].ToString(); else return "&nbsp;"; } else return "&nbsp;"; }
    this way, the aspx form relies on the code-behind to supply it with a value instead of it trying to process the null value itself.
  • February 8th, 2006, 08:16 AM


  • nods

    i think my problem is lying like this

    I recieve no error if i declare the container like below


    <%# Container.DataItem("Amount") %>
    But if i want to add formating like


    <%# FormatCurrency(Container.DataItem("Amount")) %>
    It will throw up an error if the amount value is null
  • February 8th, 2006, 08:25 AM


  • your code would do something like this, it's not correct syntax but the method is correct


    <asp:Repeater id="rptList" runat="server"> <ItemTemplate> <%# GetCurrency (Container.DataItem, "Amount"); %><br> </ItemTemplate> </asp:Repeater>


    protected string GetCurrency (object dataItem, string fieldName) { if (dataItem != null) { DataRowView cls = (DataRowView)dataItem; if (!cls.Row.IsNull(fieldName)) return FormatCurrency (cls[fieldName]).ToString(); else return "&nbsp;"; } else return "&nbsp;"; }
    you may be able to do this an easier way, but I don't usually write or use inline code so I can't help you there
  • February 8th, 2006, 08:56 AM


  • shame you can just do


    if Container.DataItem("Amount") <> "" then FormatCurrency(Container.DataItem("amount")) End if

    I can do this database side
    isNull(Fieldname,0) As Fieldname

    but i dont want to really return 0 if the field is null
    I need it to be blank
  • February 8th, 2006, 11:51 AM


  • in that case, return the field as a string. try this in your sql statement:


    CASE WHEN FieldName IS NOT NULL THEN CAST(FieldName AS VARCHAR(10)) ELSE '' END AS FieldString
  • February 9th, 2006, 04:10 AM


  • Nice db side solution

    Im still trying to do it inline
    i found this will work


    <%# iif(container.DataItem("FieldName") is DBNULL.value,"",container.DataItem("FieldName") )%>
    But if i add FormatCurrency to it and if its not DBNull.value


    <%# iif(container.DataItem("FieldName") is DBNULL.value,"",formatCurrency(container.DataItem("FieldName")) )%>
    it throws up
    System.InvalidCastException: Cast from type 'DBNull' to type 'Currency' is not valid.

    now im not sure if im using the IIF command correctly
    or im trying to make it do somthing it cant

    my thoughts were

    if the fieldName is null then "" Else fieldName {formated a currency ..}

    very strange

    {Comes running back}

    Right I have re looked at using functions to Fix Null values

    The function FixNull checks for null values and returns nothing if a null if found

    Else it will return the Value in the database and formats it
    as currency ... Yay

    Function FixNull(ByVal dbvalue) As String
    If dbvalue Is DBNull.Value Then
    Return ""
    Return FormatCurrency(dbvalue.ToString)
    End If
    End Function


    <%# FixNull(container.DataItem("FieldName"))%>
  • February 9th, 2006, 06:25 AM


  • Cool, that's pretty much along the same lines as my GetCurrency function that I gave above. Well done for sussing it out.

  • February 9th, 2006, 09:47 AM


  • laugh ! cheers

  • May 29th, 2008, 11:22 AM


  • I have the same problem
    Here is my code


    <itemtemplate> <%# test = getcontainertest(CType(Container.DataItem, DataRowView).Row.ItemArray(1).ToString())%> <% if test = "N" then%> <IMG height="80" id="ImgAucun" src = "Photo_Produit/Aucun.gif"> <%Else%> <asp:image height="80" id="Image1" runat="server" imageurl='<%#DataBinder.Eval(Container.DataItem, "IMAGE", "~/Photo_Produit/{0}")%>'/> <%End if%> </itemtemplate> private function GetContainertest(ByVal sDataItem) as string If sdataitem ="" Then return "N" else return "O" End if end function
    The problem I have is that the value is not null but it's empty. When I execute my code like that, I see the Image when it has something in the field("IMAGE") of my database, but I don't see the other image named Aucun.gif.
    Can someone help me please
  • May 29th, 2008, 11:47 AM


  • No, you don't have the same problem. Instead of digging up a 2-year-old thread, please start your own thread. While you're at it, please get acquainted with the sticky threads called "How to post a question".

    Joel B Fant
    "An element of conflict in any discussion is a very good thing. Shows everybody's taking part, nobody left out. I like that."

    .NET Must-Haves
    Tools: Reflector
    References: Threading in .NET