Wednesday, November 6, 2013

SQL Server : Context_Info

 

While working one of my project, need for connection specific setting required to enable and disable functionality. Project has very large database in terms of entities and many of entities have triggers that affect other entity and also done task for sending notification.

Sometime there is need to disable certain trigger required during data mining work without affecting application that use data.

While searching for solution to that issue I came across Context_Info functionality available in SQL Server.It is varbinary field and its value available only for particular connection session.

You can use it following way.

Step 1. SET CONTEXT_INFO 0x1

Step 2. If 0x01 = Context_Info()
    Print ‘ContextInfo Set’
else
    print ‘ContextInfo Not Set’

If you have SQL Server Management Studio then open two new query window.

In first window put code specified in Step1 and Step2 and In Second window only put code specified in Step 2.

image

You can also use above functionality during coding as well. You just have to call that function with value. Following is the sample with C#. ( Test application that I created for this)

 class SqlContextInfoSample
{
public void ExecuteSample()
{
using (var conn = GetConnection())
{
SetContextInfomationToConnection(conn);
Console.WriteLine(GetContextInformationFromConnection(conn));
}

using (var conn = GetConnection())
{
Console.WriteLine(GetContextInformationFromConnection(conn));
}
}

SqlConnection GetConnection()
{
var conn = new SqlConnection();
conn.ConnectionString = @"<>";
conn.Open();
return conn;
}

void SetContextInfomationToConnection(SqlConnection conn)
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "SET CONTEXT_INFO 0x1";
cmd.ExecuteNonQuery();
}
}

string GetContextInformationFromConnection(SqlConnection conn)
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "Select CONTEXT_INFO()";
object result = cmd.ExecuteScalar();
return result.ToString();
}
}
}

Hope this will help.

Tuesday, November 5, 2013

SQL Server 2012 : OFFSET FETCH VS CTE WITH ROW_NUMBER()

 

Since SQL Server 2005 and 2008 ROW_NUMBER() Function available and many of us will us that function with CTE to get data by custom page size.

In SQL Server 2012 there is new functionality to get record by page size and it is known as OFFSET FETCH.

While Learning SQL Server 2012 Syntax for OFFSET and FETCH clause I found that this will not work if you are specify order by clause in Select query. So I decided to check ROW_NUMBER() with CTE to new functionality in terms of performance.

For this example AdventureWorks_2012 database in used.

Old Way Of Query ( Version 2005 , 2008)

WITH CTE
AS
(
Select AccountNumber , P.FirstName , P.LastName , P.MiddleName , ROW_NUMBER() OVER (ORDER BY P.BusinessEntityId) RowNum from Sales.Customer SC
LEFT JOIN Person.Person P ON SC.PersonID = P.BusinessEntityID
WHERE AccountNumber Like 'A%'
)
Select AccountNumber , FirstName , LastName , MiddleName from CTE WHERE RowNum > 1000 and RowNum <= 2000

SQL Server 2012

Select AccountNumber , P.FirstName , P.LastName , P.MiddleName   from Sales.Customer SC
LEFT JOIN Person.Person P ON SC.PersonID = P.BusinessEntityID
WHERE AccountNumber Like 'A%'
ORDER BY P.BusinessEntityID
OFFSET 1000 ROWS
FETCH NEXT 100 Rows ONLY

At this point I thought there must be a performance improvement in SQL Server 2012. To check that I used execution plan and statics IO.


Execution Plan ( Both Query Took 50%)


image


STATISTICS IO ( Both are same)


image


As per my thinking there is no performance improvement and it is just syntax get change ( Internally In SQL Engine both query are same as they generated same execution plan)


Please give me your opinion regarding this and correct me If I am wrong somewhere to understand functionality.

Monday, May 14, 2012

ASP.net MVC generic client and server side validation

