Blogger Widgets
  • Sharing Photos using SignalR
  • TFS Extenstion - allows copy work items between projects
  • Displaying jquery progressbar with ajax call on a modal dialog
  • Managing windows services of a server via a website
  • Exploring technologies available to date. TechCipher is one place that any professional would like to visit, either to get an overview or to have better understanding.

Search This Blog

Wednesday, 24 August 2011

Formatting date in XSL using .NET standard formatting

Microsoft standard .NET date format can be used in xsl transform which is a key feature provided using msxsl:script Element.

The following snippet demonstrates using .NET "ToShortDateString" inside XSL transform:-


  
  

Above defined method "ToCSharpShortDateString" can be used as follows:-



Now that xsl is ready, lets use this with ASP.NET xml control . Code behind should look something like this.

XmlDocument doc = new Document(xmlPath);
 XPathNavigator nav = doc.CreateNavigator();
xmlData.XPathNavigator = nav;
string xslFileName = Server.MapPath("~/InvoiceReport.xsl");
xmlData.TransformSource = xslFileName;
xmlData.DataBind();


Technological progress has merely provided us with more efficient means for going backwards. ~Aldous Huxley

Thursday, 18 August 2011

jqGrid error at line $t.p.colModel[lvc].width += cr;

Recently I am getting an error in jqGrid plugin at the following line :-

$t.p.colModel[lvc].width += cr;


After debugging through the code this issue occurs with in the function
setGridWidth: function (nwidth, shrink)
.....
$t.p.colModel[lvc].width += cr;
.....

where nwidth is null

So while calling method setGridWidth on a grid make sure the width is a definite value and is greater than 0

Control is the wrong word. The practice is very much about sharing, and, in any creative practice, some individuals, whether partners or directors, are much closer to certain projects than I could ever be.
Norman Foster

Friday, 12 August 2011

Using SQL Server Compact with FluentNHibernate

FluentNHibernate is an alternative to NHibernate that supports Object Relational Mapping framework. Actually FluentNHibernate is build on top of NHibernate that provides strongly typed mapping using c# as opposed to xml mapping (.hbm.xml).

SQL Server CE (Compact edition) is commonly used for small applications as this would not need any packaging and is a simple file copy.

Using FluentNHibernate with SQL Server CE 3.5 file has a flaw in which any changes to mapping would not be reflected in the database unless the database is re-created.

Now this has been solved in the latest version of SQL Server CE 4.0, where in any changes are automatically reflected in the database.

Here is code to configure SQL Server CE
MsSqlCeConfiguration sqlconfig = MsSqlCeConfiguration.Standard.ConnectionString(ConnectionString);
FluentConfiguration fc = Fluently.Configure();
fc = fc.Database(sqlconfig );
ISessionFactory sessionFactory = fc.Cache(c => c
   .UseQueryCache()
   .ProviderClass())
   .Mappings(m => m
   .FluentMappings.AddFromAssemblyOf())
   .ExposeConfiguration((NHibernate.Cfg.Configuration config) => new SchemaUpdate(config)
   .Execute(false, true))
   .BuildSessionFactory();




Telegraphs are machines for conveying information over extensive lines with great rapidity.
Charles Babbage


JQGrid extension to track row selection between pages

Have been using JQGrid for quite some time now and was pretty impressed with the jqGrid plugin itself.

Ok now, how about extending jqGrid for tracking selection between pages. Here is a jquery extension that does the job.

