Wednesday, June 17, 2015

Modify LabelFor to display an * for required fields

We are going to create a html helper method in MVC to automatically show an * for required fields.


Create a class let say ReqLabel

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

namespace YourNameSpace.Models
{
public static class ReqLabel
{
    public static MvcHtmlString ReqLabelFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, object htmlAttributes)
    {
        var metaData = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);

        string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
        string labelText = metaData.DisplayName ?? metaData.PropertyName ?? htmlFieldName.Split('.').Last();

        if (metaData.IsRequired)
            labelText += "*";

        if (String.IsNullOrEmpty(labelText))
            return MvcHtmlString.Empty;

        var label = new TagBuilder("label");
        label.Attributes.Add("for", helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName));

        foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(htmlAttributes))
        {
            label.MergeAttribute(prop.Name.Replace('_', '-'), prop.GetValue(htmlAttributes).ToString(), true);
        }

        label.InnerHtml = labelText;
        return MvcHtmlString.Create(label.ToString());
    }

}
}

don't forget at add name of your name space to your class


Now you can use this new helper method inside your views as follows

include the namespace in your view

@using YourNameSpace.Models



and use ReqLabelFor instead of LabelFor

@Html.ReqLabelFor(model => model.Category, new { @class = "control-label col-md-3" })




Friday, March 27, 2015

Table Variable vs Temp Table, which one to prefer and when

Yesterday I came accross a situation where one of the stored procedure written by my collegue started misbehaving all of a sudden, It was becomming non responsive to a point where we were inserting a record into a table variable from a CTE query.

On further analysis I found that everything was okay with the query untill the point it tries to insert records into the table variable. I replaced that table variable with a temp table and everything went smoothly. That's when I thought to make a writeup on this topic for which one to use, and when, cause both are have some pross and cons.


  • For larger datasets use temp table. If your number of records are going to be greater than 100 use temp table, micrsoft recommends
  • Indexes cannot be created explicitly on table variables and no statistics are kept on table variable, so use temp tables if want to improve performance by indexes and supported statistics
  • Since table variables are still a varibales they do not participate in transactions, you can use them at a place where you want to log progress of large SQL batch
  • Queries that modify table variables do not generate parallel query execution plans. Performance can be affected when very large table variables, or table variables in complex queries, are modified. In these situations, consider using temporary tables instead
  • On the other hand when using fewer number of records use table variable instead cause it create less overhead on the database, and may need less or no in case they are stored in memory, because of being smaller in size. 
  • If you are frequently adding and deleting records from it then use temporay tables cause they support truncate command which has better performance than delete
  • If the source for the data inserted to the table is from a potentially expensive SELECT statement then consider that using a table variable will block the possibility of this using a parallel plan.
  • When using a #temp table within a user transaction, locks are held longer than for table variables and also it can prevent truncation of the tempdb transaction log until the user transaction ends. So this might favour the use of table variables
  • A table varibale cannot be altered after creation, however you you can alter the structure of a temp table by using an alter table command    

Tuesday, March 24, 2015

What data type we should preferably use for storing flag values in sql server

This question came to my mind some time back, when I was working on a project with a database having billions of records, spanned accross 20 financial years. I saw that for most of the flag fields Int data type was used. I was curious to know that if size of field impacts the performace of database opeartions. I posted a question for the same on stackoverflow and finally I found the solution by the help of a link provided by a stack overflow member.

What I found as answer is that we should consider using smallest data type sufficient enough to store flag values required. This is with respect to the performance of database as storage size is not a big concern these days.

Have a look on this link for more insights.

Rows and index entries are stored in 8k pages. So a million rows at 3 bytes per row isn't 3 MB on disk: it affects the number of rows per page ("page density").


The same applies to nvarchar to varchar, smalldatetime to datetime, int to tinyint etc

This article describes how the dataype and rowsize matters for DML operations in a database.


Wednesday, March 4, 2015

WCF Training

A Good article published by Microsoft.







Problem of SQL Server Ignoring seconds and milisecons at the time of implicit conversion

Hi Guys,

SQL server has the feature of implicit conversion of data types at the time of inserting records into a table.

I want to take your attention here on a specific issue related to varchar to datetime data type conversion.
When we insert data into a DATETIME or SMALLDATETIME column, SQL Server automatically attempts to convert the data if it is of a different type. 

For example, if you insert a varchar value into a DATETIME column, SQL Server will convert the data – if the value is in an  acceptable format.
Sometimes you will notice that this implicit conversion has ignored the values of seconds and milisecons, and inserted zero instead.To cope up with this situation you need to explicitly convert the field value using Convert or Cast function.

Friday, November 16, 2012

How to permanently delete a file from Team Foundation server

tf destroy $/proj/proj/proj/projEntities.Designer.cs /login:admin,abc@123 /collection:http://server:8080/tfs/proj

Friday, February 3, 2012

Deploying a .Net crystal report website on a shared server

Hi Guys,

Deploying a crystal report website on a shared server has really been a very difficult task. I have been using rdlc reports upto now for my web applications, but due to the limitations they have I had to use crystal report to one of my web applications, that's when I had to learn this thing, and also realised that many of the guys are struggling with this issue and some of them are not able to sort this thing out for months/years. Though there so many deployment issues which is not possible for me to list out, but here I am writting steps to deploy a crystal report site, which you can use as a primary guideline for deploying a crystal report website.

1. Put the following DLLs to your BIN directory at your shared server.

CrystalDecisions.CrystalReports.Engine
CrystalDecisions.Shared
CrystalDecisions.ReportSource

2. Install the appropriate crystal report redistributable(the redistributable applicable for the version you are using) at server.

3. Give the IIS user account and asp.net worker process account full control for the reports folder.

4. Make sure to give 'everyone' full control to the temp directory of windows.

5. for more information and support log on to http://forums.sdn.sap.com/index.jspa.

wish you all the best for your deployment.