May 25, 2011

The C# ?? null coalescing operator on value and reference type


In C#  "??" operator is known as null coalescing operator. It can be used on both nullable value type &  reference type.
?? operator checks if  the value provided on the left side of the expression is null, it returns value on right side of the expression. if its not null it return the value of the left.
Q. how the value type will have null.
A. With nullable type. int? a = null;

Example 1:
string str = null;
string output = str ?? "Punjab";
------------------------------------
output = "punjab"
because str is null, 
--------------------------------------
Example 2.
string str = "Punjab";
string output = str ?? "Delhi";
------------------------------------
output = "punjab"
because str is not null, 
--------------------------------------

Example 3.
int test?=null
int output = test ?? 164;
------------------------------------
output =164
because test is null, 
--------------------------------------

May 21, 2011

Three mistakes of Steve Jobs life : Rise of Microsoft

Three Mistakes of Steve Paul Jobs, Which caused Rise of Microsoft as largest software vendor, now windows has eighty percent of monopoly and large base of developer who develop for Windows.

1) Macintosh's Prototype to bill gates: Apple firstly invent LISA then created a MAC and mistakenly displayed that to Bill Gates. Bill Gates saw future in the MAC, as Microsoft was also pursuing interest in OS but their team  failed to create such impressive GUI like MAC, Bill Gates persuaded Jobs to give him the prototype via showing   big dreams, He said to  Jobs that Microsoft will create software for MAC. Jobs could not see ill will of Bill as that time Microsoft was very small as compared to Apple. But he realized his mistake very soon when he launched Windows with GUI.

2) Mac Cloning: Apple started give rights to clone the mac via ROM chips only in 1995, ROM chips contains MAC os APIs etc. Any manufacture can create a clone of MAC via using the licensed ROM chips from MAC. Motorola and many other created bit expensive clone of MAC. On returning back to Apple, Steve stopped this business.
                   Had the apple started it in 80s, they could have hold on developer community, Which develop software for windows, real power of MS. Apple do have developer community but not that much, including IOS developers.

3) Over-priced MAC: Apple is now most valuable company in technology after success of IOS devices and App Store, they are getting developer community now, But mac seems over-priced, if they still want more developer they should reduce the price of mac. Windows machine cost around 15-20k, and Notebook starts around 25k. MAC is almost double price though hardware configuration is higher, if we compare same configuration machine price then diff of 150-200$. but that comes for price of style, but not all want style. apple should reduce its prices by $100.
                                They already have captured mobile market, but to get the desktop community above is very required
 
4) He did not take Cancer seriously and choose medicines rather than operation. He might be alive today, if have chosen operation on medicines

April 28, 2011

There is no Unicode byte order mark. Cannot switch to Unicode

Issue: {There is no Unicode byte order mark. Cannot switch to Unicode}

Reason:  Data/xml/text is in different encoding & you are trying to read/process it in different encoding.


Steps: it can occur with Microsoft .net application, asp.net mvc or any java, php applicaiton.


Just confirm that. if you are reading a stream or file, explicitly specify the encoding rather that rely on default encoding select by CLR. 
You may have to put some more lines in the code because some time the method you are using does not have parameter for that. so you have to go additional step to provide stream which is having that parameter and supply it.


Example: 


Default: 


XmlSerializer xs = new XmlSerializer(typeof(T)); return (T)xs.Deserialize(reader);


Explicitly:


XmlSerializer xs = new XmlSerializer(typeof(T)); MemoryStream memoryStream = new MemoryStream(StringToASCIIByteArray(xml)); XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.ASCII); return (T)xs.Deserialize(memoryStream);



November 27, 2010

Allow Illegal characters in path, Double quotes issue in asp.net and MVC

How to allow Illegal characters in path. This problem appears in asp.net MVC when double quotes "" or any other special character. IIS do not allow the special character in the URL, so URL will not match with any route and throw the error.
Solution:
open the web config and enter following tag in tag

