Wednesday 7 December 2016

Difference Between Where and Having Clause



The WHERE clause does not work with aggregates like SUM,Count etc.

The SQL above will not work, because the where clause doesn’t work with aggregates – like sum, avg, max, etc.. Instead, what we will need to use is the having clause. The having clause was added to sql just so we could compare aggregates to other values – just how the ‘where’ clause can be used with non-aggregates. Now, the correct sql will look like this:

Many time i have seen code where Having and where clause are misused see Examples Below -

BAD SQL:
select employee, sum(bonus) from emp_bonus
group by employee where sum(bonus) > 1000;

GOOD SQL:
select employee, sum(bonus) from emp_bonus
group by employee having sum(bonus) > 1000;


Difference between having and where clause

So we can see that the difference between the having and where clause in sql is that the where clause can not be used with aggregates, but the having clause can. One way to think of it is that the having clause is an additional filter to the where clause.

Sql Standard Theory says WHERE Clause restricts the result set before returning rows and HAVING restricts the result set after retrieving all records.That's why WHERE is faster as compared to Having and that's why having is not used more commonly or always as that of Having so it should used in circumstances where it is necessary to use having and nothing else is possible like as in case we are using aggregate functions


Tuesday 6 December 2016

How To Redirect Non-www to www in Asp.net


Please Follow Below Set of Steps to Redirect Non-www To www for your domain in asp.net or asp classic Web Application : -

1. Open web.config file of your Web Application
2. Before Closing of </system.webserver> Add below code in order to redirect your domain
    from xyz.com to http://www.xyz.com for all pages of your website -


<system.webServer>
      <rewrite>
            <rules>
                  <rule name="Redirect domain.com to www" patternSyntax="ECMAScript" stopProcessing="true">
                  <match url=".*" />
                 <conditions>
                     <add input="{HTTP_HOST}" pattern="^xyz.com$" />
                  </conditions>
                  <action type="Redirect" url="http://www.xyz.com/{R:0}" />
              </rule>
    </rules>
</rewrite>
</system.webServer>

3. Save web.config file and Upload it to Hosting Server
4. Now every request to xy.com will be rewritten with http://www.xyz.com

Difference Between System.Web and System.WebServer



System.web is the settings section for IIS6 and the built in cassini server (the test server in Visual Studio 2010 and before where you are targeting asp.net 3.5 or lower)

The system.webserver is a child of the configuration section that means in web.config file <system.webserver> tag is placed under
<configuration> Tag.

System.WebServer Tag is contained in web.config file in root directory of website. This file contains configuration that needs to be implemented on IIS for Current project. Configuration coded in <system.webserver> Tag are implemented on IIS and are only applied to Project or Webapplication to which corresponding web.config Belongs. This Tag Can Exist is one of below formats in your project's web.config file in root directory of Web Application.

Empty <system.webserver> Tag Under <configuration> Tag -

<configuration>
  <system.webServer>
  </system.webServer>
</configuration>

The system.web section is used in web.config file of Asp.net WebApplications for defining Server Configuration that are applicable for IIS 6, while the system.webserver version is used to configure IIS 7.0. IIS 7.0 includes a new ASP.NET pipeline and some configuration differences. you can check complete details on link below -

https://msdn.microsoft.com/en-us/library/bb763179.aspx


System.webserver is the Server Configuration section for IIS 7 and above, and the build in IIS Express server which is used in Visual Studio 2010 targeting asp.net 4 and any newer versions that are released in the future.

Sample of Web.config file Contains Both <system.web> and <system.webserver>

<?xml version="1.0"?>
<!--
    Note: As an alternative to hand editing this file you can use the
    web admin tool to configure settings for your application. Use
    the Website->Asp.Net Configuration option in Visual Studio.
    A full list of settings and comments can be found in
    machine.config.comments usually located in
    \Windows\Microsoft.Net\Framework\v2.x\Config
-->
<configuration>
<appSettings>
<add key="username" value="admin-heemanshu@564496"/>
<add key="password" value="@htennty50!334#"/>
</appSettings>

<connectionStrings/>

<system.web>
<!--
            Set compilation debug="true" to insert debugging
            symbols into the compiled page. Because this
            affects performance, set this value to true only
            during development.

            Visual Basic options:
            Set strict="true" to disallow all data type conversions
            where data loss can occur.
            Set explicit="true" to force declaration of all variables.
        -->
<compilation debug="true" strict="false" explicit="true"/>
<pages>
<namespaces>
<clear/>
<add namespace="System"/>
<add namespace="System.Collections"/>
<add namespace="System.Collections.Specialized"/>
<add namespace="System.Configuration"/>
<add namespace="System.Text"/>
<add namespace="System.Text.RegularExpressions"/>
<add namespace="System.Web"/>
<add namespace="System.Web.Caching"/>
<add namespace="System.Web.SessionState"/>
<add namespace="System.Web.Security"/>
<add namespace="System.Web.Profile"/>
<add namespace="System.Web.UI"/>
<add namespace="System.Web.UI.WebControls"/>
<add namespace="System.Web.UI.WebControls.WebParts"/>
<add namespace="System.Web.UI.HtmlControls"/>
</namespaces>
</pages>

<!--
            The <authentication> section enables configuration
            of the security authentication mode used by
            ASP.NET to identify an incoming user.
               -->

<authentication mode="Windows"/>

<!--
            The <customErrors> section enables configuration
            of what to do if/when an unhandled error occurs
            during the execution of a request. Specifically,
            it enables developers to configure html error pages
            to be displayed in place of a error stack trace.-->
<customErrors mode="Off" defaultRedirect="GenericErrorPage.htm">
<error statusCode="404" redirect="FileNotFound.htm"/>
</customErrors>
</system.web>
 
      <system.webServer>
       <rewrite>
        <rules>
          <rule name="Redirect" patternSyntax="ECMAScript" stopProcessing="true">
            <match url=".*" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^xyz.com$" />
            </conditions>
            <action type="Redirect" url="http://www.xyz.com/{R:0}" />
         </rule>
       </rules>
     </rewrite>
    </system.webServer>

</configuration>