AIR24.INFO

Viewing sexy dataitem null rowupdating Sins literally gets

February 8th, 2006, 04:33 AM

#1

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


    im opening a database in ASP.net 1.1 with a data reader

    Within a repeat I have data containers.

    Container.DataItem

    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

    #2

  • 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

    #3

  • 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 ""
    Else
    Return dbvalue.ToString
    End If
    End Function

    Just want to know if you can do this inline ?

  • February 8th, 2006, 07:56 AM

    #4

  • 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:

    Code:

    <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:

    Code:

    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

    #5

  • nods

    i think my problem is lying like this

    I recieve no error if i declare the container like below

    Code:

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

    Code:

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

    #6

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

    Code:

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

    Code:

    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 VB.net or use inline code so I can't help you there
  • February 8th, 2006, 08:56 AM

    #7

  • shame you can just do

    Code:

    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

    #8

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

    Code:

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

    #9


  • Nice db side solution

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

    Code:

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

    Code:

    <%# 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 ""
    Else
    Return FormatCurrency(dbvalue.ToString)
    End If
    End Function

    Code:

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

    #10

  • 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

    #11

  • laugh ! cheers

  • May 29th, 2008, 11:22 AM

    #12

  • I have the same problem
    Here is my code

    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

    #13

  • 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

  • http://forums.devshed.com/net-development-87/container-dataitem-deal-null-numeric-data-324544.html