Thursday, October 29, 2015

What is JSON?

JSON

- JSON stands for Javascript Object Notation.
- It's a message format, used for interchanging data.
- JSON is based on the Javascript.
- JSON is lightweight than XML and modern webservices use JSON to exchange data.
- JSON can store data in Name/Value pairs and Ordered List.

Here is a simple example of JSON. 

{"employees":[
    {"firstName":"John""lastName":"Doe"},
    {"firstName":"Anna""lastName":"Smith"},
    {"firstName":"Peter""lastName":"Jones"}
]}


Monday, October 26, 2015

Advantages of using Stored Procedures.

1. They help in reducing the network traffic and latency which in turn boosts application performance. 
2. They help in promoting code reuse. 
3. They provide better security to data. 
4.  It is possible to encapsulate the logic using stored procedures. This allows to change stored procedure code without affecting clients. 
5.  It is possible to reuse stored procedure execution plans, which are cached in SQL Server's memory. This reduces server overhead.

Thursday, October 22, 2015

How to prevent partial page to access directly in asp.net MVC.

Use the ChildActionOnly Attribute on Action of your Controller. 
For example:

        [HttpGet]
        [ChildActionOnly]
        public ActionResult GetProductList() {
            var vm = new ViewModels.EntryListViewModel(Models.UIdentity.Id);
            return View(vm);
        }

ASP.NET MVC DisplayFor Html Helper Set Default Value

Use use DisplayFor Html Helper for displaying our model text in to razor view. It is actually render Span <span> tag in our html document. Sometime our Model returns null values and we required to display the text over null value like this. "Null" , "N/A" ,"$0.00" and so on...
Here is the simplest solution.

In your Model Class (C#). 


Import the namespace:
using System.ComponentModel.DataAnnotations;

Decorate the your property. 

[DisplayFormat(NullDisplayText="N/A")]
public string LabelText { get; set; }

Thats it!. 

Tuesday, October 20, 2015

Remove dash from GUID in C#

C# 
Guid.NewGuid().ToString("N");

Difference between encoding and Encryption


Encoding: transforms data into another format using a scheme that is publicly available so that it can easily be reversed and can be transmitted without danger over a communication channel or stored without danger on a storage medium. 
Algorithms Used in Encoding: ASCII, Unicode, URL Encoding, Base64. 
Example: Binary data being sent over email, or viewing special characters on a web page.

Encryption: transforms data into another format in such a way that only specific individual(s) can reverse the transformation.
Encryption method uses secret keys : the algorithm is well-known, but the encryption and decryption process requires having the same key for both operations, and the key is then kept secret.
Algorithms Used in Encryption: AES, Blowfish, RSA ...  
Example: Sending someone a secret letter that only they should be able to read, or securely sending a password over the Internet.

Monday, October 19, 2015

C#: Converting Object in to JSON

This is not the single way to serializing object into JSON, but the simple way is to do that. 

1. Download the Newtonsoft JSON serializer. 
2. Reference the DLL. 
3. Using the namespace in your class. 


C# Code. 


namespace:

using Newtonsoft.Json;


 public string GetData(string name)
        {
            person p = new person();
            p.name = name;
            return JsonConvert.SerializeObject(p);
        }
    }

Friday, October 16, 2015

WPF: Set Startup Window.

You can set your Startup page by setting StartupUri="MainWindow.xaml"  in App.xaml file. 
<Application x:Class="MyApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MyWindow.xaml">
    <Application.Resources>
   
    </Application.Resources>

</Application>

Tuesday, October 13, 2015

WPF: How to stretch in width a WPF user control to its window?


Just Remove the width from UserControl it will automatically adjust the width.  

<UserControl x:Class="etc"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" >

WPF: DockPanel - When to use?



Dockpanels 


are useful when when placing and organising several different items onto a window, specifically when anchoring items to the top, bottom, left, right and then fitting to the remaining space in the centre (I recently discovered they're quite handy when used in conjunction with expanders). No real downsides, could well be effective for you.

XAML: 

<Window x:Class="WpfTutorialSamples.Panels.DockPanel"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="DockPanel" Height="250" Width="250">
        <DockPanel>
                <Button DockPanel.Dock="Left">Left</Button>
                <Button DockPanel.Dock="Top">Top</Button>
                <Button DockPanel.Dock="Right">Right</Button>
                <Button DockPanel.Dock="Bottom">Bottom</Button>
                <Button>Center</Button>
        </DockPanel>
</Window>

Thursday, October 8, 2015

WPF: Styling a button in code.


Add Style in your App.xaml file (placed on the root of your project).

   <Application.Resources>
<Style x:Key="MyButton" TargetType="Button">
            <Setter Property="Background" Value="Red" />
            <Setter Property="Width" Value="100"></Setter>
            <Setter Property="Height" Value="100"></Setter>
 </Style>
   <Application.Resources>


In .CS file. 

  Button btn = new Button();
  Style style = this.FindResource("MyButton") as Style;
  btn.Style = style

WPF: Programmatically Add Click Event to a Button.


Sometime we create button on the fly and want to attach an event at runtime. 
so here we go. 

// Creating button on the fly.
 Button btn = new Button();
 // Passing an argument to the handler
 btn.CommandParameter= "0012" ;

// attaching handler to click event.
btn.Click += teamHandler;


// Handler for button
 protected void teamHandler(object sender, RoutedEventArgs args) {
            var btn = (Button)sender;
            int teamId = int.Parse(btn.CommandParameter.ToString());
            string name = Repository.GetItem(teamId).Name
            MessageBox.Show(name);
}

WPF: StackPanel Alignment

Set the attribute (Orientation) of StackPanel Horizontal or Vertical so all child controls will be aligned automatically. 

Orientation="Horizontal"
Or
Orientation="Vertical"

WPF: dynamically add images



This is how we can add an image dynamically in stackpanel.

XAML
<stackpanel name="Container"></stackpanel>

.CS (C#)
Image img = new Image();
img.Source = new BitmapImage(new Uri(@"pack://application:,,,/Quiz1;component/Images/loin.png"));
Container.Children.Add(img);

WPF Window Center Align

Set the attribute of window element.
WindowStartupLocation="CenterScreen"

WPF Set Window Background

<Window.Background>
 <ImageBrush ImageSource="Images/background.jpg" AlignmentY="Top"
 AlignmentX="Center"/>
</Window.Background>