Friday, October 19, 2012

Getting Entity Framework Exception Details

Sunday, October 7, 2012

Could not load file or assembly 'SqlTrackingParticipant' or one of its dependencies. An attempt was made to load a program with an incorrect format

Problem Statement:   Could not load file or assembly 'SqlTrackingParticipant' or one of its dependencies. An attempt was made to load a program with an incorrect format

Solution :  CLR is unable to load the assembly because the assembly is not complied in X64 platform. Change it to "Any CPU" using "Right Click on the Project -> Properties -> Build -> Platform target.

Friday, October 5, 2012

highlighting a row in JqGrid based on a column value

function hightlightGridRows(grid, colName, valueToCheck) {     
    var grid_ids = grid.jqGrid('getDataIDs');
    var rowcount = grid_ids.length;
    var columnNames = grid.jqGrid('getGridParam', 'colNames');
    
    $.each(columnNames, function (i, v) {
        columnNames[i] = v.toUpperCase();
    });

    var colNameInUpper = colName.toUpperCase();

    if ($.inArray(colNameInUpper, columnNames) > -1) {
        for (var counter = 0; counter < rowcount; counter++) {
            var rowid = grid_ids[counter];
            var aRow = grid.jqGrid('getRowData', rowid);
            var celldata = grid.getCell(rowid, colName);

            if (celldata == valueToCheck) {
                var trElement = $("#" + rowid, grid);
                trElement.removeClass('ui-widget-content');
                trElement.addClass("gridHighlightRow");
            }
        }
    }
    else {
        alert("Invalid column name to evaluate the condition for highlighting rows in the grid");
    }

}
where
ui-widget-content = It is css class that is applied by default to a JqGrid row.
gridHighlightRow = It is the custom css class to highlight grid row.
References: http://tpeczek.codeplex.com/

Update an item of a querystring using Jquery

function updateQuerystring(str, itemToReplace, value) {
    if (str != null) {
        var pairs = str.split('&');
        $.each(pairs, function (i, v) {
            if (v.indexOf(itemToReplace) >= 0) {
                pairs[i] = itemToReplace + "=" + value;
            }
        });
        str = '';
        $.each(pairs, function (i, v) {
            if (pairs[i].indexOf("?") >= 0)
                str = pairs[i];
            else
                str = str.concat("&" + pairs[i]);
        });
    }

    return str;
}

Extension class for IEnumerable( Of T)

public static class IEnumerableExtensions
    {
        /// 
        /// ForEach extension method for IEnumerable(Of T)
        /// 
        /// The element type of the IEnumerable object
        /// The IEnumerable(Of T) source
        /// The delegate to perform on each element of the IEnumerable(Of T)
        public static void ForEach(this IEnumerable source, Action action)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            foreach (T item in source)
            {
                action(item);
            }
        }

        /// 
        /// Converts an IEnumerable(Of T) to a delimited string
        /// 
        /// The element type of the IEnumerable object
        /// The IEnumerable(Of T) object
        /// The delimiter to be used in the output string
        /// Delimited string representation of the items
        /// Null or empty items are ignored
        public static string ToDelimitedString(this IEnumerable items, string separator)
        {
            if (items == null || items.Count() == 0)
            {
                return string.Empty;
            }

            return string.Join(separator, items.Where(i => i != null && !i.ToString().IsNullOrWhiteSpace()));
        }

        /// 
        /// Converts an IEnumerable(Of T) to a comma-delimited string
        /// 
        /// The element type of the IEnumerable object
        /// The IEnumerable(Of T) object
        /// Comma-delimited string representation of the items
        /// Null or empty items are ignored
        public static string ToSpaceDelimitedString(this IEnumerable items)
        {
            return items.ToDelimitedString(" ");
        }

        /// 
        /// Converts an IEnumerable(Of T) to a space-delimited string
        /// 
        /// The element type of the IEnumerable object
        /// The IEnumerable(Of T) object
        /// Space-delimited string representation of the items
        /// Null or empty items are ignored
        public static string ToCommaDelimitedString(this IEnumerable items)
        {
            return items.ToDelimitedString(",");
        }
    }

Masking SSN in C#

 public static string MaskSSN(string ssn)
        {
            string pattern = @"(?:\d{3})-(?:\d{2})-(\d{4})";
            return (null != ssn) ? Regex.Replace(ssn, pattern, "XXX-XX-$1") : string.Empty;
        }

Storing files in DB in asp.net MVC

CSHTML
@using (Html.BeginForm("SaveFile", "MyController", FormMethod.Post, new { enctype = "multipart/form-data", id = "fileAjaxForm", autocomplete="off" }))
{

}

Controller
public JsonResult SaveFile(HttpPostedFileBase resumeFile)
{  
   if (resumeFile != null && resumeFile.ContentLength > 0)
   {
        int size = resumeFile.ContentLength;
        byte[] fileData = resumeFile.InputStream.Read(fileData, 0, size);
        DataAccess.SaveFileData(Path.GetFileName(resumeFile.FileName),resumeFile.ContentType, fileData)  
   }
}
DB
Field Name Type
FileName nvarchar(255)
Content Type nvarchar(255)
Content image

What’s New in EF5

EF 5 includes a number of new features and bug fixes to the EF4.3 release. Most of the new features are only available in applications targeting .NET 4.5, see the Compatibility section for more details.
  1. Enum support allows you to have enum properties in your entity classes.
  2. Spatial data types can now be exposed in your model using the DbGeography and DbGeometry types.
  3. The Performance enhancements –
    3.1 Automatic Compilation of Linq to Entities queries.
    3.2 67% increase in performance over 4.0
  4. Code First will now detect if you have LocalDb or SQL Express available for creating new databases. Visual Studio 2012 includes LocalDb, whereas Visual Studio 2010 includes SQL Express.
  5. Code First will add tables to existing database if the target database doesn’t contain any of the tables from the model. The EF Designer in Visual Studio 2012 also has some new features:
  6. DbContext code generation for new models means that any new models created using the EF Designer will generate a derived DbContext and POCO classes by default. You can always revert to ObjectContext code generation if needed. Existing models will not automatically change to DbContext code generation.
  7. Multiple-diagrams per model allow you to have several diagrams that visualize subsections of your overall model. Shapes on the design surface can also have coloring applied.
  8. Table-Valued functions in an existing database can now be added to your model.
  9. Batch import of stored procedures allows multiple stored procedures to be added to the model during model creation.