; (function($) {
    $.jgrid.extend({
        enableTracking : true,
        gridTrackingIds : null,  
        TrackPageSelection: function (Rowids, selected, currentpageno) {
            var currentPage = this.GetCurrentPage(currentpageno);
            currentPage.RowIDs = new Array();
            if(selected)
            {
                currentPage.RowIDs = Rowids;
            }
            this.UpdateSelection(currentPage);
        }, 
        GetCurrentPage: function(currentpageno){
            var selectedPages = $(this).jqGrid.gridTrackingIds;

            var currentGrid = this;
            if (selectedPages == null) {
                selectedPages = new Array();
            }

            //find current page
            var currentPage = null;
            $.each(selectedPages, function (i, item) {
                if(item.Page == currentpageno){
                    currentPage = item;
                }
            });

            if(currentPage == null)
            {
                currentPage = {
                    Page : currentpageno,
                    RowIDs : new Array()
                };
                selectedPages.push(currentPage);
            }

            $(this).jqGrid.gridTrackingIds = selectedPages;
            return currentPage;
        },
        UpdateSelection: function(currentPage){
            var selectedPages = $(this).jqGrid.gridTrackingIds;

            var currentGrid = this;
            if (selectedPages == null) {
                selectedPages = new Array();
            }

            var filteredPages = $.grep(selectedPages, function(elem, index) { 
		            return elem.Page != currentPage.Page;
	            });

            selectedPages = filteredPages;
            selectedPages.push(currentPage);

            $(this).jqGrid.gridTrackingIds = selectedPages;

        },
        TrackSelection: function (id, selected, currentpageno) {
            var selectedItems = $(this).jqGrid.gridTrackingIds;            
            //find current page
           var currentPage = this.GetCurrentPage(currentpageno);

           if(selected){                
                var itemIndex = $.inArray(id, currentPage.RowIDs);
                if(itemIndex == -1){
		            currentPage.RowIDs.push(id);
	            }
            }
            else{
                var filteredItems = $.grep(currentPage.RowIDs, function(elem, index) { 
		            return elem !== id;
	            });
	            currentPage.RowIDs = filteredItems;
	        }
            this.UpdateSelection(currentPage);                            
        },
        UpdatePageSelection: function(currentpageno){            
            var selectedItems = $(this).jqGrid.gridTrackingIds;
            if(selectedPages != null)
            {
                //find current page
                var currentPage = this.GetCurrentPage(currentpageno);                
                if(currentPage != null)
                {
                    var selectedItems = currentPage.RowIDs;
                    var currentGrid = this;
                    if(selectedItems != null) {
                        $.each(currentGrid.jqGrid("getDataIDs"), function (i, id) {
                            if($.inArray(id, selectedItems) > -1) {
                                currentGrid.setSelection(id, false);
                            }
                        });                        
                    }
                }
            }
        }
        ResetTracking: function(){
            $(this).jqGrid.gridTrackingIds = null;            
        }
    });
})(jQuery);



All you need is to just copy this into a .js file and add it to your project. Now on row select just call this

onSelectRow: function(rowid,selected)
{
  var pageno = $(this).getGridParam('page');
  $(this).TrackSelection(rowid,selected,pageno);   
}




At each increase of knowledge, as well as on the contrivance of every new tool, human labour becomes abridged.
Charles Babbage

Tuesday, 9 August 2011

JQGrid sorting with date and time column using localization

Have been using JQGrid for quite some time now and have got stuck with unable to sort on DateTime columns. But finally managed a way to be able to handle this. Not only to allow sorting but also should be able use localization and display/sort in current local format.

Not use if this is the right way and works for me. The various DateTime formats available are listed at DateTime formats. Assuming the default date format is "The General Date Short Time ("g") Format Specifier".

DateTimeFormatInfo dateformatInfo = Thread.CurrentThread.CurrentCulture.DateTimeFormat;
string dateFormat = dateformatInfo.ShortDatePattern;

Now that we know the dateformat we are going to use is defined in a variable "dateFormat". Convert this date format string to JQGrid format.
public string ConverToJQGridDateFormat(string DateFormat)
{ 
            // Day
            DateFormat = DateFormat.Replace("dddd", "d");
            DateFormat = DateFormat.Replace("ddd", "d");
            DateFormat = DateFormat.Replace("dd", "d");
            // Month            
            DateFormat = DateFormat.Replace("MMMM", "M");
            DateFormat = DateFormat.Replace("MMM", "M");
            DateFormat = DateFormat.Replace("MM", "m");
            DateFormat = DateFormat.Replace("M", "m");
            // Year
            DateFormat = DateFormat.Replace("yyyy", "Y");
            DateFormat = DateFormat.Replace("yyy", "y");
            DateFormat = DateFormat.Replace("yy", "y");
            return DateFormat;
}


Ok now we have got our JQGrid date format, just apply this to colmodel options. Finally the column for "Invoice date" should be rendered as

name: 'InvDate',index: 'InvDate',sorttype:'date',formatter:'date',formatoptions:{srcformat:'d/m/Y',newformat:'d/m/Y'}
Another mode of accumulating power arises from lifting a weight and then allowing it to fall.
- Charles Babbage


