dotnetable

Experiences of working in software development with .NET.

ZenCoding Visual Studio plug in Overview

The ZenCoding extension for Visual Studio can be installed by itself or as part of the WebEssentials plug-in bundle, see also WebEssentials website. In ZenCoding you use a shorthand to describe a block of html, with the cursor at the end of the line hitting the TAB key will generate the full html code. Getting down to business, here is a list of the formatting characters and their meaning.

# Indicates text defines an element id
> Secified a child element
+ A sibling element
. css class(es)
*n Element repeats n times
[] Explicitly specify atrribute.
{xxx} Explicit text content
$ Numeric increment

Example 1 – Element Id
So as a first example, type this in to Visual Studio and with the cursor at the end of the line hit the TAB key.

div#sidePanel

And the following HTML is produced

<div id="sidePanel"></div>

That is pretty much all there is to it. For brevity the following examples show the shorthand to type followed by the HTML generated when the TAB key is pressed (when the cursor is at the end of the line).

Example 2 – Explicit Attributes

img[alt='logo image']

<img src="" alt="logo image" />

Exampe 3 – Css Class

span.heading

<span class="heading"></span>

For multiple css classes the [] can be used

img[class='logo tooltip-flyout']
or specify each class using the dot notation
img.logo.tooltip-flyout

<img src="" alt="" class="logo tooltip-flyout" />

Example 4 – Repeating Elements

ul#features>li*3

<ul id="features">
    <li></li>
    <li></li>
    <li></li>
</ul

Example 5 – Explicit Text

label{submit}

<label>submit</label>

Example 6 – Putting It All Together

div#sidePanel>span.heading+img.logo[alt='logo image']+ul#features>li*3>a{link $}

<div id="sidePanel">
    <span class="heading"></span>
    <img src="" alt="logo image" class="logo" />
    <ul id="features">
        <li><a href="">link 1</a></li>
        <li><a href="">link 2</a></li>
        <li><a href="">link 3</a></li>
    </ul>
</div>

There are a few special features built in, Generation of lorem ipsum text, Lorem Pixel Code and PlaceHold.it blank images.

Example 7 – Generating Lorem Ipsum Text
Here we specify 5 list items each containing 4 words.

ul>li*5>lorem4

<ul>
    <li>Amet, consectetur adipiscing elit.</li>
    <li>Fusce vel sapien elit.</li>
    <li>In malesuada semper mi.</li>
    <li>Id sollicitudin urna fermentum.</li>
    <li>Ut fusce varius nisl.</li>
</ul>

Example 8 – Lorem Pixel Code
Lorempixel is a free image place holder service. Useful for mocking up layouts where you do not have all your own images yet. Below are a number of Zencoding examples. There are more features like greyscale images and images with text, see the lorempixel.com site for a full list

A completely random image with a default 30 by 30 pixel size.

pix

<img src="http://lorempixel.com/30/30/" alt="" />

A random image of a specific but equal with and height.

pix-200

<img src="http://lorempixel.com/200/200/" alt="" />

A random image with different width and height

pix-200x100

<img src="http://lorempixel.com/200/100/" alt="" />

An image of a specific but equal with and height from a specific category. A random image index will be selected from those in the category, in this case 7, though this can be changed if another image fits your needs. There are generally 20 images per category currently though the odd category may have less than 20 images.

pix-200-animals

<img src="http://lorempixel.com/200/200/animals/7/" alt="" />

Mixing in some of the other Zencoding shorthand. A random image with an ALT attribute and specific CSS class.

pix[alt='product image'].borderless

<img src="http://lorempixel.com/30/30/" alt="product image" class="borderless" />

Example 9 – PlaceHold.it Images
Placeholder.com is another image service with similar features. The big difference with this service is the images are grey tone with the image size as text which is quite useful for when you are mocking up layouts containing a number of different image sizes.

A basic image, defaults to 30 by 30 pixels.

place

<img src="http://placehold.it/30x30/" alt="" />

