Microsoft Dot Net Master

Microsoft Dot Net Master
Microsoft Dot Net Master

Wednesday, August 28, 2013

C# - What is the Difference between Const and Readonly

Const:

  •       Const can only be initialized at the time of declaration of the field.
  •       Const values will evaluate at compile time only.
  •      Const value can’t be changed these will be same at all the time.
  •      This type of fields are required when one of the field values remains constant throughout the system like Pi will remain same in your Maths Class.


Read-only:

  •     The value will be initialized either declaration time or the constructor of the class allowing you to pass the value at run time.
  •     Read only values will evaluate at runtime only.

Difference between Asp.net SessionState and ViewState in C#, VB.NET

View State: 

  • View state is maintained in page level only.
  • View state of one page is not visible in another page.
  • View state information stored in client only.
  • View state persist the values of particular page in the client (browser) when post back operation done.
  • View state used to persist page-instance-specific data.

 Session State: 

  •  Session state is maintained in session level.
  • Session state value is available in all pages within a user session.
  • Session state information stored in server.
  • Session state persist the data of particular user in the server. This data available till user close the browser or session time completes.
  • Session state used to persist the user-specific data on the server side.


 Usage

  • If you want to access the information on different web pages, you can use SessionState
  • If you want to access from the same page, then you can use Viewstate


Security

  •      Session state provides more security when compared with view state as the data value is stored in server side

Tuesday, August 27, 2013

Difference between char varchar and nvarchar in sql server


 Char DataType

Char datatype which is used to store fixed length of characters. Suppose if we declared char(50) it will allocates memory for 50 characters. Once we declare char(50) and insert only 10 characters of word then only 10 characters of memory will be used and other 40 characters of memory will be wasted.

varchar DataType

Varchar means variable characters and it is used to store non-unicode characters. It will allocate the memory based on number characters inserted. Suppose if we declared varchar(50) it will allocates memory of 0 characters at the time of declaration. Once we declare varchar(50) and insert only 10 characters of word it will allocate memory for only 10 characters.

nvarchar DataType

nvarchar datatype same as varchar datatype but only difference nvarchar is used to store Unicode characters and it allows you to store multiple languages in database. nvarchar datatype will take twice as much space to store extended set of characters as required by other languages.

So if we are not using other languages then it’s better to use varchar datatype instead of nvarchar

Differences between ExecuteReader, ExecuteNonQuery AND ExecuteScalar

ExecuteNonQuery

ExecuteNonQuery method will return number of rows effected with INSERT, DELETE or UPDATE operations. This ExecuteNonQuery method will be used only for insert, update and delete, Create, and SET statements.

ExecuteScalar

Execute Scalar will return single row single column value i.e. single value, on execution of SQL Query or Stored procedure using command object. It’s very fast to retrieve single values from database.

ExecuteReader

Execute Reader will be used to return the set of rows, on execution of SQL Query or Stored procedure using command object. This one is forward only retrieval of records and it is used to read the table values from first to last.