mechalaris
mechalaris

Reputation: 161

SSIS 2008 Script in C#: need to convert Y2K string to date format and use it to populate variable

I am trying to get records from an AS400 system into Dynamics CRM programatically. To achieve this i have pushed the AS400 records into a SQL table and am able to push those records to CRM by referencing the CRM 4 web service endpoints in a SSIS 2008 C# script.

The problem is one of the fields is in Y2K date string format. In order to get it into a date field (D.O.B) in CRM i believe i will need to convert it to a date format then reference resulting value in a variable.

I do not know how to do this.

This question/answer (http://stackoverflow.com/a/4880021/1326443) may help with part of the question but i do not know how to use this into my script to get a value (haven't done any scripting for a number of years and new to C#)

Script snippet:

public class ScriptMain : UserComponent
{
private CrmService service = null;


public override void PreExecute()
{
    base.PreExecute();


    CrmAuthenticationToken token = new CrmAuthenticationToken();
    token.AuthenticationType = 0;
    token.OrganizationName = "DevOrg";

    service = new CrmService();
    service.Url = "http://crm/mscrmservices/2007/crmservice.asmx";
    service.CrmAuthenticationTokenValue = token;
    service.Credentials = System.Net.CredentialCache.DefaultCredentials;
}

public override void PostExecute()
{
    base.PostExecute();

}

public override void LeadInput_ProcessInputRow(LeadInputBuffer Row)
{
    lead cont = new lead();

if (!Row.TITL20_IsNull)

{
        cont.salutation = Row.TITL20;
    }


if (!Row.DOBI20_IsNull)

{
            cont.new_birthdate = Row.DOBI20;
        }

....

....

            service.Create(cont);
        }

    }
}

{ cont.new_birthdate = Row.DOBI20; } throws:

cannot implicitly convert type 'string' to .....CrmSdk.CRMDateTime

Upvotes: 3

Views: 456

Answers (1)

Mutation Person
Mutation Person

Reputation: 30500

Just had a look at the documentation for CRMDateTime (http://msdn.microsoft.com/en-us/library/bb928935.aspx)

This states that you can set this using the Value property (http://msdn.microsoft.com/en-us/library/bb928944.aspx)

So you might like to try:

cont.new_birthdate.Value = Row.DOBI20

Edit

In response to your comments, try the following

string ConvertDate(string dateToConvert)
{ 
    dateToConvert= dateToConvert.PadLeft(7, '0');
    int c;
    int.TryParse(dateToConvert.Substring(0,1), out c);  
    c = (c * 100) + 1900;

    int y;
    int.TryParse(dateToConvert.Substring(1,2), out y);  

    return (c+y).ToString() + dateToConvert.Substring(3,4);
}

Upvotes: 1

Related Questions