An image with a specific but equal width and height.

place-200

<img src="http://placehold.it/200x200/" alt="" />

An image of a specific but different width and height

place-200x100

<img src="http://placehold.it/200x100/" alt="" />

Mixing in some of the other Zencoding shorthand. A random image with an ALT attribute and specific CSS class.

place[alt='product image'].borderless

<img src="http://placehold.it/30x30/" alt="product image" class="borderless" />

ZenCoding is one of those things where you either love it and tend to use it a lot or don’t see the point in it and so never use it. In the latter case I can see how taking the time to learn and use ZenCoding, which can take a few attempts when trying to create a specific formatted HTML layout, takes more time than just typing (i.e. Copy/Paste) the desired html code in the first place. On the Visual Studio Marketplace ZenCoding pages some people have mentioned how the plug-in does not work in the Community 2017 edition. All the above examples were created using VS2017 Community edition, apart from a few bugs (highlighted below) I can say it does work.

Below are some examples of bugs I have experienced when using Zencoding in MVC views in VS2017 Community edition, although they have worked with Web Forms.

ul>li*3>a

The above produces half a job by formatting the link
ul>li*3><a href="#">content</a>

where I was hoping for an un-ordered list of 3 list items, with each containing a link.
<ul>
	<li><a href=""></a></li>
	<li><a href=""></a></li>
	<li><a href=""></a></li>
</ul>

Altough in MVC Razor view the following works.

ul>li*3>a.referenceLink

<ul>
    <li><a href="" class="referenceLink"></a></li>
    <li><a href="" class="referenceLink"></a></li>
    <li><a href="" class="referenceLink"></a></li>
</ul>

Trying to generate a list with explicit text fails but in a different way.

div>ul>li*3{abc}

<div>
    <ul></ul>
</div>

For me, the Zencoding plug in is a simple tool that enhances my coding time.

Advertisements

Searching text of all stored procsedures in MSSQL

Here are a few useful SQL statements I use occasionally, so occasionally that I have to keep searching to dig them out of their “safe place”.
All these scripts have been written with MSSQL in mind, the where clause can easily be modified to change how matches are found, match exactly, match containing etc.

/*************************************************
 List all stored procedures containing given text 
*************************************************/

--method 1
SELECT DISTINCT so.name 
FROM syscomments sc 
INNER JOIN sysobjects so on sc.id=so.id WHERE sc.text LIKE '%amr%'

--method 2
SELECT o.name,o.type
FROM sys.sql_modules sm
INNER JOIN sys.objects o ON
    o.object_id = sm.object_id
WHERE sm.definition LIKE '%amr%'


/*************************************************
 List all tables with partially matching columns
*************************************************/

select so.name, sc.name from syscolumns sc 
INNER JOIN sysobjects so on sc.id=so.id WHERE sc.name like '%turnover%'


/*************************************************
 Use built in views to get schema information
*************************************************/

SELECT * FROM information_schema.tables
ORDER BY table_name

SELECT * FROM information_schema.columns
where column_name like '%amr%'
ORDER BY table_name, ordinal_position

Using EntityFramework 6 Part 2: Database Connections

This post is part of a mini series on using EntityFramework (EF). If you do not have EF installed go to Part 1: Introduction and Setup to see what (basic) steps we took to get EF installed.
In this part we are going to use Code-First methods to start model building, then eventually connecting to the database . This is not the only way and you do not have to do things in exactly the same order as I have but if you follow along you should have a working version at the end.

Database
Firstly, create a database at your preferred location. Once you have a database add the connection string to the web.config file. I added a database to the App_Data folder so this is what the connectionsString definition look like in my web.config file.

  <connectionStrings>
    <add name="DBConn" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\DB.mdf;Integrated Security=True"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

Next, add a class which inherits from DBContext. An instance of DBContext, LibraryDBContext in this example, is used to perform CRUD operations against a database. To read up on DBContext see these articles
http://www.entityframeworktutorial.net/EntityFramework4.3/dbcontext-vs-objectcontext.aspx

