Pages

Thursday 16 August 2012

Difference Between Sql Server VARCHAR and NVARCHAR Data Type


Difference Between Sql Server VARCHAR and NVARCHAR Data Type





The abbreviation for Varchar is non-Unicode Variable Length character String.

* The abbreviation of NVarchar is Unicode Variable Length character String. 

Differences :

Character Data Type
Varchar - Non-Unicode Data 
NVarchar - Unicode Data

2 Character Size 
Varchar - 1 byte
NVarchar - 2 bytes

3 Maximum Length 
Varchar - 8,000 bytes
NVarchar - 4,000 bytes

4 Storage Size 
Varchar - Actual Length (in bytes)
NVarchar - 2 times Actual Length (in bytes)



Difference between Sql Server Char and Varchar Data Type


Difference between Sql Server Char and Varchar Data Type



CHAR Data Type is a Fixed Length Data Type.
 For example if you declare a variable/column of CHAR (10) data type, then it will always take 10 bytes irrespective of whether you are storing 1 character or 10 character in this variable or column. And in this example as we have declared this variable/column as CHAR(10), so we can store max 10 characters in this column.

On the other hand VARCHAR is a variable length Data Type.
 For example if you declare a variable/column of VARCHAR (10) data type, it will take the no. of bytes equal to the number of characters stored in this column. So, in this variable/column if you are storing only one character then it will take only one byte and if we are storing 10 characters then it will take 10 bytes. And in this example as we have declared this variable/column as VARCHAR (10), so we can store max 10 characters in this column.

Tuesday 17 July 2012

How to sent mail in ASP .Net ?

How to sent mail in ASP .Net

--use the following namespaces :

using System.Net.Mail;
using System.Net;

**********************************************

--Call the mathod on the button click :
SendMail();

***********************************************

--defination of the method :

private void SendMail() //string body, string msgto, string msgfrom, string sub, string cpcc)
{
System.Text.StringBuilder msg = new System.Text.StringBuilder();
msg.Append("<table style='font-family:Calibri'>");
msg.Append("<tr><td align='left'>Dear " + txtFirstName.Text + ",<br/></td></tr>");
msg.Append("<tr><td><p>you have been successfully registered.</p></td></tr><br/>");
msg.Append("<tr><td><p>Login Details:</p></td></tr><br/>");
msg.Append("<tr><td><p>User Id: " + txtMobileNumber.Text + " and Password: " + txtPassword.Text + "</p></td></tr><br/>");
msg.Append("<tr><td><p>Thanks & Regards,</p></td></tr>");
msg.Append("<tr><td><p>CommSoft E-Services Pvt. Ltd.</p></td></tr>");
msg.Append("</table>");

string To = txtEmail.Text;
string From = "no-reply";

string Subject = "*** Confirmation Registration  ***";

System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
MailMessage message = new MailMessage();
//message.CC.Add(new MailAddress(cc));
message.Body = Convert.ToString(msg);
message.IsBodyHtml = true;
message.To.Add(To);
message.From = new MailAddress(From);
message.Subject = Subject;
message.Priority = MailPriority.High;

SmtpClient SmtpMail = new SmtpClient();
{
SmtpMail.Host = "smtp.gmail.com";
SmtpMail.Port = 4543;
SmtpMail.EnableSsl = true;
SmtpMail.UseDefaultCredentials = true;
SmtpMail.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
SmtpMail.Credentials = new System.Net.NetworkCredential("no-reply", "4545345");
SmtpMail.Send(message);
}

message.Attachments.Dispose();
}
}
}

Tuesday 19 June 2012

Add years(data) in Dropdownlist in .cs page

Add years(data) in Dropdownlist in .cs page


#region BINDYEAR

    private void BINDYEAR()
    {
        try
        {
            int A = 1930;
            for (int i = 0; i < 64; i++)
            {
                ListItem LI = new ListItem();
                LI.Text = Convert.ToInt16(A).ToString();
                LI.Value = A.ToString();
                A = A + 1;
                DropDownList1.Items.Add(LI);
            }
            DropDownList1.Items.Insert(0, "YYYY");

        }
        catch (Exception errmsg)
        {
            Response.Write(errmsg.Message.ToString());
        }
    }
    #endregion

Short cut key in asp.net


Short cut key in asp.net