In ASP.net Webform has CustomValidator which can call any javascript function for validation and which make our task is easy in terms of validation. During ASP.net MVC development we have validation via “DataAnnotation Attribute” for validation. All default attribute support most of our requirement but sometime need for custom validation.ASP.net MVC support this via Custom Attribute development but when this lead us to develop to many validation attribute for our need.

Now here i am going to show you one generic validation attribute implement which call any javascript function ( client side validation) or static function ( server side validation). Here i am assuming that ASP.net MVC 3 or later used which Unobtrusive validation true for project.

To develop custom validation attribute create class that inherits from ValidationAttribute and implement IClientValidatable interface.

C# Code 


public class JSFunctionValidateAttribute : ValidationAttribute, IClientValidatable
{
/// <summary>
/// Ctor: Javascript functionname is mandatory and serversideStaticFunction is not mandatory.
/// </summary>
/// <param name="javascriptFunctioname"></param>
/// <param name="serverSideStaticFunction"></param>
public JSFunctionValidateAttribute(string javascriptFunctioname,string serverSideStaticFunction = "")
:base()
{
JavaScriptFunctionName = javascriptFunctioname;
ServerSideFunction = serverSideStaticFunction;
}

/// <summary>
/// Javascript function that need to call
/// </summary>
public string JavaScriptFunctionName { get; set; }

/// <summary>
/// Server side validation.
/// </summary>
public string ServerSideFunction { get; set; }

/// <summary>
/// This function is used to call server side validation function.
/// </summary>
/// <param name="value"></param>
/// <param name="validationContext"></param>
/// <returns></returns>
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (!string.IsNullOrEmpty(ServerSideFunction))
            {
                string className = ServerSideFunction.Substring(0, ServerSideFunction.LastIndexOf('.'));
                string functionName = ServerSideFunction.Substring(ServerSideFunction.LastIndexOf('.')+1);
                Type staticclassMethod = Type.GetType(className);
                MethodInfo info = staticclassMethod.GetMethod(functionName, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
                object result = info.Invoke(null ,new object[]{ value, validationContext , ErrorMessage });
                if (result != null)
                {
                    return (ValidationResult)result;
                }
            }           
            return null;
        }

/// <summary>
/// This generate client side unobtrusive validation.
/// </summary>
/// <param name="metadata"></param>
/// <param name="context"></param>
/// <returns></returns>
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(
ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule();
rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName());
rule.ValidationType = "jsfunctionvalidation";
rule.ValidationParameters.Add("jsfunctionname", JavaScriptFunctionName);
yield return rule;
}
}

In above code two important thing.

1. For client side validation

 public IEnumerable<ModelClientValidationRule> GetClientValidationRules(
ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule();
rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName());
rule.ValidationType = "jsfunctionvalidation";
rule.ValidationParameters.Add("jsfunctionname", JavaScriptFunctionName);
yield return rule;
}

ValidationType is “jsfunctionvalidation” and validation parameter contain “jsfunctionname” which contain name of javascript function to call.


Now register for unobstrusive validation to Jquery validation adaptor.

jQuery.validator.addMethod("jsfunctionvalidation", function (value, element, param) {  
return window[param](value, element, param);
});

jQuery.validator.unobtrusive.adapters.add("jsfunctionvalidation", ["jsfunctionname"], function (options) {
options.rules["jsfunctionvalidation"] = options.params.jsfunctionname;
options.messages["jsfunctionvalidation"] = options.message;
});

2. For Server-side validation

protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (!string.IsNullOrEmpty(ServerSideFunction))
            {
                string className = ServerSideFunction.Substring(0, ServerSideFunction.LastIndexOf('.'));
                string functionName = ServerSideFunction.Substring(ServerSideFunction.LastIndexOf('.')+1);
                Type staticclassMethod = Type.GetType(className);
                MethodInfo info = staticclassMethod.GetMethod(functionName, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
                object result = info.Invoke(null ,new object[]{ value, validationContext , ErrorMessage });
                if (result != null)
                {
                    return (ValidationResult)result;
                }
            }           
            return null;
        }