namespace WebApplication2.Models
{
    public class LibraryDBContext:DbContext
    {
        public LibraryDBContext() : base("DBConn")
        { }

        public DbSet<Book>Books{ get; set; }
    }
}

This gives sufficient Entity Framework code to enable our application to access a database.

The Model
Now we will start the model by creating a Book entity. Create a Book class in the Models folder of your application.

namespace WebApplication2.Models
{
    public class Book
    {
        public int BookId { get; set; }
        public string Author { get; set; }
        public string Title { get; set; }
    }
}

Following Code-First conventions, because we have defined an property called BookId (class name ‘Book’ plus ‘Id’) Entity Framework will nominate this property as an identity column when the database tables are generated. If we had a property simply named Id, Entity Framework would have nominated that as the identity and created an identity column called Id.

Displaying Book Info
Add a Controller to the application, I have called it BooksController. The Index action retrieves all the books from what will be the Books table.

using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using WebApplication2.Models;

namespace WebApplication2.Controllers
{
    public class BooksController : Controller
    {
          public ActionResult Index()
        {
            var books = new List<Book>();
            using (var db = new LibraryDBContext())
            {
                books = db.Books.ToList();
            }
            return View(books);
        }
    }
}

The last code related change is to create a View for the Index Action to display all the books.

@model IEnumerable<WebApplication2.Models.Book>
    @Html.ActionLink("Create New", "Create")
<table class="table">
<tr>
<th>
            @Html.DisplayNameFor(model => model.Author)</th>
<th>
            @Html.DisplayNameFor(model => model.Title)</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
            @Html.DisplayFor(modelItem => item.Author)</td>
<td>
            @Html.DisplayFor(modelItem => item.Title)</td>
<td>
            @Html.ActionLink("Edit", "Edit", new { id=item.BookId }) |
            @Html.ActionLink("Details", "Details", new { id=item.BookId }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.BookId })</td>
</tr>
}</table>

Running the Application
The application should build at this point. Currently we have a database that contains no tables. If you run the application now The database/tables will only be created/modified on a read/write action. So run the application and navigate to your controller (e.g. \books). Because the Index action of the Books controller contains code that retrieves data via the inherited DBContext class, and because Entity Framework knows there are models with no corresponding database table, the tables will be created with columns types that match the model properties.

Using EntityFramework 6 Part 1: Introduction and Setup

In this mini series we will be looking at how to implement EntityFramework 6 (from now on referred to as EF) into an MVC application, primarily using Code First methods. In this first part we are going to set up our project.  If you already have a project ready skip to the Setup EF section. If you already have EF version 6 installed feel free to go to part 2: Putting Database Connections into Context.

New MVC Project

I was going to lay out the steps required to create a new project. However, I assume you are able to complete that without step-by-step instructions. Depending on which options you select when creating a new project will determine whether you need to install EF. If you selected an internet application with individual user accounts then EF will have been installed during project creation with a DataContext used for user authentication.

Setup EntityFramework
There are two primary ways to install a NuGet package. By using PowerShell commands in the Package Manager Console or by using a visual interface.
To use the Package Manager Console go to Tools > Nuget Package Manager > Package Manager Console. At the NuGet command line enter this command to install EF

Install-Package EntityFramework -projectname DemoApplication

The -projectname switch forces EF to be installed in a specific project. If you have only one project in your solution you can omit the parameter and use

Install-Package EntityFramework

Either way you should see a message confirming EF has been successfully installed.

To use the visual interface go to Tools > NuGet Package Manager > Manage NuGet Packages for Solution… to open the interface.  On the left select Online and in the search box on the right enter entity framework.

nuget-entityframework6

I already have EF installed which is why you see a green circle with a tick.  If it is not installed then you will see an install button.
Once you have EF installed you are ready to continue.
 

Changing JSON properties to camel case notation