File
1. New Project (Ctrl+Shift+N )
2. Open Project/Solution (Ctrl+Shift+O)
3. Open File (Ctrl+O)
4. Save (Ctrl+S)
5. Save As (Ctrl+Shift+S)
6. Print (Ctrl+P)
 
Edit
7. Undo (Ctrl+Z)
8. Redo (Ctrl+Y)
 
9. Cut (Ctrl+X)
10. Copy (Ctrl+C)
11. Paste (Ctrl+V)
12. Cycle Clipboard Ring (Ctrl+Shift+V)
13. Select All (Ctrl+A)
14. Quick Find (Ctrl+F)
 
15. Quick Replace (Ctrl+H)
16. Find in Files (Ctrl+Shift+F)
17. Replace in Files (Ctrl+Shift+H)
18. Find Symbol (Alt+F12)
 
19. Format Document (Ctrl+E,D)
20. Format Selection (Ctrl+E,F)
21. Make Uppercase (Ctrl+Shift+U)
22. Make Lowercase (Ctrl+U)
23. Delete Horizontal White Space (Ctrl+E, \)
24. View White Space (Ctrl+E,S)
25. Word Wrap (Ctrl+E,W)
26. Incremental Search (Ctrl+I)
27. Comment Selection (Ctrl+E,C)
28. Uncomment Selection (Ctrl+E,U)
 
29. Toggle Bookmark (Ctrl+B,T)
30. Enable Bookmark (Ctrl+B,E)
31. Previous Bookmark (Ctrl+B,P)
32. Next Bookmark (Ctrl+B, N)
33. Clear Bookmarks (Ctrl+B,C)
34. Add Task List Shortcut (Ctrl+E,T)
 
35. Hide Selection (Ctrl+M, Ctrl+H)
36. Toggle Outlining Expansion (Ctrl+M,M)
37. Toggle All Outlining (Ctrl+M,L)
38. Stop Outlining (Ctrl+M,P)
39. Stop Hiding Current (Ctrl+M, Ctrl+U)
 
40. Generate Method Stub (Ctrl+K,M)
41. List Members (Ctrl+K,L)
42. Parameter Info (Ctrl+K,P)
43. Complete Word (Ctrl+K,W)
44. Insert Snippet (Ctrl+K,X)
45. Surround With (Ctrl+K,S)
 
View
46. Code (F7)
47. Designer (Shift+F7)
48. Server Explorer (Ctrl+W,L)
49. Class View (Ctrl+W,C)
50. Code Definition Window (Ctrl+W,D)
51. Object Browser (Ctrl+W,J)
52. Error List (Ctrl+W,E)
53. Output (Ctrl+W,O)
54. Properties Window (Ctrl+W,P)
55. Task List (Ctrl+W,T)
56. Toolbox (Ctrl+W,X)
 
57. Find Symbol Results (Ctrl+W,Q)
58. Bookmark Window (Ctrl+W,B)
59. Command Window (Ctrl+W,A)
60. Document Outline (Ctrl+W,U)
61. Resource View (Ctrl+W,R)
62. Macro Explorer (Alt+F8)
63. Web Browser (Ctrl+W,W)
 
64. Full Screen (Shift+Alt+Enter)
65. Pending Checkins (Ctrl+W,G)
66. Navigate Backward (Ctrl+-)
67. Navigate Forward (Ctrl+Shift+-)
68. Property Pages (Shift+F4)
 
Refactor
69. Rename (F2)
70. Extract Method (Ctrl+R,M)
71. Encapsulate Field (Ctrl+R,E)
72. Promote Local Variable to Parameter (Ctrl+R,P)
73. Remove Parameters (Ctrl+R,V)
74. Reorder parameters (Ctrl+R,O)
 
Website
75. Add New Item (Ctrl+Shift+A)
76. Add Existing Item (Shift+Alt+A)
 
Build
77. Build Solution (F6)
78. Build Web Site (Shift+F6)
 
Debug
79. Breakpoints (Ctrl+D,B)
80. Immediate (Ctrl+D,I)
 
81. Start Debugging (F5)
82. Start without Debugging (Ctrl+F5)
83. Exceptions (Ctrl+D,E)
84. Step Into (F11)
85. Step Over (F10)
86. Break at Function (Ctrl+D,N)
87. Delete All Breakpoints (Ctrl+Shift+F9)
 
Tools
88. Attach to Process (Ctrl+Alt+P)
89. Code Snippets Manager (Ctrl+K, Ctrl+B)
 