<system.webserver>
   <rewrite>
            <rules>
        <rule name="DoubleQuote" stopProcessing="true">
            <match url="(.*)" />
            <conditions logicalGrouping="MatchAny">
                <add input="{REQUEST_URI}" pattern=".*[&quot;]" />
                <add input="{QUERY_STRING}" pattern=".*[&quot;]" />
              
            </conditions>
            <action type="Redirect" url="/GenericError" appendQueryString="false" />
        </rule>
          
          </rules>
        </rewrite>
</system.webserver>


like the above rule, we can put multiple rule.

Other possible options for input are

Variable Description
ALL_HTTP Returns all HTTP headers sent by the client. Always prefixed with HTTP_ and capitalized
ALL_RAW Returns all headers in raw form
APPL_MD_PATH Returns the meta base path for the application for the ISAPI DLL
APPL_PHYSICAL_PATH Returns the physical path corresponding to the meta base path
AUTH_PASSWORD Returns the value entered in the client's authentication dialog
AUTH_TYPE The authentication method that the server uses to validate users
AUTH_USER Returns the raw authenticated user name
CERT_COOKIE Returns the unique ID for client certificate as a string
CERT_FLAGS bit0 is set to 1 if the client certificate is present and bit1 is set to 1 if the cCertification authority of the client certificate is not valid
CERT_ISSUER Returns the issuer field of the client certificate
CERT_KEYSIZE Returns the number of bits in Secure Sockets Layer connection key size
CERT_SECRETKEYSIZE Returns the number of bits in server certificate private key
CERT_SERIALNUMBER Returns the serial number field of the client certificate
CERT_SERVER_ISSUER Returns the issuer field of the server certificate
CERT_SERVER_SUBJECT Returns the subject field of the server certificate
CERT_SUBJECT Returns the subject field of the client certificate
CONTENT_LENGTH Returns the length of the content as sent by the client
CONTENT_TYPE Returns the data type of the content
GATEWAY_INTERFACE Returns the revision of the CGI specification used by the server
HTTP_<HeaderName> Returns the value stored in the header HeaderName
HTTP_ACCEPT Returns the value of the Accept header
HTTP_ACCEPT_LANGUAGE Returns a string describing the language to use for displaying content
HTTP_COOKIE Returns the cookie string included with the request
HTTP_REFERER Returns a string containing the URL of the page that referred the request to the current page using an tag. If the page is redirected, HTTP_REFERER is empty
HTTP_USER_AGENT Returns a string describing the browser that sent the request
HTTPS Returns ON if the request came in through secure channel or OFF if the request came in through a non-secure channel
HTTPS_KEYSIZE Returns the number of bits in Secure Sockets Layer connection key size
HTTPS_SECRETKEYSIZE Returns the number of bits in server certificate private key
HTTPS_SERVER_ISSUER Returns the issuer field of the server certificate
HTTPS_SERVER_SUBJECT Returns the subject field of the server certificate
INSTANCE_ID The ID for the IIS instance in text format
INSTANCE_META_PATH The meta base path for the instance of IIS that responds to the request
LOCAL_ADDR Returns the server address on which the request came in
LOGON_USER Returns the Windows account that the user is logged into
PATH_INFO Returns extra path information as given by the client
PATH_TRANSLATED A translated version of PATH_INFO that takes the path and performs any necessary virtual-to-physical mapping
QUERY_STRING Returns the query information stored in the string following the question mark (?) in the HTTP request
REMOTE_ADDR Returns the IP address of the remote host making the request
REMOTE_HOST Returns the name of the host making the request
REMOTE_USER Returns an unmapped user-name string sent in by the user
REQUEST_METHOD Returns the method used to make the request
SCRIPT_NAME Returns a virtual path to the script being executed
SERVER_NAME Returns the server's host name, DNS alias, or IP address as it would appear in self-referencing URLs
SERVER_PORT Returns the port number to which the request was sent
SERVER_PORT_SECURE Returns a string that contains 0 or 1. If the request is being handled on the secure port, it will be 1. Otherwise, it will be 0
SERVER_PROTOCOL Returns the name and revision of the request information protocol
SERVER_SOFTWARE Returns the name and version of the server software that answers the request and runs the gateway
URL Returns the base portion of the URL

 List of available server variable following script can be used
Request.ServerVariables
so you can create rule using above server variable and filter the values accordingly.