A quick note on how to change the JSON property names to camel notation. This is achieved by adding the following lines to the WebApiConfig class.

var settings = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
settings.Formatting = Formatting.Indented;

This is how it would look in the class.

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Web.Http;

namespace Carrera
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            var settings = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;
            settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            settings.Formatting = Formatting.Indented;

            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}

Simple ASP.Net Gridview Sorting and Paging Example

The simplest way to perform paging and sorting in an ASP.Net webforms application is to use the SQLDatasource control. If you do not use the suite of DataSource controls paging and sorting can manually be achieved without too much extra work.

The page contains a GridView control that has paging and sorting enabled by setting AllowPaging and AllowSorting attributes to true. Height and width are optional.

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" Height="292px" 
OnDataBound="GridView1_DataBound" OnPageIndexChanging="GridView1_PageIndexChanging" OnSorting="GridView1_Sorting" 
Width="600px"></asp:GridView>

As I am not using any data access methods, I have created a private method to generate and return a DataTable of data.

private DataTable GetTableData()
{
	DataTable dt = new DataTable();
	dt.Columns.Add("ProductId", typeof(int));
	dt.Columns.Add("Name", typeof(string));
	dt.Columns.Add("ProductNumber", typeof(string));
	dt.Columns.Add("Quantity", typeof(int));
	dt.Columns.Add("UnitPrice", typeof(decimal));

	Random rnd = new Random();
	for (var i = 1; i <= 100; i++)
	{
		var qty = rnd.Next(1, 100);
		DataRow dr = dt.NewRow();
		dr["ProductId"] = i;
		dr["Name"] = $"Product{i}";
		dr["ProductNumber"] = $"A-{qty}";
		dr["Quantity"] = qty;
		dr["UnitPrice"] = i;
		dt.Rows.Add(dr);
	}
	return dt;
}

The webform has two properties that are used as accessors to values stored in viewstate, the name of the sort column and the sort direction.

public string SortColumn
{
	get {return Convert.ToString(ViewState["SortColumn"]);}
	set {ViewState["SortColumn"] = value;}
}

public string SortDirection
{
	get { return Convert.ToString(ViewState["SortDirection"]); }
	set { ViewState["SortDirection"] = value; }
}

In the page load method, if the page load was not from a postback the default sort direction and sort column is set and the BindGrid method is called.

protected void Page_Load(object sender, EventArgs e)
{
	if (!Page.IsPostBack)
	{
		SortDirection = "ASC";
		SortColumn = "ProductId";
		BindGrid();
	}
}

The Bind Grid method calls the GetTableData method to generate the data and the data is bound to the grid.

private void BindGrid()
{
	var dt = GetTableData();
	if (dt != null)
	{
		//Sort the data.
		dt.DefaultView.Sort = SortColumn + " " + SortDirection;
		GridView1.DataSource = dt;
		GridView1.DataBind();
	}
}

The PageIndexChanging event is raised when one of the pager options is clicked, but before the GridView handles the paging operation. Notice the BindGrid method is called causing the grid to be rebound to the data.

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
	GridView1.PageIndex = e.NewPageIndex;
	BindGrid();
}

The Sorting event is raised when the column header link is clicked, but before the GridView control handles the sort operation. Here the sort expression, in this case the data column to sort by and the sort order are set. The data is then rebound by calling the BindGrid method.

		
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
	SortDirection = (SortDirection == "ASC") ? "DESC" : "ASC";
	SortColumn = e.SortExpression ;
	BindGrid();
}

The DataBound event fires when all the databinding for the GridView is finished. All we do here is find the index of the GridView column that matches the sort expression and add an image to the column header to indicate the current sort direction.

protected void GridView1_DataBound(object sender, EventArgs e)
{
	int columnIndex = 0;
	foreach (DataControlFieldHeaderCell headerCell in GridView1.HeaderRow.Cells)
	{
		if (headerCell.ContainingField.SortExpression == SortColumn)
		{
			columnIndex = GridView1.HeaderRow.Cells.GetCellIndex(headerCell);
			break;
		}
	}

	Image sortImage = new Image();
	sortImage.ImageUrl = string.Format("images/sort-{0}ending.png", SortDirection);
	GridView1.HeaderRow.Cells[columnIndex].Controls.Add(sortImage);
}

