Using SkipIf with Aspose words for .Net

Aspose words for .Net doesn’t support word field skipif to skip some merge fields between tablestart and tableend i,e skipping rows based on some condition.

Here, I’ll explain how to include skipif common functionality  with custom merge field in the word document.
The custom skipif merge field syntax should be as below

skipif:tablename:condition  

where condition must be a valid t-sql where clause and use ~ for spaces if any. Skipif can be used at any position and for any table. If you use multiple skipif’s for the same table, the last skipif will be considered ignoring all the previous skipif’s.

Ex:

if the table name is productstable (provided with tablestart: and tableend:)

skipif:productstable:price>0   (or in alt f9 view, { MERGEFIELD skipif:productstable:price>0 } )

skipif:productstable:price~in~(100,200,300,400)

1) Define a method ApplyFilters that takes two parameters, data set and aspose document.


DataSet ApplyFilters(DataSet ds, Document doc)
        {
            var fieldNames = doc.MailMerge.GetFieldNames().ToList();
            var newDs = ds.Clone();
            var skipIfs = fieldNames.Where(x => x.ToLower().StartsWith("skipif"));
            if (skipIfs.Any())
            {
                foreach (DataTable table in ds.Tables)
                {
                    foreach (var s in skipIfs)
                    {
                        var sp = s.Split(":".ToCharArray());
                        if (sp.Length > 2)
                        {
                            var where = sp[2];
                            if (!String.IsNullOrWhiteSpace(where))
                            {
                                DataTable newTable;
                                where = where.Replace("~", " ");
                                var skipRows = table.Select(where);
                                var rows = table.AsEnumerable().Except(skipRows);
                                var dataRows = rows as List ?? rows.ToList();
                                if (dataRows.Any())
                                {
                                    newTable = dataRows.CopyToDataTable();
                                }
                                else
                                {
                                    table.Clear();
                                    newTable = table;
                                }
                                newTable.TableName = table.TableName;
                                newDs.Tables.Remove(newTable.TableName);
                                newDs.Tables.Add(newTable);
                            }
                        }
                    }
                }
            }
            return newDs;
        }

2) Define a class that inherits IFieldMergingCallback and implement FieldMerging method


public class HandleSkipIfField : IFieldMergingCallback
    {
        void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
        {
            if (!String.IsNullOrWhiteSpace(args.TableName))
            {
                var fieldName = args.FieldName;
                if(fieldName.ToLower().StartsWith("skipif"))
                {
                    args.Text = "";
                }
            }
        }

        void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
        {
            // Do Nothing
        }
    }

3) Add below code before firing executewithregions


//Add below 2 lines before executewithregions
doc.MailMerge.FieldMergingCallback = new HandleSkipIfField();
var dataSource = ApplyFilters(ds, doc);


doc.MailMerge.ExecuteWithRegions(dataSource);

Passed MCTS .Net Framework 4 Web Applications (70-515)

Smile  Yesterday, I passed MCTS .Net framework 4 (70-515), Web applications(VS2010) with a score of 850/1000. My exam is of 51 questions. The study materials I referred are MSDN documentation  Microsoft Self Paced Training Kit 70-515, JQuery Documentation. The training kit book explains each individual topic in a simple way although most of the contents you can find in MSDN documentation(not organised same as the book).

if you want to pass in the first attempt, focus on the following 

  • 15% nearly 5 questions on Jquery (focus on Jquery core selectors, attributes, Css, Form, Manipulation, Traversing, AJAX) and 2 on Ajax($.ajax)
  • Configuration/Deployment (Web.config structure , focus on all the System.Web elements, Project references, publishing websites using Web Deploy, Publish tools, Generic config replacement)
  • ASP.Net Ajax(UpdatePanels, Timer, UpdateProgress, Postback and asychronous triggers, UpdatePanel modes )
  • 20% Web Forms(Page Declaration attributes, Content) and Controls(User controls, Web Controls, Custom Controls, Templates, Server Controls and HTML Controls etc, Data Binding.), Validation Controls.
  • Application, Page and Control life cycles
  • Master Pages, Themes, Skins, Stylesheets, Scripts, Site Navigation, Menus
  • 2 questions on HttpModules, HttpHandlers( asynchronous too) and configuration in IIS6, IIS7 (Classic and Integrated modes)
  • Security, Authorization and Authentication( Windows and Forms Authentication, Roles, Memberships, Profiles etc)
  • 10% on MVC2 (Controllers, Areas, Constraints, Defaults, ActionResult and its types, Views and Strong typed Views, Models, MVC structure)
  • Globalization and Localization (Local and Global resources, culture formats, meta:resourceKey)
  • Configuring and Updating WCF and ASMX service references and proxies.
  • Caching(5%, focus on Cache.Insert with absolute and sliding expirations, dependencies, OutputCache), Session state, Application States, ViewState, Control State
  • Data related  SQLDataSource, ObjectDataSource, LinqDataSource, XMLDataSource,EntityDataSource and Parameters(Select, Where, Insert, Delete, Update, QueryString etc), Linq, Entity Framework.

Also the new topics in ASP.Net 4 like Dynamic Data (namely scaffolding ), new Web.Config changes etc..

This is my second attempt for MCTS Web applications, My last attempt was in December 2009 for MCTS .Net framework 3.5 certification(based on VS2008 which requires two exams 70-536 and 70-562). I passed 70-536 exam but failed 70-562 exam with a score of 650/1000 where as pass score is 700.

ASP.Net 4.0 BlogEngine.Net A potentially dangerous Request.Form value

This one related to page save error in BlogEngine.net running under .Net 4.0. When saving edited content in the Administration screen you may get this error.

Raw Url : /admin/Pages/Pages.aspx?id=6a2f6007-b224-48f3-94f9-0b4af750668f
Message : A potentially dangerous Request.Form value was detected from
the client (ctl00$cphAdmin$txtContent$TinyMCE1$txtContent=". 

ASP.Net validates page request data to protect against any possible XSS attacks. In ASP.Net 2.0, request validation is enabled for only ASP.Net pages and validated when those pages are executing. Whereas in ASP.Net 4.0, by default request validation is enabled for all requests. As a result validation applies to not only to ASP.Net pages but also to the Web service calls, Http handlers etc.. To prevent this error simply revert ASP.Net behaviour back to 2.0. To do this, add a configuration element in Admin Web.Config.

<httpRuntime requestValidationMode="2.0" /

Visual Studio 2010 and .Net Framework 4 Training Kit

This training kit is designed to help you learn how to utilize visual studio 2010 and framework technologies. it includes presentations, hands-on labs and demos of the following framework technologies

  • C# 4
  • ASP.Net 4
  • Windows 7
  • Entity Framework
  • ADO.Net Data Services
  • Managed Extensibility Framework
  • Windows Communication Foundation
  • Windows Workflow
  • Windows Presentation Foundation

This kit works with Visual Studio 2010 and .Net Framework 4 only. It’ll walk you step by step through loads of content and is a great starter if you’re getting into what’s new in .NET 4.

You can download kit from here Visual Studio 2010 and .Net Framework 4 Training kit