Nullable int fields showing as zeroes

  1. #1

    Nullable int fields showing as zeroes

    When I databind an object with an int? property to a store which includes the recordfield definition:

    <ext:RecordField Name="SomeIntId" Type="Int" />
    I'll find that if SomeIntId is NULL in the database and hence NULL in my C# object, it ends up as 0 in the store's data rather than null.

    Why?

    I can work around it but I'm surprised at the default behaviour of the RecordField. Looking at the ExtJS API docs for Ext.data.Field
    (http://dev.sencha.com/deploy/dev/doc...Ext.data.Field) I notice a useNull config option

    (Optional) Use when converting received data into a Number type (either int or float). If the value cannot be parsed,
    null will be used if useNull is true, otherwise the value will be 0. Defaults to false
    I don't see the equivalent Ext.NET property for useNull and RecordField doesn't have a CustomConfig property.

    Or am I missing something?
  2. #2
    Hi,

    You are right, .useNull config option is absent. I'm not sure why. I'm pretty sure we will add this one.

    For now, I can suggest the following: use your own converter basing on the default 'INT' converter.
    http://dev.sencha.com/deploy/dev/doc...data.Types-INT

    It's almost the same, just set .useNull to true.

    Example
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                Store store = this.GridPanel1.GetStore();
                store.DataSource = new object[] 
                { 
                    new object[] { null },
                    new object[] { 5 },
                    new object[] { null },
                };
                store.DataBind();
            }
        }
    </script>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Ext.Net Example</title>
    
        <script type="text/javascript">
            var myConverter = function (v) {
                 this.useNull = true;
                 return v !== undefined && v !== null && v !== '' ?
                        parseInt(String(v).replace(Ext.data.Types.stripRe, ''), 10) : (this.useNull ? null : 0);
    
            }
        </script>
    </head>
    <body>
        <form runat="server">
        <ext:ResourceManager runat="server" />
        <ext:GridPanel ID="GridPanel1" runat="server" AutoHeight="true">
            <Store>
                <ext:Store runat="server">
                    <Reader>
                        <ext:ArrayReader>
                            <Fields>
                                <ext:RecordField Name="test">
                                    <Convert Fn="myConverter" />
                                </ext:RecordField>
                            </Fields>
                        </ext:ArrayReader>
                    </Reader>
                </ext:Store>
            </Store>
            <ColumnModel runat="server">
                <Columns>
                    <ext:Column Header="Test" DataIndex="test">
                        <Renderer Handler="return value ? value : 'null';" />
                    </ext:Column>
                </Columns>
            </ColumnModel>
        </ext:GridPanel>
        </form>
    </body>
    </html>
  3. #3
    If you set UseNull="true" on the config, it should be parsed correctly. The property will not show up in VS Intellisense, but it should render.

    Hope this helps.
    Geoffrey McGill
    Founder & CEO
  4. #4
    I should say that I tried to set UseNull for RecordField, but got the exception that RecordFiled doesn't consist UseNull property.
  5. #5
    Quote Originally Posted by Daniil View Post
    Hi,

    You are right, .useNull config option is absent. I'm not sure why. I'm pretty sure we will add this one.

    For now, I can suggest the following: use your own converter basing on the default 'INT' converter.
    http://dev.sencha.com/deploy/dev/doc...data.Types-INT

    It's almost the same, just set .useNull to true.
    Yes, that's more or less my workaround.

    Any idea when useNull will be added to the recordfield and/or the customconfig tag will be added?
  6. #6
    Hi,

    The property is added in SVN
    Vladimir Shcheglov
    Sr. Developer
  7. #7
    Quote Originally Posted by Vladimir View Post
    Hi,

    The property is added in SVN
    Could you consider adding a CustomConfig child tag to all Ext.Net controls? That'd get around issues like the one I've raised in this post.
  8. #8
    The feature ticket has been created.
    https://extnet.lighthouseapp.com/pro...res/tickets/79

Similar Threads

  1. DateField / Data Index Nullable
    By mcfromero in forum 1.x Help
    Replies: 0
    Last Post: May 13, 2012, 11:56 PM
  2. Nullable Parameters
    By Zdenek in forum 1.x Help
    Replies: 1
    Last Post: Mar 16, 2012, 7:54 PM
  3. Replies: 1
    Last Post: Apr 28, 2011, 1:00 PM
  4. nullable TextField with Vtype of email
    By javadzarrin in forum 1.x Help
    Replies: 1
    Last Post: Sep 06, 2010, 8:40 PM
  5. Replies: 7
    Last Post: Aug 12, 2009, 5:04 PM

Posting Permissions