That is pretty much it.

Using HtmlHelpers to generate Custom TextArea (Part 2)

This is Part 2 in a (very) mini series on HtmlHelper methods. Here is the link for Part 1, Using HtmlHelpers to generate HTML elements

This article is about creating a HtmlHelper for a TextArea although the same principles apply for other input controls as well. For this example I have created a simple Employee model class.

namespace HtmlHelpersDemo.Models
{
    public class Employee
    {
        public string Title { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

Nothing special about the controller. An Employee object is instantiated and passed to the view. Following MVC conventions the Index view from the Home folder is rendered.

using HtmlHelpersDemo.Models;
using System.Web.Mvc;

namespace HtmlHelpersDemo.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new Employee());
        }
    }
}

Before looking at the new TextAreaFor HtmlHelper method we will look at how it is called. The method is written as an extension to the Mvc HtmlHelper class (System.Web.Mvc) which allows it to be called the same way the built in TextAreaFor methods are called using @Html..
The first example passes values in a htmlattributes parameter in the form of a class and a data_ref attribute which has an underscore rather than hyphen.

@model HtmlHelpersDemo.Models.Employee
@using HtmlHelpersDemo.Code;
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>

@Html.TextAreaFor(m => m.FirstName, new { @class = "input" , data_ref="123"}, false)

@Html.TextAreaFor(m => m.FirstName,  false)

Here is the code that does all the work. As mentioned this is written as a HtmlHelper extension method so the first parameter is not actually passed in from the calling code. Rather it defines the class the method is associated with and provides us with a local reference to manipulate, in this instance the local name of HtmlHelper is htmlHelper.

using System;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Routing;

namespace HtmlHelpersDemo.Code
{
    public static class TextAreaExtensions
    {
        public static MvcHtmlString TextAreaFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression, object htmlAttributes, bool IsReadonly)
        {
            var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
			attributes["class"] = "form-control" + " " + attributes["class"];

            if (IsReadonly)
            {
                attributes.Add("readonly", IsReadonly);
            }

            MvcHtmlString html = default(MvcHtmlString);
            RouteValueDictionary routeValues = new RouteValueDictionary(attributes);
            html = System.Web.Mvc.Html.TextAreaExtensions.TextAreaFor(htmlHelper, expression, routeValues);
			TextAreaFor()
            return html;
        }

        public static MvcHtmlString TextAreaFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression, bool IsReadonly)
        {
            return htmlHelper.TextAreaFor(expression, null, IsReadonly);
        }
    }
}

Lets disect what the method is doing. The first line calls a built in method passing in the htmlAttributes parameter. This method replaces underscore characters with hyphens which is how the data_ref is translated to data-ref when rendered.

var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);

As all text area controls is this application requires the form-control css class, it is being added to the attributes in this method along with any other css class passed in throught the htmlattributes parameter.

attributes["class"] = "form-control" + 
    (!string.IsNullOrEmpty( attributes["class"] as string) ? " "+ attributes["class"] : "");

If the IsReadOnly parameter is set, the readonly state of the control is assigned to the attribues object.

if (IsReadonly)
{
	attributes.Add("readonly", IsReadonly);
}

The final four rows create a TextArea input control from the attributes and route values if there are any.

MvcHtmlString html = default(MvcHtmlString);
RouteValueDictionary routeValues = new RouteValueDictionary(attributes);
html = System.Web.Mvc.Html.TextAreaExtensions.TextAreaFor(htmlHelper, expression, routeValues);
return html;

There rendered html for the two examples above looks like this.

<textarea class="form-control input" cols="20" data-ref="123" id="FirstName" name="FirstName" rows="2">
</textarea>