This function call static function for validation via reflection and ServerSideFunction must contain fully qualified name. (Look at example later in this post)


Now look at complete example.


Let say we have to implement that required that Name must start which “C”.

Model class

    /// <summary>
/// Person model.
/// </summary>
public class Person
{
public int Id { get; set; }

[Display(Name = "Full name")]
[Required]
[JSFunctionValidate("NameValidation", "Models.Models.ServerSideValidation.ServerSideNameValidation", ErrorMessage = "Name must start with character 'C'")]
public string Name { get; set; }
}
JSFunctionValidate attribute indicate that need to call “NameValidation” javascript function for client side validation ( you will see sample function later in code) and for server side validation function you need to call “ServerSideNameValidation” static function of static class “Models.Models.ServerSideValidation”.

Server side validation class ( Signature of method must be same and class,method must be static)

    /// <summary>
/// Static server side validation function
/// </summary>
public static class ServerSideValidation
{
public static ValidationResult ServerSideNameValidation(object value,ValidationContext context, string errorMessage)
{
if (value != null)
{
if (value.ToString().StartsWith("C"))
{
return null;
}
}
return new ValidationResult(errorMessage);
}
}

Create View ( Razor View)

@model Models.Models.Person
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/customvalidations.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Person</legend>

<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>

<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<script language="javascript" type="text/javascript">
function NameValidation(value, element, param) {
if (value != null) {
if (value[0] == 'C')
return true;
}
return false;
}
</script>

Highlighted JS code is for client side validation.


Test Controller

public class TestController : Controller
{

public ActionResult Index()
{
return View();
}


public ActionResult Create()
{
return View();
}

[HttpPost]
public ActionResult Create(Models.Person person)
{
try
{
// TODO: Add insert logic here
if (ModelState.IsValid)
{
return RedirectToAction("Index");
}
else
{
return View("Create", person);
}
}
catch
{
return View();
}
}
}

Hope this solution give you your solution. Feel free to give your valuable comment.


Sample Code: Download

Create and Read Excel File Using Oledb provider

When working with excel file for data mining or export data to excel many time required to read excel file and also write file. One can use Oledb Provider to connect existing file or create new excel file for report in .net

1. Get Sheets Name from Excel File

      
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
try
{
string filePath = @"c:\myExcel2012.xlsx";
conn.ConnectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES"";",filePath);
conn.Open();
DataTable dtSheets = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
foreach (DataRow dr in dtSheets.Rows)
{
Console.WriteLine(dr["TABLE_NAME"].ToString()); // Print Sheet Name
}
}
finally
{
if(conn.State == ConnectionState.Open)
conn.Close();
}

2. Read Sheet Data

System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
try
{
string filePath = @"c:\myExcel2012.xlsx";
conn.ConnectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES"";", filePath);
conn.Open();
DataTable dtSheets = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
foreach (DataRow dr in dtSheets.Rows)
{
Console.WriteLine(dr["TABLE_NAME"].ToString());
var cmd = conn.CreateCommand();
cmd.CommandText = "SELECT * FROM " + dr["TABLE_NAME"].ToString();
var reader = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader); // This will load data from excel sheet to datatable.

// your code to work on sheet data.
}
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}

3. Create and Write Data to Excel sheet

            System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
try
{
string pathOfFileToCreate = "c:\newexcel.xlsx";
conn.ConnectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES"";",pathOfFileToCreate);
conn.Open();
var cmd = conn.CreateCommand();
cmd.CommandText = "CREATE TABLE sheet1 (ID INTEGER,NAME NVARCHAR(100))"; // Create Sheet With Name Sheet1
cmd.ExecuteNonQuery();
for (int i = 0; i < 1000; i++) // Sample Data Insert
{
cmd.CommandText = String.Format("INSERT INTO sheet1 (ID,NAME) VALUES({0},'{1}')", i, "Name" + i.ToString());
cmd.ExecuteNonQuery(); // Execute insert query against excel file.
}
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}

 


