views:

54

answers:

5

I'm working on a biztalk project and use a map to create the new message.

Now i want to map a datefield to a string.

I thought i can do it on this way with an Function Script with inline C#

public string convertDateTime(DateTime param) { return System.Xml.XmlConvert.ToString(param,ÿyyyMMdd"); }

But this doesn't work and i receive an error. How can i do the convert in the map?

It's a Biztalk 2006 project.

A: 

Check out this thread.

http://stackoverflow.com/questions/336226/string-to-datetime-conversion-in-c

A: 

FWIW, we always pass in the /text() into the C# functions and then the C# function will always expect, and return a string data type

<CreateDate>
    <xsl:value-of select="userCSharp:GetDateyyyyMMdd(string(s0:StatusIdChangeDate/text()))" />
</CreateDate>

And then the C# script

<msxsl:script language="C#" implements-prefix="userCSharp">
<![CDATA[
public System.String GetDateyyyyMMdd(System.String p_DateTime)
{
    return System.DateTime.Parse(p_DateTime).ToString("yyyyMMdd");
}
]]>

Edit : This is BTS 2009 though

nonnb
A: 

If you're using the mapper, you just need a Scripting Functiod (yes, using inline C#) and you should be able to do:

public string convertDateTime(DateTime param)
{
    return(param.ToString("YYYYMMdd");
}

As far as I know, you don't need to call the System.Xml namespace in anyway.

AllenG
A: 

I'd suggest

public static string DateToString(DateTime dateValue)
{
    return String.Format("{0:yyyyMMdd}", dateValue);
}

You could also create a external Lib which would provide more flexibility and reusability:

public static string DateToString(DateTime dateValue, string formatPicture)
{
    string format = formatPicture;

    if (IsNullOrEmptyString(formatPicture)
    {
        format = "{0:yyyyMMdd}";
    }

    return String.Format(format, dateValue);
}

public static string DateToString(DateTime dateValue)
{
    return DateToString(dateValue, null);
}

I tend to move every function I use twice inside an inline script into an external lib. Iit will give you well tested code for all edge cases your data may provide because it's eays to create tests for these external lib functions whereas it's hard to do good testing on inline scripts in maps.

Filburt
A: 

Without the details of the error you are seeing it is hard to be sure but I'm quite sure that your map is failing because all the parameters within the BizTalk XSLT engine are passed as strings1.

When I try to run something like the function you provided as inline C# I get the following error:

Object of type 'System.String' cannot be converted to type 'System.DateTime'

Replace your inline C# with something like the following:

public string ConvertDateTime(string param1)
{
    DateTime inputDate = DateTime.Parse(param1);
    return inputDate.ToString("yyyyMMdd");
}

Note that the parameter type is now string, and you can then convert that to a DateTime and perform your string format.

As other answers have suggested, it may be better to put this helper method into an external class - that way you can get your code under test to deal with edge cases, and you also get some reuse.


1 The fact that all parameters in the BizTalk XSLT are strings can be the source of a lot of gotchas - one other common one is with math calculations. If you return numeric values from your scripting functoids BizTalk will helpfully convert them to strings to map them to the outbound schema but will not so helpfully perform some very random rounding on the resulting values. Converting the return values to strings yourself within the C# will remove this risk and give you the expected results.

David Hall

related questions