6 March 2014

How to get drive list in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window4.xaml
    /// </summary>
    public partial class Window4 : Window
    {
        public Window4()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgse)
        {
            DriveInfo[] strdrives = DriveInfo.GetDrives();
            foreach (DriveInfo strdrive in strdrives)
            {
                listBox1.Items.Add(strdrive);
            }
        }
    }
}
 
 
 

aspx page creation code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class createaspx : Form
    {
        public createaspx()
        {
            InitializeComponent();
        }
        StreamWriter sw;

        private void create_Click(object sender, EventArgse)
        {
            sw = File.CreateText(@"E:\hello.aspx");
            sw.WriteLine("<html>");
            sw.WriteLine("<form runat=\"server\">");
            sw.WriteLine("<asp:button text=\"hello\" runat=\"server\"></asp:button>");
            sw.WriteLine("</form>");
            sw.WriteLine("</html>");
            sw.Close();
            System.Diagnostics.Process.Start(@"E:\hello.aspx");
        }
    }
}

5 March 2014

var datatype in C#


The var keyword can be used in place of a type when declaring a variable to allow the compiler to infer the type of the variable. This feature can be used to shorten variable declarations, especially when instantiating generic types, and is even necessary with LINQ expressions (since queries may generate very complex types).
The following:
int num = 123;
string str = "asdf";
Dictionary<int, string> dict = new Dictionary<int, string>();
is equivalent to:
var num = 123;
var str = "asdf";
var dict = new Dictionary<int, string>();
var does not create a "variant" type; the type is simply inferred by the compiler. In situations where the type cannot be inferred, the compiler generates an error:
var str; // no assignment, can't infer type
 
void Function(var arg1, var arg2) // can't infer type
{
    ...
}


 

var Type: 
  • C# 3.0 adds the interesting behavior of Declaring Local Variables Implicitly. This means that there is no need to mention the data type when declaring a variable. A local variable can be declared implicitly using the var keyword in C#.
  • Declaring local variables implicitly has some restrictions; the variable must be initialized to some expression that can not be null. 

    var a= 10; 
    var z = "Rekha";
  • The primary reason for its existence is the introduction of anonymous types in C#
  • Another point to stress is that variable inference does not work for class level fields or method arguments or anywhere else other than for local variables in a method.
Advantages :
  • Less typing with no loss of functionality
  • Increases the type safety of your code. A foreach loop using an iteration variable which is typed to var will catch silently casts that are introduced with explicit types
  • Makes it so you don't have to write the same name twice in a variable declaration.
  • Some features, such as declaring a strongly typed anonymous type local variable, require the use of var

Difference between var and dynamic in C#

Introduction

The type keyword 'var' was introduced in C# 3.0 (.NET 3.5 with Visual Studio 2008) and the type 'dynamic' was introduced in C# 4.0 ( .NET 4.0 with Visual Studio 2010). Let us see the difference between these two

Background

Variables declared with var are implicitly but statically typed. Variables declared with dynamic are dynamically typed. This capability was added to the CLR in order to support dynamic languages like Ruby and Python.
This means that dynamic declarations are resolved at run-time, var declarations are resolved at compile-time. 

Table of difference 

vardynamic
Introduced in C# 3.0
Introduced in C# 4.0
Statically typed – This means the type of variable declared is decided by the compiler at compile time.
Dynamically typed - This means the type of variable declared is decided by the compiler at runtime time.
Need to initialize at the time of declaration.
e.g., var str=”I am a string”;
Looking at the value assigned to the variable str, the compiler will treat the variable str as string.
No need to initialize at the time of declaration.
e.g., dynamic str; 
str=”I am a string”; //Works fine and compiles
str=2; //Works fine and compiles
Errors are caught at compile time.
Since the compiler knows about the type and the methods and properties of the type at the compile time itself
Errors are caught at runtime 
Since the compiler comes to about the type and the methods and properties of the type at the run time.
Visual Studio shows intellisense since the type of variable assigned is known to compiler.
Intellisense is not available since the type and its related methods and properties can be known at run time only

e.g., var obj1;
will  throw a compile error since the variable is not initialized. The compiler needs that this variable should be initialized so that it can infer a type from the value.
e.g., dynamic obj1; 
will compile;
e.g. var obj1=1;
will compile 
var obj1=” I am a string”;
will throw error since the compiler has already decided that the type of obj1 is System.Int32 when the value 1 was assigned to it. Now assigning a string value to it violates the type safety.
e.g. dynamic obj1=1;
will compile and run 
dynamic obj1=” I am a string”; 
will compile and run since the compiler creates the type for obj1 as System.Int32 and then recreates the type as string when the value “I am a string” was assigned to it.
This code will work fine. 

Advantages of ASP.NET

ASP.NET has many advantages over other platforms when it comes to creating Web applications. Probably the most significant advantage is its integration with the Windows server and programming tools. Web applications created with ASP.NET are easier to create, debug, and deploy because those tasks can all be performed within a single development environment—Visual Studio .NET.
ASP.NET delivers the following other advantages to Web application developers:
  • Executable portions of a Web application compiled so they execute more quickly than interpreted scripts
  • On-the-fly updates of deployed Web applications without restarting the server
  • Access to the .NET Framework, which extends the Windows API
  • Use of the widely known Visual Basic programming language, which has been enhanced to fully support object-oriented programming
  • Introduction of the new Visual C# programming language, which provides a type-safe, object-oriented version of the C programming language
  • Automatic state management for controls on a Web page (called server controls) so that they behave much more like Windows controls
  • The ability to create new, customized server controls from existing controls
  • Built-in security through the Windows server or through other authentication/authorization methods
  • Integration with Microsoft ADO.NET to provide database access and database design tools from within Visual Studio .NET
  • Full support for Extensible Markup Language (XML), cascading style sheets (CSS), and other new and established Web standards
  • Built-in features for caching frequently requested Web pages on the server, localizing content for specific languages and cultures, and detecting browser capabilities

Which ASP.NET Server controls have Auto Postback set to TRUE by default in asp.net

All except, Buttons, Hyperlinks and LinkButtons have a default Auto Post Back property of false in the stock ASP.NET control library.

Adding web reference in class library

  1. Right click ‘References’ folder under class library project (there is no Add Web Reference option) instead select ‘Add Service Refrence’ 
    1
  2. In Add Service Reference window click Advanced… button 
    2
  3. in Service Reference Setting Window click Add Web Reference button 
    3
  4. Now you are landing on regular .NET 2.0 Add Web Reference window and you can provide a web service URL to be added to your class library.
by adding web reference and from now on you can see ‘Add Web Reference’ option in the right click context menu of Reference folder
4




and also Visual studio will create a Web References folder under your class library.

3 March 2014

How to Encrypt and Decrypt a fild in Sql Server 2008

Create a table :-

create table tbl_employee(id int identity(100,1) not null primary key,name varbinary(50))

Insert Encrypted Data in Sql :-

 insert into tbl_employee(name) values(encryptbypassphrase('abc','jc.adi'))

Show data with Decryption:-

 select id,convert(varchar(max),Decryptbypassphrase('abc',name))as name from tbl_employee