barrycarey / SnipeSharp

A c# wrapper for the Snipe IT API
25 stars 30 forks source link

Unable to add PurchaseDate to a new asset #16

Closed azmcnutt closed 5 years ago

azmcnutt commented 5 years ago

I think I must be doing something wrong and it is probably extremely obvious what I should be doing, but I seem to be having trouble adding a PurchaseDate to a new asset.

I've got everything else to submit (Name, Serial, Asset Tag, etc.). Here is what I have tried:

asset.PurchaseDate = "2018-08-08"; // cannot implicitly convert string to SnipeSharp.Common.ResponseDate

asset.PurchaseDate.Format = "2018-08-08"; // System.NullReferenceException: 'Object reference not set to an instance of an object.'

DateTime dt = new DateTime(2018,08,08); asset.PurchaseDate = dt; // cannot implicitly convert DateTime to SnipeSharp.Common.ResponseDate

Can someone point me in the right direction?

Thanks, James

cofl commented 5 years ago

Sorry if this reply isn't relevant to you anymore (and I haven't tested this), but would

public static SnipeSharp.Common.ResponseDate ConvertSnipeSharpDate(DateTime date)
{
    return new SnipeSharp.Common.ResponseDate
    {
        DateTime = date.ToString("yyyy-MM-dd HH:mm:ss"),
        Formatted = date.ToString("yyyy-MM-dd hh:mm:ss tt")
    };
}

// ...

asset.PurchaseDate = ConvertSnipeSharpDate(new DateTime(2018, 08, 08));

work?

azmcnutt commented 5 years ago

Hi cofl. Thanks for the tip. I did get it working.

Probably not the best, but this is what ended up working for me:

ResponseDate dt = new ResponseDate();
dt.DateTime = (dataGridSnipeData.Rows[i].Cells["snipeData"].Value.ToString());
asset.PurchaseDate = dt;

Where dataGridSnipeData.Rows[i].Cells["snipeData"].Value.ToString() Is a text date in the yyyy-MM-dd format already. It is fed from a GUI Date Field.

I'll convert to your method though. Looks much cleaner and easier to understand in in the code window.

Thanks, James