Date Attribute Field Update

3990
13
03-21-2011 03:07 PM
JingjingLi
New Contributor II
I am facing an issue of updating Date field using null value. The Date field on the feature class is nullable. I created a feature service and loaded it in a silverlight app. I want to make a value in the field to be blank (null). But the attributes property of a graphic is IDictioanry<string, object> which is not nullable. How can I make it accept a null value?
Just like Graphic.Attributes["DateField"] = null
0 Kudos
13 Replies
JenniferNery
Esri Regular Contributor
Can you share the service end point field information?

For example, when I visit this service end point from my web browser: http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/HomelandSecurity/operations/FeatureServer...


collection_time (Type: esriFieldTypeDate, Alias: Collection Date, Length: 36, Editable: True)


This means there is a required length to the field.
0 Kudos
JingjingLi
New Contributor II
UPDATEDATE (Type: esriFieldTypeDate, Alias: UPDATEDATE, Length: 36, Editable: True) 

And 'Allow Null values' on the field is set to true.
This is actually the default Date field setting. I don't think I can create a Date field with a specific length. Does this mean you cannot update the date value to null in silverlight?
0 Kudos
dotMorten_esri
Esri Notable Contributor
Graphic.Attributes["DateField"] = null should work.
Are you saying it doesn't? It not, what happens when you try that?
0 Kudos
JenniferNery
Esri Regular Contributor
I think that since you gave a length requirement, a validation exception will be thrown if this is not met. If you made g.Attributes["UPDATEDATE"] = null.

If you remove this length requirement, I think you should be able to set it to a null value without validation exception.
0 Kudos
JingjingLi
New Contributor II
Graphic.Attributes["DateField"] = null should work.
Are you saying it doesn't? It not, what happens when you try that?


No it does not work. It throws a NullReferenceException.
foreach (ESRI.ArcGIS.Client.Graphic g in fl.Graphics)
                {
                    if ((int)g.Attributes["OBJECTID"] == objid)
                    {
                        IDictionary<string, object> att = g.Attributes;
                        if (att["UPDATETIME"] != null)
                        {
                            att["UPDATETIME"] = "";
                        }

                        if (att["UPDATEDATE"] != null)
                        {
                            //UpdateDate is a Date field
                            att["UPDATEDATE"] = null;
                        }

                        if (att["DESCRIPTION"] != null)
                        {
                            att["DESCRIPTION"] = "";
                        }
                        
                    }
                }


It's strange. For these records that have null value in the date field. I am able to get the null value from the Attributes property. Just not able to write a null value to it.
0 Kudos
JingjingLi
New Contributor II
I think that since you gave a length requirement, a validation exception will be thrown if this is not met. If you made g.Attributes["UPDATEDATE"] = null.

If you remove this length requirement, I think you should be able to set it to a null value without validation exception.


How can you remove the length of a Date field? I think by default you can only setup the field type and nullable for a Date field.
0 Kudos
PreetiMaske
Esri Contributor
I guess DateTime is Non-Nullable DataType so setting it to null won't work.
Try setting it to null as follows:
g.Attributes["UPDATEDATE"] = new DateTime?();
0 Kudos
JenniferNery
Esri Regular Contributor
You are right, by default DateField has a default length of 36.

You can try the following feature service that has a nullable DateField. Setting the attribute value to null does not raise any exception. You can wire up to EndSaveEdits and SaveEditsFailed to see that this new value is actually saved.

FeatureLayer layer = new FeatureLayer() { Url = "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/HomelandSecurity/operations/FeatureServer/0", OutFields = new ESRI.ArcGIS.Client.Tasks.OutFields() { "*" } };
layer.Initialized += (s,e) =>
{
 layer.Update();
};
layer.UpdateCompleted += (s,e) =>
{
 var graphic = layer.Graphics.FirstOrDefault(g => (int)g.Attributes["objectid"] == 105635);
 graphic.Attributes["collection_time"] = null;
};
layer.Initialize();


If you are still experiencing issues please explain your use case with details about the data and application workflow so we can reproduce it here. Thanks.
0 Kudos
JingjingLi
New Contributor II
I guess DateTime is Non-Nullable DataType so setting it to null won't work.
Try setting it to null as follows:
g.Attributes["UPDATEDATE"] = new DateTime?();


It throws a NullReferenceException error. I think it is the same as
g.Attributes["UPDATEDATE"] = null
.
0 Kudos