[CLOSED] [2.0] e.DataHandler.ObjectData and null

Page 1 of 2 12 LastLast
  1. #1

    [CLOSED] [2.0] e.DataHandler.ObjectData and null

    Hi,

    How can I make e.DataHandler.ObjectData turn string.empty to null?

    In my real application, I have a combobox inside the gridview. When I dont select anything, the combobox returns string.empty instead of null.

    Bellow is a sample code. To test, just delete a column containing a string.

    Thanks

    <%@ Page Language="C#" %>
    
    <%@ Import Namespace="System.Collections.Generic" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        public class Company
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public double Price { get; set; }
            public double Change { get; set; }
            public double PctChange { get; set; }
            public DateTime LastChange { get; set; }
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                this.BindData();
            }
        }
    
        private void BindData()
        {
            this.GridPanel1.Store.Primary.DataSource = this.GetData();
            this.GridPanel1.Store.Primary.DataBind();
        }
    
        private List<Company> GetData()
        {
            return new List<Company> 
            {
                new Company { ID = 1, Name = "3m Co", Price = 71.72, Change = 0.02, PctChange = 0.03, LastChange = DateTime.Now },
                new Company { ID = 2, Name = "Alcoa Inc", Price = 29.01, Change = 0.42, PctChange = 1.47, LastChange = DateTime.Now },
                new Company { ID = 3, Name = "Altria Group Inc", Price = 83.81, Change = 0.28, PctChange = 0.34, LastChange = DateTime.Now },
                new Company { ID = 4, Name = "American Express Company", Price = 52.55, Change = 0.01, PctChange = 0.02, LastChange = DateTime.Now },
                new Company { ID = 5, Name = "American International Group, Inc.", Price = 64.13, Change = 0.31, PctChange = 0.49, LastChange = DateTime.Now },
                new Company { ID = 6, Name = "AT&T Inc.", Price = 31.61, Change = -0.48, PctChange = -1.54, LastChange = DateTime.Now },
                new Company { ID = 7, Name = "Boeing Co.", Price = 75.43, Change = 0.53, PctChange = 0.71, LastChange = DateTime.Now },
                new Company { ID = 8, Name = "Caterpillar Inc.", Price = 67.27, Change = 0.92, PctChange = 1.39, LastChange = DateTime.Now },
                new Company { ID = 9, Name = "Citigroup, Inc.", Price = 49.37, Change = 0.02, PctChange = 0.04, LastChange = DateTime.Now },
                new Company { ID = 10, Name = "E.I. du Pont de Nemours and Company", Price = 40.48, Change = 0.51, PctChange = 1.28, LastChange = DateTime.Now },
                new Company { ID = 11, Name = "Exxon Mobil Corp", Price = 68.1, Change = -0.43, PctChange = -0.64, LastChange = DateTime.Now },
                new Company { ID = 12, Name = "General Electric Company", Price = 34.14, Change = -0.08, PctChange = -0.23, LastChange = DateTime.Now },
                new Company { ID = 13, Name = "General Motors Corporation", Price = 30.27, Change = 1.09, PctChange = 3.74, LastChange = DateTime.Now },
                new Company { ID = 14, Name = "Hewlett-Packard Co.", Price = 36.53, Change = -0.03, PctChange = -0.08, LastChange = DateTime.Now },
                new Company { ID = 15, Name = "Honeywell Intl Inc", Price = 38.77, Change = 0.05, PctChange = 0.13, LastChange = DateTime.Now },
                new Company { ID = 16, Name = "Intel Corporation", Price = 19.88, Change = 0.31, PctChange = 1.58, LastChange = DateTime.Now },
                new Company { ID = 17, Name = "International Business Machines", Price = 81.41, Change = 0.44, PctChange = 0.54, LastChange = DateTime.Now },
                new Company { ID = 18, Name = "Johnson & Johnson", Price = 64.72, Change = 0.06, PctChange = 0.09, LastChange = DateTime.Now },
                new Company { ID = 19, Name = "JP Morgan & Chase & Co", Price = 45.73, Change = 0.07, PctChange = 0.15, LastChange = DateTime.Now },
                new Company { ID = 20, Name = "McDonald\"s Corporation", Price = 36.76, Change = 0.86, PctChange = 2.40, LastChange = DateTime.Now },
                new Company { ID = 21, Name = "Merck & Co., Inc.", Price = 40.96, Change = 0.41, PctChange = 1.01, LastChange = DateTime.Now },
                new Company { ID = 22, Name = "Microsoft Corporation", Price = 25.84, Change = 0.14, PctChange = 0.54, LastChange = DateTime.Now },
                new Company { ID = 23, Name = "Pfizer Inc", Price = 27.96, Change = 0.4, PctChange = 1.45, LastChange = DateTime.Now },
                new Company { ID = 24, Name = "The Coca-Cola Company", Price = 45.07, Change = 0.26, PctChange = 0.58, LastChange = DateTime.Now },
                new Company { ID = 25, Name = "The Home Depot, Inc.", Price = 34.64, Change = 0.35, PctChange = 1.02, LastChange = DateTime.Now },
                new Company { ID = 26, Name = "The Procter & Gamble Company", Price = 61.91, Change = 0.01, PctChange = 0.02, LastChange = DateTime.Now },
                new Company { ID = 27, Name = "United Technologies Corporation", Price = 63.26, Change = 0.55, PctChange = 0.88, LastChange = DateTime.Now },
                new Company { ID = 28, Name = "Verizon Communications", Price = 35.57, Change = 0.39, PctChange = 1.11, LastChange = DateTime.Now },
                new Company { ID = 29, Name = "Wal-Mart Stores, Inc.", Price = 45.45, Change = 0.73, PctChange = 1.63, LastChange = DateTime.Now }
            };
        }
    
        protected void Save(object sender, BeforeStoreChangedEventArgs e)
        {
            var vagas = e.DataHandler.ObjectData<Company>();
        }
    
    </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>Simple Array Grid - Ext.NET Examples</title>
        <link href="../../../../resources/css/examples.css" rel="stylesheet" type="text/css" />    
    
        <script type="text/javascript">
            var template = '<span style="color:{0};">{1}</span>';
    
            var change = function (value) {
                return String.format(template, (value > 0) ? "green" : "red", value);
            };
    
            var pctChange = function (value) {
                return String.format(template, (value > 0) ? "green" : "red", value + "%");
            };
    
            var startEditing = function (e) {
                if (e.getKey() === e.ENTER) {
                    var grid = GridPanel1,
                        record = grid.getSelectionModel().getSelected(),
                        index = grid.store.indexOf(record);
    
                    grid.startEditing(index, 1);
                }
            };
    
        </script>
    </head>
    <body>
        <ext:ResourceManager runat="server" RemoveViewState="true" IDMode="Explicit" />
        
        <h1>Editable GridPanel With Save To [DirectMethod]</h1>
        
        <ext:GridPanel 
            ID="GridPanel1"
            runat="server" 
            Title="Editable GridPanel" 
            StripeRows="true"
            TrackMouseOver="true"
            Width="600" 
            Height="350"
            AutoExpandColumn="Name">
            <Store>
                <ext:Store ID="Store1" OnBeforeStoreChanged="Save"  runat="server">
                    <Reader>
                        <ext:JsonReader IDProperty="ID">
                            <Fields>
                                <ext:RecordField Name="ID" Type="Int" />
                                <ext:RecordField Name="Name" />
                                <ext:RecordField Name="Price" Type="Float" />
                                <ext:RecordField Name="Change" Type="Float" />
                                <ext:RecordField Name="PctChange" Type="Float" />
                                <ext:RecordField Name="LastChange" Type="Date" />
                            </Fields>
                        </ext:JsonReader>
                    </Reader>
                </ext:Store>
            </Store>
            <ColumnModel runat="server">
                <Columns>
                    <ext:Column Header="ID" DataIndex="ID" Width="35" />
                    <ext:Column ColumnID="Name" Header="Name" DataIndex="Name">
                        <Editor>
                            <ext:TextField runat="server" />
                        </Editor>
                    </ext:Column>
                    <ext:Column Header="Price" DataIndex="Price">
                        <Renderer Format="UsMoney" />
                        <Editor>
                            <ext:TextField runat="server" />
                        </Editor>
                    </ext:Column>
                    <ext:Column Header="Change" DataIndex="Change">
                        <Renderer Fn="change" />
                        <Editor>
                            <ext:TextField runat="server" />
                        </Editor>
                    </ext:Column>
                    <ext:Column Header="Change" DataIndex="PctChange">
                        <Renderer Fn="pctChange" />
                        <Editor>
                            <ext:TextField runat="server" />
                        </Editor>
                    </ext:Column>
                    <ext:DateColumn Header="Last Updated" DataIndex="LastChange" Format="yyyy-MM-dd">
                        <Editor>
                            <ext:DateField runat="server" />
                        </Editor>
                    </ext:DateColumn>
                </Columns>
            </ColumnModel>
            <SelectionModel>
                <ext:RowSelectionModel runat="server" SingleSelect="true" />
            </SelectionModel>
        </ext:GridPanel>      
        <ext:Button Text="Save" runat="server">
        <Listeners>
        <Click Handler="alert('2'); #{Store1}.save()" />
        </Listeners>
        </ext:Button>
    </body>
    </html>
    Last edited by Daniil; Dec 07, 2012 at 5:01 PM. Reason: [CLOSED] [2.0]
  2. #2
    Hi,

    Record's type is string. Type of Name property is string. TextField (editor) can't consist a null, only string. Everywhere is string.

    Why can't you consider an empty string as null?
  3. #3
    Good point. But when I serialize empty.string to the db, I have a problem with my null foreign key. :(
  4. #4
    It's serialized as empty string, but you need 'null' in db, right?

    What way do you serialize? Maybe, you could write your own converter for this case.
  5. #5
    Hi,

    As simple solution, you can check value in the setter of string property, if value is empty string then set null instead empty string
    Vladimir Shcheglov
    Sr. Developer
  6. #6
    Quote Originally Posted by Daniil View Post
    It's serialized as empty string, but you need 'null' in db, right?

    What way do you serialize? Maybe, you could write your own converter for this case.
    I dont think it will be simple to check for string.empty since i am using entity framework.

    let me show you guys:

      
      var cities = e.DataHandler.ObjectData<Model.City>();
      cities.Created.ForEach(p => City.InsertOrUpdate(p));
    
    -----------
     public static int InsertOrUpdate(Model.City city)
     {
           Entity.AddToCity(city);
     }
    Thanks
  7. #7
    Quote Originally Posted by Vladimir View Post
    Hi,

    As simple solution, you can check value in the setter of string property, if value is empty string then set null instead empty string
    Hi, Vladimir,

    Please check my post above. Using entity framework, it is not that simple. I would have to change .net generated code, which I dont feel comfortable or recommend.

    I could something like the code bellow, if you guys dont have a better solution:

    cities.Created.Where(p => p.City == String.Empty).ForEach(p => p.City = null);
  8. #8
    As far as I can judge this code should work fine.

    But we recommend to implement your own json converter.
  9. #9
    Thanks for the suggestion! I will think about that. Meanwhile, if you guys can add this feature in a future release, would be great for me and others.

    Feature: If string.empty, convert to null.

    Thanks, mark as solved.
  10. #10
    Hi,

    We have created the feature ticket to review in the future.
    https://extnet.lighthouseapp.com/pro...res/tickets/83
Page 1 of 2 12 LastLast

Similar Threads

  1. [CLOSED] ObjectData and PrimaryKey
    By SouthDeveloper in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Mar 06, 2012, 9:15 AM
  2. Replies: 0
    Last Post: Dec 01, 2011, 7:43 AM
  3. Ext.fly(...)' is null or not an object
    By kapil gupta in forum 1.x Help
    Replies: 5
    Last Post: Nov 10, 2010, 9:59 AM
  4. [CLOSED] [1.0] d=null
    By rthiney in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Jul 22, 2010, 12:06 AM
  5. Replies: 1
    Last Post: Jun 08, 2010, 12:38 PM

Posting Permissions