Friday 12 April 2013

C# Evolution

I summarized all the key features into a C# Evolution Matrix for your reference as below diagram shows:

Tuesday 9 April 2013

Getting a User’s Time from Twitter API in C#

<?xml version="1.0" encoding="UTF-8"?>
<statuses type="array">
  <status>
    <created_at>Tue Apr 09 10:08:15 +0000 2013</created_at>
    <id>1016846548</id>
    <text>Hi &quot;everybody&quot; #greetings</text>
    <source>web</source>
    <truncated>true</truncated>
    <in_reply_to_status_id></in_reply_to_status_id>
    <in_reply_to_user_id></in_reply_to_user_id>
    <favorited>false</favorited>
    <user>
      <id>2566935</id>
      <name>Abc Xyz</name>
      <screen_name>Abc</screen_name>
      <location></location>
      <description></description>
      <profile_image_url>http://.../normal.jpg</profile_image_url>
      <url>https://www.blogger.com/blogger</url>
      <protected>false</protected>
      <followers_count>42</followers_count>
    </user>
  </status>
  <status>
    ...
  </status>
</statuses>

Extracting the Data

XDocument document = XDocument.Parse(response, LoadOptions.None);

var query = from e in document.Root.Descendants("status")

            select new UserStatus
            {
                UserName = e.Element("user").Element("name").Value,
                ProfileImage = e.Element("user").Element("profile_image_url").Value,
                Status = HttpUtility.HtmlDecode(e.Element("text").Value),
                StatusDate = (e.Element("created_at").Value.ParseDateTime())
            };

Few things to note about the above code:

1. HttpUtility.HtmlDecode is being used in order to get a readable text. The response contained text like “Testing "posts" #devacademy3” and this method (in System.Web) converts it to “Testing “posts” #devacademy3”.

2. The ParseDateTime() method on the element’s value is an extension method I created in order to parse a DateTime from the special string representation of the time the status was posted: Sat Nov 15 10:08:15 +0000 2008 This method looks like:

public static class StringExtensions
{
    public static DateTime ParseDateTime(this string date)
    {
        string dayOfWeek = date.Substring(0, 3).Trim();
        string month = date.Substring(4, 3).Trim();
        string dayInMonth = date.Substring(8, 2).Trim();
        string time = date.Substring(11, 9).Trim();
        string offset = date.Substring(20, 5).Trim();
        string year = date.Substring(25, 5).Trim();
        string dateTime = string.Format("{0}-{1}-{2} {3}", dayInMonth, month, year, time);
        DateTime ret = DateTime.Parse(dateTime);
        return ret;
    }
}

Wednesday 3 April 2013

How to Execute Large Database script in SQL Server?

In case the generated scripts files are too large to open in SQL Server Management Studio, you can utilize the SQLCMD Utility to execute the scripts.
SQLCMD -S LOCALHOST -d AdventureWorks -i C:\script.sql -E 



You will be able to see the below snippet once the schema and data is successfully loaded to the destination database.


How to Execute Large Database script in SQL Server?

In case the generated scripts files are too large to open in SQL Server Management Studio, you can utilize the SQLCMD Utility to execute the scripts.
SQLCMD -S LOCALHOST -d AdventureWorks -i C:\script.sql -E 



You will be able to see the below snippet once the schema and data is successfully loaded to the destination database.


Tuesday 2 April 2013

Difference Between Razor View Engine and ASPX View Engine

View Engine is responsible for rendering the view into html form to the browser.
By default, Asp.net MVC support Web Form(ASPX) and Razor View Engine.

Razor uses @ symbol to make the code like as:

 @Html.ActionLink("Login", "Login")

Webform uses <% and %> delimiters to make the code like as:

 <%: Html.ActionLink("Login", "Login") %> 

 One of the advantages of using Razor is that you can have a Razor parser run against any string, where aspx needs an httpcontext and other heavyweight elements

Wednesday 20 March 2013

What is the difference between Scope_Identity(), Identity(), @@Identity??

Most of the developers get confused in these terms:

  1. @@identity always returns the last identity value created in the same session regardless of the table that produced the value, and regardless of the scope of the statement.
  2. scope_identity() returns the identity value created in the same session and the same scope.
  3. identity() is not used to get an identity value but instead is used to create an identity in select...into statement that is declaring a column in a table as an identity column.
Here the session means the database connection.

Here is an example:
CREATE TABLE ABC(
  ID INT IDENTITY(1,1),
  Name VARCHAR(100)
)

INSERT INTO ABC (Name) VALUES ('x')
SELECT SCOPE_IDENTITY(), @@IDENTITY

Note:
To avoid the potential problems associated with adding a trigger later on, always use SCOPE_IDENTITY() to return the identity of the recently added row in your T SQL Statement or Stored Procedure.

Tuesday 19 March 2013

How to make windows application to run on 32 bit and 64 bit

From your project in visual studio, follow the menu items as below:

Solution explorer  -->  Right click  -->  Properties (A new tab should open up)  --> Build tab
Setting configurations to 'Any CPU' ensures that your application will work on both 32 and 64bit computers.