Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I updated a list of mine by adding a column. After this update I receive the following error "Value does not fall within the expected range" when I try to get the data in the colum.

This is the code that I use to get the data:

        private static DateTime GetModified(SPListItem item)
    {
        var field = item.Fields.GetFieldByInternalName("LastRevision");

        if (!field.Hidden && item.Fields.ContainsFieldWithStaticName(field.StaticName) 
            && item.GetFormattedValue(field.InternalName) != null)
        {
            return (DateTime)item[field.Id];
        }

        return DateTime.Parse(item["StartDate"].ToString());
    }

I want show to you what happen with the Convert.ToDateTime too: enter image description here

share|improve this question

2 Answers 2

Ok, try with this code:

    private static DateTime GetModified(SPListItem item)
    {
        var properties = item.Properties;
        const string mycustomField = "LastRevision";
        var startDate = Convert.ToDateTime(item["StartDate"].ToString());

        if (!properties.ContainsKey(mycustomField))
            return startDate;

        string lastRevision = properties[mycustomField].ToString();
        DateTime dt;

        if (DateTime.TryParse(lastRevision, out dt))
            return dt;

        return startDate;
    }

if you use an item from a library you can get the metadata info by the properties of the item.

share|improve this answer

Looks like you are getting a null value in your date field, add some error checking on your parser... (Or use Convert.ToDateTime(string) which adds it for you)

DateTime.Parse only contains:

// Reflected code
internal static DateTime Parse(string s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
{
    if (!DateTimeParse.TryParse(s, dtfi, styles, ref result1))
    {
        throw DateTimeParse.GetDateTimeParseException(ref result1);
    }
        return result1.parsedDate;
    }
}

So if you try and parse the time data yourself by calling DateTimeParse.TryParse, I would then set the value to:

new DateTime((long) 0);

But as I said Convert.ToDateTime(string) handles this check for you.

[UPDATE]

To get the value of a field properly as a type:

DateTime fldModified = (DateTime)item.Fields.GetFieldByInternalName("CustomDateField");

Try that, I just tested on mine works fine.

share|improve this answer
    
it is not null the value inside but the system doesn't find the column! –  Gaetanu Nov 8 '12 at 9:30
    
@Gaetanu In that case you must be using the wrong internal name for the column. Edit the field and check the internal name for LastRevision. –  Hugh Wood Nov 8 '12 at 9:33
    
it's correct; I got it by powershell –  Gaetanu Nov 8 '12 at 9:37
    
@Gaetanu there must be a reason why the field isn't being found. But surely your LastRevision column is no different to Modified? Try checking the list with SharePoint Manager, and having a look at the field in detail. spm.codeplex.com/releases/view/51438 –  Hugh Wood Nov 8 '12 at 9:49
1  
Excuse me @Gaetanu, is it an item in a list or in a library? Did you try to use the Properties property? –  Salvatore Di Fazio Nov 8 '12 at 14:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.