Wednesday, 18 December 2013

Difference between regular web application and MVC

Advantages of Web Forms
1)    ViewState - A ViewState that allows you to persist objects and values throughout your application.
2)    Rapid Application Development - Very little learning curve, user-friendly and easy to get an application up and running quickly.
3)    Controls and Toolbox - A huge Toolbox with basically any major Control that you would need for developing applications.
4)    Maturity - Web Forms have been around over 10 years so there are a lot of communities and resources available and many bugs and issues have been addressed.
5)    Code Generation - Web Forms makes it quite a bit easier to not worry about wiring up different events and methods as much of this can be handled automatically.
6)    Familarity - Developers coming from a Windows Forms background will find themselves very familar with Web Forms and can easily jump into development.

Advantages of MVC (both MVC3 and MVC4)
1)    Separation of Concerns - The MVC Paradigm of Model-View-Controller allows you to more easily separate all of your concerns within your application. This can lead to improved scalability within applications.
2)  Testing - MVC is designed and built for Testing-Driven Development and allows for applications to be very easily tested to help secure against error and unexpected behavior.
3)    Integration with Client-Side Tools - The MVC pattern easily allows client-side tools such as jQuery to be seamlessly integrated. This can allow you to create very rich user interfaces for your applications.
4)    Statelessness - Although one of the main advantages of Web Forms, this allows you to adhere to a more stateless format consistent with the rest of the web.
5)   Control and Flexibility - MVC allows developers to have complete control over their applications as the developer doesn't rely on Controls to automatically generate the appropriate HTML, which after switching to MVC can often be seen as "bloated". The developer will write their own HTML instead of having it generated for them.

6)    Search Engine Optimization - The use of RESTful URLs within MVC makes it friendlier for being accessed through search engines. 

ASP.Net Razor

Reading User Input
Another important feature of dynamic web pages is that you can read user input.
Input is read by the Request[] function, and posting (input) is tested by the IsPost condition:
@{
var totalMessage = "";
if(IsPost)
    {
    var num1 = Request["text1"];
    var num2 = Request["text2"];
    var total = num1.AsInt() + num2.AsInt();
    totalMessage = "Total = " + total;
    }
}
<html>
<body style="background-color: beige; font-family: Verdana, Arial;">
<form action="" method="post">
<p><label for="text1">First Number:</label><br>
<input type="text" name="text1" /></p>
<p><label for="text2">Second Number:</label><br>
<input type="text" name="text2" /></p>
<p><input type="submit" value=" Add " /></p>
</form>
<p>@totalMessage</p>
</body>
</html>


Converting Data Types
Converting from one data type to another is sometimes useful.

The most common example is to convert string input to another type, such as an integer or a date.
As a rule, user input comes as strings, even if the user entered a number. Therefore, numeric input values must be converted to numbers before they can be used in calculations.
Below is a list of common conversion methods:

Method
Description
Example
AsInt()
IsInt()
Converts a string to an integer.
if (myString.IsInt())
  {myInt=myString.AsInt();}
AsFloat()
IsFloat()
Converts a string to a floating-point number.
if (myString.IsFloat())
  {myFloat=myString.AsFloat();}
AsDecimal()
IsDecimal()
Converts a string to a decimal number.
if (myString.IsDecimal())
  {myDec=myString.AsDecimal();}
AsDateTime()
IsDateTime()
Converts a string to an ASP.NET DateTime type.
myString="10/10/2012";
myDate=myString.AsDateTime();
AsBool()
IsBool()
Converts a string to a Boolean.
myString="True";
myBool=myString.AsBool();
ToString()
Converts any data type to a string.
myInt=1234;
myString=myInt.ToString();

Wednesday, 31 July 2013

Import xml data from xml file into MS Access database file



