Friday, August 25, 2017

Sealed Vs Private Class

Sealed Class:
Sealed keyword is used to avoid the inheritance of that class. it cannot be inherited.
In inherited hierarchy Sealed class is the leaf node.
Even a public class is declared as Sealed it will be ban to inherit.
No instance of a Sealed class is possible because we cannot create constructor of it.

Private Class:
Private keyword is used to limit the scope. if we declare a class private within a class means it will not be visible out of base class.
Private classes cannot be used directly as this will throw compilation error.
We have must nest a private class within another public class.
We can create constructor for a private class so instance of private class is possible.
Private class is only visible to that class it is defined and within it.
Private class can be inherited but a private class cannot be inherited from a public class. because a child class can not be more accessible than base class. Either parent should be more accessible or equal.
Comments and Suggestions are always Welcome!

Wednesday, August 23, 2017

Ado.Net Components, DataSet or DataReader

ADO.Net:
There are two main components of ado.net which are .Net framework Data provider and DataSet.

1. .Net frameword Data provider are very lightweight, used to connect with database, run the command/query by using Command object and get the results. then it depends upon us how to treat that Result set either we directly process the data or we place in DataSet as per requirements. there are different types of Data Providers in .Net Framework like:
a. .NET Framework Data Provider for SQL Server
it gives data access for Microsoft SQL Server. Uses namespace System.Data.SqlClient.
b. .NET Framework Data Provider for OLE DB
Used for data sources discovered by using OLE DB. Uses namespace System.Data.OleDb.
c. .NET Framework Data Provider for ODBC
It is used for data sources discovered by using ODBC. Uses namespace System.Data.Odbc.
d. .NET Framework Data Provider for Oracle
It provides supports for Oracle data sources. namespace System.Data.OracleClient
e. EntityClient Provider
give data access to Entity Data Model (EDM) applications. System.Data.EntityClient
f. .NET Framework Data Provider for SQL Server Compact 4.0.
It is for Microsoft SQL Server Compact 4.0. using System.Data.SqlServerCe.