<textarea class="form-control" cols="20" id="FirstName" name="FirstName" rows="2">
</textarea>

Using HtmlHelpers to generate HTML elements (Part 1)

When working in a Microsoft MVC application, creating forms containing form elements is made easier due to the number of available HtmlHelper methods for a TextBox, DropDownList, TextArea etc. Although HtmlHelpers are not only available for form controls, there are helpers for links, view rendering, validation and more built in to the MVC libraries.

At some point there will not be a HtmlHelper that does just what is needed and want to share a few examples of customer HtmlHelpers.

Example 1. Label Helper
This example generates html label page elements. Creating the label is as simple as building a string containing the html to be displayed on the page.

using System.Web.Mvc;

namespace HtmlHelpersDemo.Code
{
    public static class Helpers
    {
        public static MvcHtmlString Label(string labelText)
        {
            return new MvcHtmlString(string.Format("<label>{0}</label>", labelText));
        }

        public static MvcHtmlString Label(string labelText, string forAssociatdControl)
        {
            return new MvcHtmlString(string.Format("<label for='{0}'>{1}</label>", forAssociatdControl, labelText));
        }
    }
}

These methods return an instance of type MvcHtmlString class which represents an HTML-encoded string. MvcHtmlString is a member of System.Web.Mvc namespace (in System.Web.Mvc.dll).

Usage
To display a label we add a using statement at the beginning of our view.

@using HtmlHelpersDemo.Code;
@{
    ViewBag.Title = "Home Page";
}

@Helpers.Label("First Name:")

@Helpers.Label("Last Name:", "SomeControl")

When rendered the page contains the labels as expected.
html-helper-label-code

This is quite a simple example but the method could be applied to creating a html table row or even the whole table.

Date Formatting and Calling C# Script From XSLT in BizTalk 2013 Map

I needed to set a field value to a formatted date string in a map. As we are using BizTalk 2013R1 we are restricted to XSLT1.0 which makes date formatting more difficult (XSLT2.0 has better support for dates). However this got me thinking of the different ways this could be achieved. For demonstartion purposes lets say I have the below schema

biztalk-date-functoid
the last three fields are the fields of interest and are set up with the data types as in this table

Field Data Type
ApprovedOn xs:dateTime
CreatedDate xs:date
CreatedTime xs:time

Setting the ApprovedOn field to the current datetime value is quite straight forward with a C# scripting functoid.

biztalk-date-functoid-linked
Setting the CreatedDate and CreatedTime fields can be done in a similar way and just requires a C# scripting functoid for each field that returns the date in the relevant format.

The CreatedDate C# scripting functoid

public string CurrentDate()
{
    return DateTime.Now.ToString("dd-MM-yyyy");
}

The CreatedTime C# scripting functoid

public string CurrentTime()
{
    return DateTime.Now.ToString("HH:mm:ss");
}

This is the result of testing the map with a valid input file.

<Employee>
  <ForeName>Percy</ForeName> 
  <ApprovedOn>29/09/2016 11:59:43</ApprovedOn> 
  <CreatedDate>29-09-2016</CreatedDate> 
  <CreatedTime>11:59:43</CreatedTime> 
</Employee>

Moving on to the orignal problem I was trying to solve
The destination schema contains an element which itself contains three child elements

biztalk-employeeoutput-schema

defined with the following data types

Field Data Type
Date xs:date
Time xs:time
Type xs:string

In the real life scenario the Type field is calculated from a number of fields in the source schema. To keep this example focused I have hard coded the Type field using a String Concatenate functoid. The output of the Concatenate is an input parameter to a scripting functoid that builds the Action destination element using an Xslt template. The value for the Type field could have been hardcoded in the scripting functoid but linking this way demonstrates parameter handling in an Xslt template.

biztalk-map-complete

So here we have a scripting funcoid (with the exclamation warning) containing inline C# code that consists of a singel FormatCurrentDateTime method.  This method has a single string parameter which will format the returned string.