90. Run TemporaryMacro (Ctrl+Shift+P)
91. Record TemporaryMacro (Ctrl+Shift+R)
 
92. Macro Explorer (Alt+F8)
93. Macros IDE (Alt+F11)
 
Help
94. How Do I (Ctrl+F1,H)
95. Search (Ctrl+F1,S)
96. Contents (Ctrl+F1,C)
97. Index (Ctrl+F1,I)
98. Help Favourites (Ctrl+F1,F)
99. Dynamic Help (Ctrl+F1,D)
100. Index Results (Ctrl+F1,T)

Monday 4 June 2012

Garbage collection


The .NET Framework is a garbage-collected environment. Garbage collection is the process of
detecting when objects are no longer in use and automatically destroying those objects, thus
freeing memory.

In .NET, this new garbage collector works so that you as a developer are no longer required to
monitor your code for unneeded objects and destroy them.

With this new garbage collector, you can control certain aspects of its functions, as it works
behind the scenes in your application. Within the SDK documentation, look under the
System.GC class for more information.


Assemblies in .Net

Assemblies can be thought of as the building blocks of your applications.

Without an associated assembly, code will not be able to compile from IL.
As in the Windows DNA world where DLLs and EXEs are the building blocks of applications,

in the .NET world, it is the assembly that is the used as the foundation of applications.
With .NET, it is now possible to have multiple versions of components, or assemblies, running

on the same server side by side. An application will always look for the assembly that built it.
When an application is started in .NET, the application will look for an assembly in the

installation folder. Assemblies that are stored in a local installation folder are referred to as
private assemblies. If the application cannot find the assembly within the installation folder,
the application will turn to the Global Assembly Cache (GAC) for the assembly.

The structure of an assembly

Assemblies contain code that is executed by the Common Language Runtime. The great thing
about assemblies is that they are self-describing.





Assemblies are made up of the following parts:

  • The assembly manifest
  • Type metadata
  • Microsoft Intermediate Language (MSIL) code


The assembly manifest is where the details of the assembly are stored. The assembly is stored
within the DLL or EXE itself. Assemblies can either be single or multifile assemblies and,
therefore, assembly manifests can either be stored in the assembly or as a separate file. The
assembly manifest also stores the version number of the assembly to ensure that the
application always uses the correct version. When you are going to have multiple versions of
an assembly on the same machine, it is important to label them carefully so that the CLR
knows which one to use.



Tuesday 29 May 2012

Counting null and non-null values in SQL Server


Counting null and non-null values in a single query

--create a table
create table employee(
emp_name varchar(50),
salary int)

--insert data in table
insert into employee values ('Rohit',10000)
insert into employee values ('Rahul',NULL)
insert into employee values ('Ravi',NULL)

--To see the data
select * from employee

--count null and not null value
SELECT count(*) FROM employee WHERE salary IS NULL
UNION ALL
SELECT count(*) FROM employee WHERE salary IS NOT NULL 

************************************************************      
                                            or
************************************************************

SELECT COUNT(*), 'null_tally' AS narrative 
  FROM employee 
 WHERE salary IS NULL 
UNION
SELECT COUNT(*), 'not_null_tally' AS narrative 
  FROM employee 
 WHERE salary IS NOT NULL;

How To Delete a null record in SQL Server


How To Delete a null record


--create a table
create table employee(
emp_name varchar(50),
salary int)

--insert data in table
insert into employee values ('Rohit',10000)
insert into employee values ('Rahul',NULL)
insert into employee values ('Ravi',NULL)

--To see the data
select * from employee

--delete all null record
delete employee where salary=null
--result after above code - (0 row(s) affected)

*******************************************************

--for delete null record we use 'is'
delete employee where salary is null
--result after above code - (2 row(s) affected)

Self-join in SQL Server 2005-08

Self-join in SQL Server 2005-08

A table can be joined to itself in a self-join. Use a self-join when you want to create a result set that joins records in a table with other records in the same table. To list a table two times in the same query, you must provide a table alias for at least one of instance of the table name. This table alias helps the query processor determine whether columns should present data from the right or left version of the table.



For self join in sql you can try the following example:
Create table employees:
emp_idemp_nameemp_manager_id
1JohnNull
2Tom1
3Smith1
4Albert2
5David2
6Murphy5
7Petra5