Mainly .Net Framework Data Providers uses few core Objects which are give below:
a. Connection: Used to establish connection. its base class is DbConnection
b. Command: used to execute a database command. this object base class is DbCommand
c. DataReader: it reads a forward-only(DataReader reads one row at a time then move forward to next row then next, it will not move back thats why it's called Forward only), read-only(it will just read records from data source does not allow any editing). base class for DataReader objects is DbDataReader class.
d. DataAdapter: it is used to populates the DataSet. basically it provides communication between data source and DataSet to enable data access and manipulation of data.

There are some additional Objects also names as Transaction, CommandBuilder, ConnectionStringBuilder, Parameter, Exception, Error and ClientPermission.

2. The DataSet: it is second component of ado.net. it is mainly designed to data access from any data source or we can say independent of data source, it can be used with different data source like XML data or to manage data local. DataSet can have one or more than one DataTable objects which have rows, columns even primary and foreign keys contraints. we can perform within application qurieson DataSet to manipulate the data.

Choose DataReader or DataSet: when you think you want only to read data and there is no requirements to perform editing then DataReader is preferable and fast also but when you require data to cache it locally in application and do some manipulation and perform some other operations then DataSet is best.

Tuesday, August 22, 2017

SqlDataSource and ObjectDataSource Differences


why we should use SqlDataSource and why ObjectDataSource.
why we do not directly use SqlDatasource and prefer ObjectDataSource even SqlDatasource is less code and fast also.

> Basically ObjectDatasource is business object that calls the stored procedure to get records from database whereas we can do directly this one by using Sqldatasource in very less code.

> Objectdatasource provide a perfect abstraction of our design and code.

> While using objectdatasource our pages does not depend upon the database information used in code behind.

> SqlDataSource is typically two-tier in nature but ObjectDataSource is based on three-tier architecture.

> SqlDataSource requires very less line of code to connect with database and ObjectDataSource needs more coding to build data access classes to communicate with database.

> SqlDataSource does not support data encapsulation whereas ObjectDataSource is fully designed for encapsulation.

> For SqlDataSource we write complete connection string for database connection. ObjectDataSource exposes TypeName attribute as a middle layer class mainly used to perform database operations.

> Sqldatasource uses ADO.NET mainly. we have to write all database queries, connections and commands on the page which looks very raw data.

> Objectdatasource behave like a wrapper which control all crud operations.

> Normally if we have to do same thing for a short time span and for a limited time perioed then it's okay to use SqlDatasource but when we talk about a big application and for long run we prefer to use Objectdatasource because direcltly use sqldatasource in your code behind is very quick but dirty habbit which is not preferable always.

> When we face frequent changes in our system then we have to go for page by page and change all sections of code which are directly accessing that change. so by using objectdatasource it is very professional and pretty to have such changes in practice.

Reference: https://forums.asp.net/t/1876836.aspx?When+do+I+use+SqlDataSource+vs+EntityDataSource+vs+ObjectDataSource

Get Total Count From Second Table - SQL Server

How to get count of a column from second table if there is any entry using SQL Server.

Hi, Today we will check that how we can get count or number of rows in a second table agains a column in first table. for example we have a table of products and we want to check total number of orders against a product. this can be done in various ways but we want to avoid sub query as mostly i don't prefer sub query due to efficiency of execution.

For this problem resolution we can use COMMON TABLE EXPRESSION. basically a common table expression (CTE) is a temporary result set. there are some limitations also but as above mentioned task we can use CTE which is not going to fall us in performance issue. As a comparison to temp table there is no need to worry about it, in CTE, as these have an execution scope within statement. CTE can also be used within Views.

Anyway Let's have a look practically: we want to get count for each product from order table but it should also be shown as column in normal query we don't want to use union or some other technique to combine the result set.

For example: normally we are showing mobile products in table and we get result from below query:
SELECT PROD_ID,PROD_TITLE 
    FROM PRODUCT_TABLE  
    WHERE CATEGORY_NAME = 'MOBILE'

and result show like this

but we want to show the count of each product as third column as shown below:

so we will use a common table expression to get this done very simple. below is shown common table expression:
;WITH CTE_PROD_COUNT (PROD_ID,TOTAL_COUNT) 
AS (
     SELECT PROD_ID,COUNT(*)TOTAL_COUNT 
      FROM ORDER_TABLE GROUP BY PROD_ID)

so how we will combine this query with our simple product query to get it done in a single result set. we will use CTE common table expression as table simple table and join it will our original query to get count for each table as below:
;WITH CTE_PROD_COUNT (PROD_ID,TOTAL_COUNT) 
  AS (
        SELECT PROD_ID,COUNT(*)TOTAL_COUNT 
          FROM ORDER_TABLE GROUP BY PROD_ID)
    
   SELECT P.PROD_ID,P.PROD_TITLE,CT.TOTAL_COUNT 
   FROM PRODUCT_TABLE P 
   LEFT OUTER JOIN CTE_PROD_COUNT CT ON CT.PRODID1 = P.PROD_ID
    WHERE P.CATEGORY_NAME = 'MOBILE'

With this query you can use your current filter and CTE will simple add a total count of each product in query as a column.
it will look like this:

Hopefully you like this experience. Comments and suggestions are always welcome!!

Sunday, August 20, 2017

Disadvantage of Session or Requests stuck in IIS due to RequestAcquireState

Some of the clients were complaining about website speed so Last month I was monitoring web server and found out that there is a request queue and server is not efficiently handling the entire request rather stuck on the event called RequestAcquireState due to which a queue is built on server. After searching I’ve come to the conclusion that it was happening due to session used in the website and there are pages where we are using sessions. So whenever user request the page where session is used it locks down other requests from entering into that page which causes slowness in web application and our clients were complaining about speed.



I’ve read about session in details and came to know about concurrency issue of sessions. If you send two requests at the same time, ASP won't even start on the second request until the first one is finished. This can cause unexpected and poor performance on applications that use a lot of AJAX or other logic that is supposed to be asynchronous.

So finally I’ve removed session from website pages and rather use query string and other techniques to send data from one page to another which solves my problem.

Hope this tutorial will be helpful for you.