I feel that above sample code is best way to explain article.

Tuesday, May 1, 2012

DataView ToTable Vs Linq to find distinct

One of my past post i mention that how to find distinct from DataTable (http://dotnetstep.in/find-distinct-value-from-dataset-datatable-in-net). Most of the time need is simple and we need to find out one or two column for distinct column but when need is complex and data is not unique than all columns are used to find distinct value of datatable.

When data in datatable is small is count ( Upto certain thousand) and it contains many duplicate record in that case performance is ok for dataview.ToTable but when no. of row increases performance decrease and in case dataView.ToTable, it is noticeable decrease in performance.
Performance depend on total no of record and total of distinct record that dataset/datatable going to return.

What is the solution ?
If you are using .net framework 3.5 SP1 or later then Linq to DataTable comes to rescue. DataTable.AsEnumerable().Distinct() is much faster than DataView ToTable functionality.
Here is performance matrix on sample data. ( You can see code that i used to measure performance later in this post)
NoOfItem/Distinct Item DataView ToTable Time (Milliseconds) Linq Distinct Time (Milliseconds)
100000/2000 13675 117
200000/2000 27271 231
100000/20000 176101 113
200000/20000 412053 483
100000/80000 777402 179
200000/80000 2249655 207
Seeing above result it is clear that Linq gives much better performance.

C# Code to find distinct using linq.
var items = dt.AsEnumerable().Distinct(System.Data.DataRowComparer.Default).ToList();   

Code used to measure performance.
class Program
    {        
        static System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
        static void Main(string[] args)
        {
            Console.WriteLine(String.Format("{0},{1},{2},{3},{4}", "NoOfItem","Distinctcountwithdataview", "Distinctcountwithdataviewtime", 
                                                                   "WithoutdataviewdistinctCount", 
                                                                   "Withoutdataviewdistinctcounttime"));
            for (int i = 0; i < 1000; i+=100)
            {
                DataTable dtSample = GenerateTable(1000 * i, 80000);
                var withDataView = NormalDataSetWithDataView(dtSample);               
                var withoutDataView = NormalDataSetWithOutDataView(dtSample);
                Console.WriteLine(String.Format("{0},{1},{2},{3},{4}",1000*i,withDataView.Item1,withDataView.Item2,withoutDataView.Item1,withoutDataView.Item2));                
            }
            
            Console.ReadLine();
        }

        private static Tuple<int,long> NormalDataSetWithDataView(DataTable dt)
        {
            StopWatchEnable(watch, true);            
            DataView dv1 = new DataView(dt);
            DataTable dtNew1 = dv1.ToTable(true, "Id", "Name", "Role", "DataColumn1", "DataColumn2", "DataColumn3", "DataColumn4", "DataColumn5", "DataColumn6", "DataColumn7");           
            return new Tuple<int,long>(dtNew1.Rows.Count , StopWatchEnable(watch, false));
        }
        private static Tuple<int, long> NormalDataSetWithOutDataView(DataTable dt)
        {
            StopWatchEnable(watch, true);            
            var items = dt.AsEnumerable().Distinct(System.Data.DataRowComparer.Default).ToList();            
            return new Tuple<int, long>(items.Count, StopWatchEnable(watch, false));
        }

        private static DataTable GenerateTable(int noOfRows, int modules)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Role", typeof(string));
            dt.Columns.Add("DataColumn1", typeof(string));
            dt.Columns.Add("DataColumn2", typeof(string));
            dt.Columns.Add("DataColumn3", typeof(string));
            dt.Columns.Add("DataColumn4", typeof(string));
            dt.Columns.Add("DataColumn5", typeof(string));
            dt.Columns.Add("DataColumn6", typeof(string));
            dt.Columns.Add("DataColumn7", typeof(string));
            for (int i = 0; i < noOfRows; i++)
            {
                int id = i % modules;
                dt.Rows.Add(new object[]
                              { id, 
                                "Test" + id.ToString(), 
                                "Test" + id.ToString(), 
                                "Test" + id.ToString(), 
                                "Test" + id.ToString(), 
                                "Test" + id.ToString(), 
                                "Test" + id.ToString(), 
                                "Test" + id.ToString(), 
                                "Test" + id.ToString(), 
                                "Test" + id.ToString() });
            }
            return dt;
        }

        private static long StopWatchEnable(System.Diagnostics.Stopwatch watch, bool enable)
        {
            if (enable)
            {
                watch.Restart();
                return 0;
            }
            else
            {
                watch.Stop();
                return watch.ElapsedMilliseconds;
            }
        }
    }



