Friday, February 28, 2014

SQL Server Data Types

Here are the SQL server data types for quick reference,

  1. Exact numerics – (bigint, bit, decimal, int, money, numeric, smallint)
  2. Approximate numerics (float, real)
  3. Date and time (date, datetime2, datetime, datetimeoffset, time)
  4. Character strings (char, varchar, text)
  5. Unicode character strings (nchar, ntext, nvarchar)
  6. Binary strings (binary, varbinary, image)
  7. Other data types (cursor, timestamp, uniqueidentifier, table)
  8. Large valued data types (varchar(max), nvarchar(max))
  9. Large object data types (text, ntext, image, xml)

Sunday, February 16, 2014

ASP.NET ViewState example

ViewState is a client side state management mechanism. It is not the only state management mechanism. Some other examples would be,

For client side state management,
·         Hidden Field
·         Cookies
·         Control State

For server side,
·         Session
·         Application Object
·         Caching
·         Database

ViewState should be used to persist only small amounts of data as having lots of data in ViewState will degrade performance.

Also by default ViewState does not encrypt data but only encodes them to Base64. So generally sensitive data should not be kept there.

However you can encrypt ViewState by setting  ViewStateEncryptionMode="Always" as a page level directive,

<%@Page ViewStateEncryptionMode="Always" %>

 or for all pages by setting in web.config,

<configuration>
   <system.web>
      <pages ViewStateEncryptionMode="Always" />
   </system.web>
</configuration>

There is a lot of confusion about ViewState and Form input preservation on postbacks.
An example would be, how a textbox persists data even when View State is set to off.
This is because many ASP.Net controls (TextBox, ListBox, RadioButtonList etc) have implemented the IPostBackDataHandler interface and calls the LoadPostData(), which repopulates them.

Here is a small example of using ViewState persist state,
  1. Create a ASP.Net web application 
  2. Add a Web Form named ViewStateExample
  3. Paste the below given code to the apsx and apsx.cs files respectively
  4. Make sure to rename of the namespace LearnASPWebForms to your namespace


  
ViewStateExample.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ViewStateExample.aspx.cs" Inherits="LearnASPWebForms.ViewStateExample" %>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

    <title>ViewStateExample</title>

</head>

<body>

    <form id="testform" runat="server">

        <asp:Label runat="server" Text="Enter a Value" />

        <asp:TextBox runat="server" id="FieldToBePreserved" /><br />

        <asp:Button runat="server" id="SubmitForm" onclick="SubmitForm_Click" text="Submit and add to ViewState" />

        <asp:Button runat="server" id="SubmitDontSave" text="Submit but dont save to ViewState" OnClick="SubmitDontSave_Click"  />

        <br /><br />

        Value from ViewState: <asp:Label runat="server" id="ViewStateValue" BorderStyle="Dotted" />

    </form> 

</body>

</html>



CodeBehind ViewStateExample.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LearnASPWebForms
{
    public partial class ViewStateExample : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (ViewState["Field"] != null)
                ViewStateValue.Text = ViewState["Field"].ToString();
            else
                ViewStateValue.Text = "Nothing in ViewState";
        }

        protected void SubmitForm_Click(object sender, EventArgs e)
        {
            ViewState["NameOfUser"] = FieldToBePreserved.Text;
            ViewStateValue.Text = FieldToBePreserved.Text;
        }

        protected void SubmitDontSave_Click(object sender, EventArgs e)
        {
            //This does nothing
        }

    }
}

Tuesday, February 4, 2014

Could not load file or assembly EntityFramework, Version=6.0.0.0 and MVC 4

In MVC 4 this mostly happens when you create a MVC 4 project and then for your DAL (Data access layer ) you have a separate class library project and install EF through NuGet. NuGet will give you the latest version which is currently EF 6. When you try to run the project you will see an error similar to the above with a yellow screen of death. The root cause is that MVC 4 has  EF5 referenced. So just open up NuGet and upgrade  EF to version 6 under updates section and you are good to go.

Saturday, February 1, 2014

ViewBag vs ViewData in ASP.NET MVC

Bottom line is that they are essentially the same, ViewBag is just a dynamic wrapper around ViewData. ViewData is actually a dictionary used to pass data from controllers to views. The following example will make this clear,

ViewBag.Name = "Padmika"

is equal to,

ViewData["Name"] = "Padmika"

Saturday, January 25, 2014

Browser definition safari3plus not found : Blackberry redirection not working

One of the clients of the product that I work on had a problem where a redirection to an apps download page was not working on Blackberry 6.0 and Blackberry 7.0 OS  devices and was working fine for BB 5.0 OS. Strangely this was working fine until the client had installed some updates from Microsoft.

When digging in to the issue the root cause was that there is a custom browser definition for BB 6.0 and BB 7.0 browsers which had a different user agent string compared against BB 5.0 and this browser definition file was having a parent of  "safari3to4". However the machine had an extra browser definition for "safari3plus" under,

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\Browsers\safari.browser and
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\Browsers\safari.browser

This was causing the custom browser definition not getting picked up as the parent ID of it was safari3to4.
The fix is (rather than removing safri3plus definition of the above mentioned files ) to rename the parent Id of the custom browser definition file under App_Browsers in the IIS deployed folder, this doesn't even require an IIS reset or an app pool  recycle.

Microsoft changes these browser definition files from time to time and  developers who use Request.Browser property to do re-directions should be aware of this fact.

 

Sunday, December 29, 2013

Func vs Action vs Predicate in C#

Often users find certain aspects of functional programming in C# confusing and this is one of them.

To simply put Action<T> and Predicate<T> are special cases of Func<T,TResult>. And all of these are delegate types provided by C# so that you do not need to define your own delegate type.

Action<T>
When you need a delegate that takes in a parameter (say an int) and returns nothing (void) then you have,

Action<int> which is equivalent to Func<int,void>

(Note that Func<int,void> is not something valid and wont work, this is just to draw a parallel between Action and Func. )

Predicate<T>
When you need a delegate that takes in a parameter (say an int ) and returns a bool then you have,

Predicate<int> which is equivalent to Func<int,bool>

Typically Linq uses Func and List<T> uses Action and Predicate.

Tuesday, December 17, 2013

An unsecured or incorrectly fault error WCF

I encountered this error while executing the following snippet,

factory.CreateChannel().Issue(rst)       // Here the rst is a RequestSecurityToken object and 
                                                          //  factory is a WSTrustChannelFactory object

It seems that this was due to incorrect clock skew between my machine (client) and the server (where the WCF service was running). After correcting the time of my machine the error went away.