Now to get the names of managers from the above single table you can use sub queries or simply the self join.
Self Join SQL Query to get the names of manager and employees:
select e1.emp_name 'manager',e2.emp_name 'employee'
from employees e1 join employees e2
on e1.emp_id=e2.emp_manager_id

Result:
manageremployee
JohnTom
JohnSmith
TomAlbert
TomDavid
DavidMurphy
DavidPetra

Understanding the Self Join Example
In the above self join query, employees table is joined with itself using table aliases e1 and e2. This creates the two views of a single table.

from employees e1 join employees e2
on e1.emp_id=e2.emp_manager_id

Here e.emp_manager_id passes the manager id from the 2nd view to the first aliased e1 table to get the names of managers.

Thursday 10 May 2012

Singleton class in C#



Singleton in C#



The Singleton pattern ensures that a class only has one instance and provides a global point of access to it from a well-known access point. The class implemented using this pattern is responsible for keeping track of its sole instance rather than relying on global variables to single instances of objects.


Examples :


using System;

public class Singleton
{
   private static Singleton instance;

   private Singleton() {}

   public static Singleton Instance
   {
      get 
      {
         if (instance == null)
         {
            instance = new Singleton();
         }
         return instance;
      }
   }
} 

Thursday 26 April 2012

LINQ in ASP .Net


LINQ quries :


protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Dataset dataContext = 
            new Dataset();
       //bind tables or data source in dataset
        var query = from contact in dataContext.tablename
          where tablename.columnname==1
          select contact;

        GridView1.DataSource = query;
        GridView1.DataBind();
    }
}

For more detail.....click here..........

Thursday 19 April 2012

Get IP Address of current system



Get IP Address :


string ip_address = HttpContext.Current.Request.UserHostAddress.ToString();

***************************************************

get host name :


string hostName = System.Net.Dns.GetHostName();



Friday 13 April 2012

Create the session in ASP .Net


//Create the session 


Session["Name"] = "Rohit Prakash";
//orSession.Add("Name","Rahul");

//retrieving the session any where in the application/page

string Name = (string)Session["Name"];


Off - No session state will be stored
InProc - (The Default) Session state exists within the process the web is using
StateServer - Session data is sent to the configured stateserver service
SQLServer - Session data is store in the configured sql server database


Wednesday 11 April 2012

Introduction of LINQ in ASP .Net


Language-Integrated Query (LINQ)


Language-Integrated Query (LINQ) is an innovation introduced in Visual Studio 2008 and .NET Framework version 3.5 that bridges the gap between the world of objects and the world of data.
Traditionally, queries against data are expressed as simple strings without type checking at compile time or IntelliSense support. Furthermore, you have to learn a different query language for each type of data source: SQL databases, XML documents, various Web services, and so on. LINQ makes a query a first-class language construct in C# and Visual Basic. You write queries against strongly typed collections of objects by using language keywords and familiar operators. The following illustration shows a partially-completed LINQ query against a SQL Server database in C# with full type checking and IntelliSense support.





LINQ query with Intellisense





In Visual Studio you can write LINQ queries in Visual Basic or C# with SQL Server databases, XML documents, ADO.NET Datasets, and any collection of objects that supportsIEnumerable or the generic IEnumerable<T> interface. LINQ support for the ADO.NET Entity Framework is also planned, and LINQ providers are being written by third parties for many Web services and other database implementations.

Saturday 21 January 2012

Difference between Abstract class and Interface in C# .Net


Difference between Abstract class and Interface in C# .Net



What is an Abstract Class?


An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.

What is an Interface?

An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.



Feature
Interface
Abstract class
Multiple inheritance
A class may inherit several interfaces.
A class may inherit only one abstract class.
Default implementation
An interface cannot provide any code, just the signature.
An abstract class can provide complete, default code and/or just the details that have to be overridden.
Access ModfiersAn interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as publicAn abstract class can contain access modifiers for the subs, functions, properties
Core VS Peripheral
Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.
An abstract class defines the core identity of a class and there it is used for objects of the same type.
Homogeneity
If various implementations only share method signatures then it is better to use Interfaces.
If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.
Speed
Requires more time to find the actual method in the corresponding classes.
Fast
Adding functionality (Versioning)
If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.
If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.
Fields and ConstantsNo fields can be defined in interfacesAn abstract class can have fields and constrants defined