DBXmlInfo.xml
<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2013-07-29T16:57:21">
 
  <Employees>
    <Employee>
        <EmpId>1001</EmpId>
        <Name>Ravi</Name>
        <Salary>111</Salary>
    </Employee>
    <Employee>
      <EmpId>1002</EmpId>
      <Name>Rahul</Name>
      <Salary>222</Salary>
    </Employee>
    <Employee>
      <EmpId>1003</EmpId>
      <Name>Chandan</Name>
      <Salary>333</Salary>
    </Employee>  
  </Employees>

  <studuents>
    <student>
      <StuId>2001</StuId>
      <Name>Ankit</Name>
      <RollNo>123</RollNo>
    </student>
    <student>
      <StuId>2002</StuId>
      <Name>Neetu</Name>
      <RollNo>456</RollNo>
    </student>
  </studuents>   
</dataroot>


C# Code
Microsoft.Office.Interop.Access.Application objAccess = new Microsoft.Office.Interop.Access.Application();

//objAccess.NewCurrentDatabase("c:\\TestDb.mdb", Microsoft.Office.Interop.Access.AcNewDatabaseFormat.acNewDatabaseFormatAccess2007);
objAccess.OpenCurrentDatabase("c:\\TestDb.mdb", false, "abc123");               

objAccess.ImportXML("C:\\DBXmlInfo.xml", Microsoft.Office.Interop.Access.AcImportXMLOption.acStructureAndData);

objAccess.CloseCurrentDatabase();
                objAccess.Quit(Microsoft.Office.Interop.Access.AcQuitOption.acQuitSaveAll);



Output
Above code will create two tables in TestDb.mdb database with like:
Employee
EmpId
Name
Salary
1001
Ravi
111
1002
Rahul
222
1003
Chandan
333

Student
StuId
Name
RollNo
2001
Ankit
123
2002
Neetu
456


Wednesday, 24 July 2013

WPF Performance Tips

Windows Presentation Foundation provides a very comfortable way to develop rich user experiences. A drop shadow for example can add by inserting two simple lines of XML. But this simplicity can also mislead us to overuse them. This leads to performance issues. The following tips may help you to avoid or fix them.
1) Dispatch expensive calls: either within the UI thread with a lower DispatcherPriority by calling Dispatcher.BeginInvoke() or to a background thread by using a BackgroundWorker to keep the UI responsive.
2) Fix binding errors: because they consume a lot of time, trying to resolve the path error, including searching for attached properties. You can find them by looking for System.Windows.Data Error in the Visual Studio output log.
3)  Reduce the number of visuals: by removing unneeded elements, combining layout panels and simplifying templates. This keeps the memory footprint small and improves the rendering performance.
4)   Prevent Software Rendering: the use of transparent windows by setting AllowsTransparency to true or using old BitmapEffects can cause WPF to render the UI in software on Windows XP, which is much slower.
5) Load resources when needed: even though it's the most comfortable way to merge all resources on application level it can also cost performance by loading all resources at startup. A better approach is to load only often used resources and load the other on view level.
6)    Virtualize lists and views: by using a VirtualizingStackPanel as ItemsPanel for lists. This only creates the visible elements at load time. All other elements are lazy created when they get visible. Be aware that grouping or CanContextScrol="True" prevents virtualization.
7)   Enable Container Recycling: virtualization brings a lot of performance improvements, but the containers will be disposed and recreated, this is the default. But you can gain more performance by recycle containers by setting VirtualizingStackPanel.VirtualizationMode="Recycling".
8)  Freeze Freezables: by calling Freeze() in code or PresentationOptions:Freeze="true" in XAML. This reduces memory consumption and improves performance, because the system doesn’t need to monitor for changes.
9) Disable Assembly localization: if you don't need it. By using the [NeutralResourcesLanguageAttribute]. This prevents an expensive lookup for satellite assemblies.
10) Lower the frame rate of animations: by setting Storyboard.DesiredFrameRate to lower the CPU load. The default is 60 frames/second.
11) Use StreamGeometries instead of PathGeometries: if possible to draw complex 2D geometries, because they are much more efficient and consume less memory.