Why DataView.ToTable is slow ?
- I found that ToTable functionality implemented generic functionality to compare each type of row and the way compare data and also it return new result set with distinct field.

Note:
Above code consider that need to find distinct from all column. If you want to find based on certain field then implement custom IEqualityComparer for datarow.I haven’t tested but PLinq might give some performance improvement in Linq operation.

Suggestion are must welcome.

Global assembly cache location change in .net 4.0

When .net Framework 4.0 release it comes with one major change and that change is GAC (Global assembly cache) location. Since first version of .net framework release GAC location is fix to %windir%/assembly. This is default location and you can change that location but default installation is always point to above location . ( If you want to know how to change GAC Location manually then look at my post http://dotnetstep.in/change-the-location-of-gac 

In .net 4.0 this default location is change to C:\Windows\Microsoft.net\Assembly

Why this thing is important to know?
1.  As default location is C:\Windows\Assembly we used to drag and drop assembly directly to GAC but this is not the case when work with 4.0
2. It is better to use GACUtil tool for .net 4.0 to install assembly.

Following link contain many information regarding this.
http://stackoverflow.com/questions/2660355/net-4-0-has-a-new-gac-why

Suggestion:
It would be nice if C:\Windows\Microsoft.net\Assembly as same look and feel as older GAC.
as per msdn shell extension is obsolete (http://msdn.microsoft.com/en-us/library/34149zk3.aspx) 

Please provide your input if you have any other thing to add.

Friday, March 9, 2012

Move to funnelweb

Hello Readers,

I am moving to funnelwebblog as my personal blog engine. I am not deleting this blog and you continue to read all my older post over here  as well as new blog ( Move old post over there )

Once i will fully satisfied with new blog engine i will disable this blog and if i not satisfied i will come back to blogspot.

New blog location : http://dotnetstep.in/

Thursday, March 8, 2012

SQL Schema Compare Tool

When Visual Studio 2010 release it comes with very important tool for SQL.

During development and deployment process required to compare source and destination database. I came across this situation many times for this either we have to remember what we changed in source ( development database) and need to migrate destination when bug fixes or changes uploaded to production.

To access this tool go to “SQL”  from Top menu and go inside schema compare as shown image below.

image_634668213190328845

Now it is self describing.

1. Select “New Schema Comparison”
2. Add Source and Target Database
3. Press Compare. It will display all changed Tables,Stored procedure and other db object.
4. This allow you to generate script for this or if you press “Update” it will update to Target DB.

Wednesday, March 7, 2012

VS 2011 Beta UI

VS2011 Beta release on 29th Feb and able to install it today. I found following thing interesting in UI Part.

  • Overall look change to black and white ( Metro UI ) style of icon and color scheme.
  • Second change found in solution explorer. Now when you expand code file like ( .cs or .vb) It expand all classes included in that file. ( Image –1)
  • When you click on particular class or method inside that class, it directly point to that part of code. (Image –1)
    image_634668194606375904
    (Image  - 1)
  • When you working with web-development , you might want to switch the browser for your test. ( Like Open Page in IE , Firefox , Chrome etc). This functionality is not new as it available in older version of studio but it is more user friendly now.
    image_634668194634217496

I am going to add many as I find functionality in studio.

Saturday, August 14, 2010

Set Account As SharePoint System Account


In my previous two post related to SharePoint System Account i explain how to display you custom “DOMAIN/<<UserName>>” instead of “System Account”. But in this post i am going to explain different thing opposite to previous post.

Previous Posts : Post1 Post2

There are two way to achieve this thing.

1. Go To Central Admin Site
2. Security –> Configure Service Accounts
image
3. Select your application pool
4. then Select an account for this component (Select your account)
image

If your account is not already register as manage account then click on “Register new managed account”.
image
Another way to do same thing is

1. Go To Central Administration.
2. Click Application Management
3. Click Manage Web Application
4. Choose your web application
image
5. Click on “User Policy”
6. When “Policy for Web Application” popup open click on “Add User”
image

7. Click next
image

8. In this add users for which you want to display that account as “System Account”. Make sure you have “Account operates as system” checkbox selected.
image 

Let me know your comment and view on this.

SharePoint/System Account Settings


In my previous post about SharePoint system account, i explain that how to configure account so that you can see your DOMAIN/<<UserName>> account as System Account.

But to do so you need SharePoint Central Administrator site permission. There is another way to do same thing using coding. ( Little bit trick :))

There is hidden List called “User Information List” in SharePoint site.You can find system account and update that listitem for “System Account”.

1. (Before Update Item)
image
2. Custom code to update item.

image

3.  In above code you can replace “your name” with your desired name.
4. After you can see in profile and Upper Right corner of your site.
image 

You can customize any account using above code. Sometime you want friendly name instead DOMAIN\<<UserName>>

Let me know your comment on this.

SharePoint System Account Issue


SharePoint\System account is special account. Sometime it happens that when you login using DOMAIN/<<UserName>> is display as “System Account”. (Following image)

 image

This is because of when application created or extended, during that time which account specify for application pool identity that consider as “SharePoint/System” account.

But if you want to display your username instead of “System Account” following are the solutions for that.

1. Go to Central Administration Site
2. Go to Security –> Configure Service Account
image
3. Next page you can see that “Credential Management”.
image
4. Select your application pool.
5. Select different account. (Other than your account). Which ever account you specify over here for that you can see “SharePoint/System” account.
6. If you still want that your application pool identity should be your name then manually set using IIS Manager.

Let me know if you still have any question about it.

Sunday, July 18, 2010

ASP.net MVC Routing - Part1


In ASP.net MVC Routing works following way and need to register in Application_Start of Global.asax

Default Route when ASP.net MVC Application created

routes.MapRoute(
   "Default", // Route name
   "{controller}/{action}/{id}", // URL with parameters
   new { controller = "Default", action = "Index", id = UrlParameter.Optional });

Default route has placeholder that done most of work.

For example :

if request is made for http://<<yoursite>//Home/Index.
This will call HomeController’s Index method.

if request is made for http://<<yousite>//Home/Index/1
This will call HomeController’s Index method with argument value “1”.

Sample Controller:
public class HomeController : Controller
    {      
        public ActionResult Index(string id)
        {
            return new ContentResult() { Content = "This is Index" };
        }
    }

Next Part will going to give you detail about “Slugish Url”.

ASP.net 4.0 Url Routing


In ASP.net 4.0 , New functionality included for URL Routing.

For Example : ( Application_Start)
RouteTable.Routes.MapPageRoute("Default","Home","~/Default.aspx");

When Browser request for http://<<your site>>/Home, It will route to page Default.aspx

And Page has some property that allow you to get route data, Page.RouteData

Even generic route is also possible.

RouteTable.Routes.MapPageRoute("Default","{Home}","~/{Home}.aspx");

Now When request made for Url : http://<<yoursite>/Test It automatically route to Test.aspx

One more thing true about ASP.net 4.0 routing is even though routing is done, there is still possible that direct request for “ASPX” pages.

To disable that

1. from UI (IIS 7.0 or later)
       - Go to inetmgr
       - In Feature View go to “Request Filtering”
         image  
       - Click Deny FileName Extension 
        image

2.  Using main web.config file of your application          
 <system.webServer>
     <modules runAllManagedModulesForAllRequests="true" />
        <security>
            <requestFiltering>
                <fileExtensions>
                    <add fileExtension=".aspx" allowed="false" />
                </fileExtensions>
            </requestFiltering>
        </security>
  </system.webServer>
    - Add Module tag with runAllManagedModulesForAllRequests = “true”
    - Add requestfiltering with fileextension=”.aspx” and “allowed=false”.


Let me know your comment on this.

Sunday, June 20, 2010

Process Bitmap Faster In .NET

In .NET (C# or VB.net) you can process Bitmap pixel by pixel.

Here is the example of simple code. ( Use Example of Gray Scale)

System.Drawing.Image img = System.Drawing.Bitmap.FromFile(@"C:\Test.jpg");
Bitmap bmp = (Bitmap)img;
         for (int x = 0; x < bmp.Width; x++)
         {
             for (int y = 0; y < bmp.Height; y++)
             {
                Color c =  bmp.GetPixel(x, y);
                int gray = (int)((c.R + c.G + c.B)/3);
                bmp.SetPixel(x,y,Color.FromArgb(gray,gray,gray));
             }
         }

Above code works fine but it takes to much time to process.In order to process Bitmap Image Faster “BitmapData” is powerful class in .NET. Here is the example of that class.

System.Drawing.Image img = System.Drawing.Bitmap.FromFile(@"C:\Test.jpg");
Bitmap bmp = (Bitmap)img;           
System.Drawing.Imaging.BitmapData data = bmp.LockBits(new Rectangle() { X = 0, Y = 0, Width = bmp.Width, Height = bmp.Height }, System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
IntPtr ptr = data.Scan0;           
int bytes  =data.Stride * bmp.Height;
byte[] rgbValues = new byte[bytes];           
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
int val = 0;
for (int counter = 0; counter < rgbValues.Length; counter += 3)
{
    val = rgbValues[counter + 0] + rgbValues[counter + 1] + rgbValues[counter + 2];
    rgbValues[counter+0] =  (byte)(val/3);
    rgbValues[counter+1] =  (byte)(val/3);
    rgbValues[counter+2] =  (byte)(val/3);
}
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
bmp.UnlockBits(data);      

This code works must faster than previous code. Test it with PictureBox Control and See the effect… :)

Translate content using Bing Service In ASP.net MVC

I used Translate Bing Service API in SharePoint Translate Web Part. Same API can be used with ASP.net MVC. Here i am writing small extension method for HtmlHelper.

public static class HtmlHelperExtention
    {
        public static string Tranlate(this System.Web.Mvc.HtmlHelper objHelper,string inputContent,string sourceLanguage,string targetLanguage)
        {
            string apikey = System.Configuration.ConfigurationManager.AppSettings["BingAPI"];
            Soap s = new Soap();
            string frenchoutputtext = s.Translate(apikey, inputContent, sourceLanguage, targetLanguage);
            return frenchoutputtext;           
        }
    }

In View Of ASP.net MVC you should write this.

<body>
    <div>
    <%=  Html.Tranlate("<div>This is Translation test.</div>","en","fr") %>
    </div>
</body>

Requirement:
1. Valid API key required.
2. Need to Add Proxy Class to user “Bing Translate Service”.

Hope this will help……. 

Sunday, January 31, 2010

Custom GroupBox Control for Silverlight 3

In previous post i put xaml that allow to create GroupBox using XAML.

Here i am adding custom groupbox control.

image

Download link :   GroupBox control for Silverlight 3

Codeplex Link :  Silverlight3 GroupBox

GroupBox control for Silverlight3

GroupBox control is not available in Silverlight 3, but you can create GroupBox control using following XAML.

<Border CornerRadius="10" Background="AliceBlue" BorderBrush="Black" BorderThickness="1" Margin="10,10,10,10" Width="400" x:Name="Test">
            <StackPanel>
                <Border Margin="20,-10,0,0" Background="AliceBlue" HorizontalAlignment="Left">
                    <TextBlock Text="User Setting" Margin="10,0,10,0" FontWeight="Bold"></TextBlock>
                </Border>
              <!-- Put your content -->
            </StackPanel>
        </Border>
In above XAML you can replace <!-- Put your content –> with your data inside groupbox .

For Example :

<StackPanel>
            <TextBox>
</StackPanel>

Let me know your comment on this.

Saturday, January 16, 2010

Silverlight 3: Programmatically Validate Textbox

In previous post about validation in Silverlight, I explained the How binding is used to validate data ? .

Here i am going to explain how to validate Textbox ( Any control that has validation states group).

There are three validation states. 1. Valid 2. InvalidFocused 3. InvalidUnfocused. Now use VisualStateManager to change the state of control. Here is common method.

public void SetState(Control c, string message,string state)     
{
if (state == "Invalid")
{
if (c.Focus())
VisualStateManager.GoToState(c, "InvalidFocused", false);
else
VisualStateManager.GoToState(c, "InvalidUnfocused", false); ToolTip tp = ((Grid)VisualTreeHelper.GetChild(c, 0)).FindName("validationTooltip") as ToolTip;
tp.DataContext = message;
tp.Template = LayoutRoot.Resources["ValidationToolTipTemplate"] as ControlTemplate;
}
else
{
VisualStateManager.GoToState(c, "Valid", false);
}
}

In XAML Page you have to specify control template.

<ControlTemplate x:Key="ValidationToolTipTemplate">
<Grid x:Name="Root" >
<Border Margin="4,4,-4,-4" Background="#152A2E31" CornerRadius="4"/>
<Border Margin="3,3,-3,-3" Background="#252A2E31" CornerRadius="4"/>
<Border Margin="2,2,-2,-2" Background="#352A2E31" CornerRadius="4"/>
<Border Margin="1,1,-1,-1" Background="#452A2E31" CornerRadius="4"/>
<Border Background="#FFDC000C" CornerRadius="4"/>
<Border CornerRadius="4">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#66FFFFFF"/>
<GradientStop Color="#66000000" Offset="1"/>
<GradientStop Color="#00E5E5E5" Offset="0.1"/>
<GradientStop Color="#00161616" Offset="0.9"/>
</LinearGradientBrush>
</Border.Background>
<TextBlock Margin="8,3,8,4" MaxWidth="250" UseLayoutRounding="false" Foreground="White" Text="{Binding}" TextWrapping="Wrap"/>
</Border>
</Grid>
</ControlTemplate>

This control template is used for validation tooltip.

Control like Textbox,listbox ,combobox,checkbox has validation states so you can use this method.

Example:

if(txtFirstName.Text == “”)
{
SetState(txtFirstName , “Invalid”, “Please enter first name”);
}

Let me know you comment on this.

Wednesday, December 9, 2009

Silverlight 3 : Enable and Disable Validation For Control.

Please check previous post about validation with silverlight.

Enable and Disable Validation or you can say manually adding and remove binding for control.

In previous post i bound myName TextBox to Name property of Customer, But some case you do not want to validate that control and to do so you need to clear binding. ( Simplest way as per my thinking).

1. Remove Binding in Silverlight or Disable Validation

// Clear all errors from validation summary. ( Review previous post XAML)

valida.Errors.Clear();

// Set Text to clear ( As per your need )
myName.Text = String.Empty;

//This call will cleae binding for TextProperty( Take any dependency property)
myName.SetValue(TextBox.TextProperty, DependencyProperty.UnsetValue);
            myName.ClearValue(TextBox.TextProperty);

2. Adding Binding in Silverlight or Enable Validation.

Binding binding = new Binding("Name");
           binding.ValidatesOnExceptions = true;
           binding.NotifyOnValidationError = true;
           binding.Mode = BindingMode.TwoWay;
           myName.SetBinding(TextBox.TextProperty, binding);


Let me know your comment on this.