Wednesday, 27 July 2011

ASP.NET MVC with SQL SERVER giving error "The SELECT permission was denied on the object 'account', database 'accounting', schema 'dbo'"

Developed an ASP.NET mvc application and ready to publish. Now steps anyone would have generally followed :-

1. Create an new application pool "AccountingAppPool" with ".NET Framework version" as v4.0.30319
2. Assign "Identity" as "NetworkService"
3. Publish the web application to http://localhost/Accounting/
4. Assign application pool as "AccountingAppPool"
5. Now check web.config

for connection string
   
    

.....


.....

6. using SQL server management studio connect to database. Add "NT AUTHORITY\NETWORK SERVICE" to logins to allow access for the identity of application pool

7. All done just run the application


Now a very disappointing error
"The SELECT permission was denied on the object 'account', database 'accounting', schema 'dbo'"

User is able to connect to database but does not have access. Ok now this is frustrating. Having spent hours thinking why this is not working.

Finally after a lot of help, found the issue is actually because of impersonation.

Now modified web.config to :-


Hurray! it worked.

To have no errors
Would be life without meaning
No struggle, no joy
~Brian M. Porter, 1998

Thursday, 23 June 2011

Using jQuery tabs with ASP.NET MVC

Developing websites using ASP.NET webforms does have its benefits. Now why not give a try with ASP.NET MVC. Ok Here is a simple article that describes about using jquery tabs for MVC application.

As everyone know ASP.NET MVC template provides menu options for various actions, now why not provide the same options with jquery tabs. Here is how it compares both

Standard MVC template


MVC template with jquery tabs


Now let look into details of how this can be achieved. First add the required references for jquery scripts as follows inside head tag of site.master





As with ASP.NET MVC what we need is
- Model
- View
- Controller

Let's first start with creating the required model data required for our JQuery tabs. Here is what we need to achive:-

1) Model for storing data for a tab panel
2) Model for maintaining list of tab panels (ie.. tabs)

Remember to create these classes under Models folder.

1) Model for storing data for a tab panel
Create JQueryTabPanel data model for tab related information and its relevant controller action as follows:-

JQueryTabPanel.cs
namespace MvcApplication.Models
{
    public class JQueryTabPanel
    {
        public string TabPanelID { get; set; }
        public string TabPanelTitle { get; set; }
        public string TabPanelAction { get; set; }
        public string TabPanelController { get; set; }

        public JQueryTabPanel(string PanelID, string PanelTitle, string PanelAction, string ControllerName)
        {
            this.TabPanelID = PanelID;
            this.TabPanelTitle = PanelTitle;
            this.TabPanelAction = PanelAction;
            this.TabPanelController = ControllerName;
        }
    }
}

2) Model for maintaining list of tab panels (ie.. tabs)
Create JQueryTabs data model for tab related information and its relevant controller action as follows:-

JQueryTabs.cs
namespace MvcApplication.Models
{
    public class JQueryTabs
    {
        public List JQueryTabPanels { get; set; }

        public JQueryTabs()
        {
            JQueryTabPanels = new List();
        }
    }
}

So far we have created the required data model, now let move on to creating the respective views. Create the following view under Shared folder ( as it will be common for the entire web application).

JQueryTabsControl.ascx

Finally lets update the controller. As these tabs are common for all controllers lets create a base controller as follows :-

BaseController.cs
namespace MvcApplication
{
    public class BaseController : Controller
    {

        public BaseController()
        {
            JQueryTabs tabs = new JQueryTabs();
            tabs.JQueryTabPanels.Add(new JQueryTabPanel("1", "Home", "Index", "Home"));
            tabs.JQueryTabPanels.Add(new JQueryTabPanel("2", "About", "About", "Home"));

            ViewData["JQueryTabs"] = tabs;
        }

    }
}

Now modidy site.master so as to render the JQueryTabsControl.

site.master

Also add jquery script to populate the tabs and select the respective tab based on the controller action.

common.js


That's it now you should have a working model of ASP.NET MVC using jquery tabs.


The real danger is not that computers will begin to think like men, but that men will begin to think like computers. ~Sydney J. Harris
Copyright © 2013 Template Doctor . Designed by Malith Madushanka - Cool Blogger Tutorials | Code by CBT | Images by by HQ Wallpapers