public string FormatCurrentDateTime(string format)
{
    return DateTime.Now.ToString(format);
}

BizTalk adds the C# script in a block at the bottom of the xslt when the map is compiled. This can be seen by compiling the assembly containing maps, and using the Show all Files command in Visual Studio Solution Explorer to look at the corresponding .btm.cs file to see what has been added.
There is also a scripting functoid containing an xsl template. This template accepts a single parameter for the Type element. There are also two variables initialised by calling the FormatCurrentDateTime method passing in a string defining the format of the DATETIME returned.  Note the use of userCSharp: prefix on the method call.

<xsl:template name="mapActionTemplate">
    <xsl:param name="type" />
    <xsl:variable name="date" select="userCSharp:FormatCurrentDateTime('yyyy-MM-dd')"/>
    <xsl:variable name="time" select="userCSharp:FormatCurrentDateTime('HH:mm:ss')"/>
        <xsl:element name="Action">
            <xsl:attribute name="Type">
                <xsl:value-of select="$type" />
            </xsl:attribute>
            <Date>
                <xsl:value-of select="$date" />
            </Date>
            <Time>
                <xsl:value-of select="$time" />
            </Time>
        </xsl:element>
</xsl:template>

For additional information on maps see this series of articles on Bizbert.

Credit to the original article:
Useful Cheat Code in Mapping (Calling C# code from XSLT) « BizTalk Server Tutorial

JavaScript Confirm on ASP.NET Form Submit

This is a quick demo showing how to prompt the user to confirm when submitting a form on an ASP.Net page.  We start with a simple form.

form

There is some server side code which will populate a label with the current date and time when the form is submitted.

protected void SubmitButton_Click(object sender, EventArgs e)
{
    Timestamp.Text = DateTime.Now.ToString();
}

If the user is simply required to confirm the submitting of the form then an OnClientClick action can be specified.

<asp:Button runat="server" ID="SubmitButton" Text="Submit" CssClass="btn btn-default" OnClientClick="return confirm('Do you want to submit this page?')" CausesValidation="false" OnClick="SubmitButton_Click" />

When the form is submitted it will display the confirmation prompt.
confirm

If the Cancel button is pressed the form will not be submitted. If the OK button is pressed the form will be submitted and the current date and time will be displayed in a label.

form-submitted

The form in this example very simple. In a real application forms are usually more complicated with fields that require validation. Suppose we only want to prompt the user to confirm the form submission if the form is validated. This is still simple to do with very minor changes, the trick is to check the status of the validation on the client side first by calling the Page_ClientValidate method.

<div class="form-group">
    <asp:Label runat="server" AssociatedControlID="Name" CssClass="col-md-2 control-label">Name</asp:Label>
<div class="col-md-10">
        <asp:TextBox runat="server" ID="Name" CssClass="form-control" />
        <asp:RequiredFieldValidator runat="server" ControlToValidate="Name" CssClass="text-danger" ErrorMessage="Name is required." />
    </div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
        <asp:Button runat="server" ID="SubmitButton" Text="Submit" CssClass="btn btn-default" OnClientClick="if (Page_ClientValidate()){return confirm('Do you want to submit this page?')}" CausesValidation="false" OnClick="SubmitButton_Click" />
    </div>
</div>

With this code the form is not valid until the Name textbox contains a value. Only when all the validation controls are valid will the Confirm prompt be displayed. As before, if the Cancel button is pressed the form will not be submitted. If the OK button is pressed the form will be submitted and the current date and time will be displayed in a label.

BizTalk musings

Issues, patterns and useful tips for BizTalk development

Sandro Pereira BizTalk Blog

My notes about BizTalk Server 2004, 2006, 2006 R2, 2009, 2010, 2013 and now also Windows Azure BizTalk Services.

Exercises in .NET with Andras Nemes

Tips and tricks in C# .NET

WordPress.com

WordPress.com is the best place for your personal blog or business site.

%d bloggers like this: