External Style Sheets

Introduction :

External style sheets have many powerful that make them ubiquitous in professional Web sites:

  • It keeps your website design and content separate.
  • It's much easier to reuse your CSS code if you have it in a separate file. Instead of typing the same CSS code on every web page you have, simply have many pages refer to a single CSS file with the "link" tag.
  • You can make drastic changes to your web pages with just a few changes in a single CSS file.
  • It allows a single style sheet to control the rendering of multiple documents.
  • This results in a time-savings for the author, a savings of space for the web server, and less download time for the user.
  • This method can be used in both HTML and XML.
  • In Application When using CSS it is preferable to keep the CSS separate from your HTML.
  • Placing CSS in a separate file allows the web designer to completely differentiate between content (HTML) and design(CSS).

An External Style Sheet is a file containing only CSS syntax and should carry a MIME type of "text/css."
It is saved with a ".css" filename extensions that file is then referenced in your HTML using the "link" instead of "style".
By using the Link Tag to load a basic external style sheet (CSS), it's possible to control the look n feel of multiply WebPages by making changes to One style sheet.
This means that it is easy to change font, bgcolor, background, etc on ALL pages - just by changing one external style sheet (CSS).

Those CSS files define page attributes for every page to which they are linked.
The style information is not explicitly tied directly to the document's elements, so Selector syntax is used to specify what styles attach to which portions of the document tree.
The full range of CSS syntax is allowed in this method.

Example with codes :

The 'link' is always added to the Head Section i.e anywhere between the "head" and the "/head"

HTML Code:
    link rel="stylesheet" type="text/css" href="style.css"

Just create a text (ASCII) file named (test.css) that contains the code shown below.
Put the style.css file in the same folder / directory as the file .

Let us create an external CSS file. Open up notepad.exe, or any other plain text editor and type the following CSS code.

CSS Code:
    body
    {
        background-color: #FFFFF0;
        font-family: Arial, Verdana, sans-serif;
        font-size: 18px;
        color: #00008B;
    }

    a
    {
        font-family: Arial, Verdana, sans-serif;
        font-size: 18px;
        color: Blue;
        text-decoration: underline;
    }

    a:hover
    {
        font-family: Arial, Verdana, sans-serif;
        font-size: 18px;
        color: Red;
        background-color: Green;
    }

    h1 
    { 
        font-family: Arial, Verdana, sans-serif; 
        font-size: 32px; 
        color: blue; 
    }

    table
    {
        font-family: Arial, Verdana, sans-serif;
        font-size: 18px;
        color: #00008B;
        margin-top: 0px;
        margin-right: 0px;
        margin-bottom: 0px;
        margin-left: 0px;
        padding-top: 0px;
        padding-right: 0px;
        padding-bottom: 0px;
        padding-left: 0px;
    }

Now save the file as a CSS (test.css) file.
Now create a new HTML file and fill it with the following code.

HTML Code:
    link rel="stylesheet" type="text/css" href="test.css"

Then save this file as "Sample.html" (without the quotes) in the same directory as your CSS file.

Now open your HTML file in your web browser and it should look something like this..

Display:

A Blue Header

Image link in Blue Colour. then on Mouse hover it will be red colur with green background because we changed it with CSS!

Advantages :

External style sheets have many powerful that make them ubiquitous in professional Web sites:
  • It keeps your website design and content separate.
  • It's much easier to reuse your CSS code if you have it in a separate file. Instead of typing the same CSS code on every web page you have, simply have many pages refer to a single CSS file with the "link" tag.
  • You can make drastic changes to your web pages with just a few changes in a single CSS file.
  • It allows a single style sheet to control the rendering of multiple documents.This results in a time-savings for the author, a savings of space for the web server, and less download time for the user.
  • This method can be used in both HTML and XML.

SET Vs. SELECT in SQL Server

Introduction

SET and SELECT both key words are used to Assign Variables in SQL Server.

SET and SELECT both specifies the columns to be changed and the new values for the columns.
The values in the specified columns are updated with the values specified in the SET and SELECT in all rows that match the WHERE clause search condition.
If no WHERE clause is specified, all rows are updated.

There are some difference based on the Performance, Process like Follows :

1. SET is the ANSI standard for variable assignment, SELECT is not.
2. SELECT can be used to assign values to more than one variable at a time, Whereas SET allows to assign data to only one variable at a time.

Example :
    /* Declaring variables */
    DECLARE @Var1 AS int, @Var2 AS int

    /* The same can be done using SET, but two SET statements are needed */
    SET @Var1 = 1
    SET @Var2 = 2

    /* Initializing two variables at once */
    SELECT @Var1 = 1, @Var2 = 2

But use SET instead SELECT, for variable initialization, It will throw the following error

Example :
    SET @Var1 = 1, @Var2 = 2

    Msg 102, Level 15, State 1, Line 10
    Incorrect syntax near ','.

3. When assigning from a query if there is no value returned then SET will assign NULL, where SELECT will not make the assignment at all .so the variable will not be changed from it's previous value.

Example :

Run it in master Database in SQL Server.
    /* Returns NULL */
    DECLARE @Title varchar(80)
    --SET @Title = 'Not Found'

    SET @Title =
    (
    SELECT error
    FROM SysMessages
    WHERE Description = 'Invalid Description'
    )

    SELECT @Title
    GO

    /* Returns the string literal 'Not Found' */
    DECLARE @Title varchar(80)
    SET @Title = 'Not Found'

    SELECT @Title = error
    FROM SysMessages
    WHERE Description = 'Invalid Description'

    SELECT @Title
    GO

4. Let using a query needs to populate a variable and the Query returns more than one value.
SET will fail with an error in this scenario.
But SELECT will assign one of the returned rows and mask the fact that the query returned more than one row.

As a result, bugs in your the could go unnoticed with SELECT, and this type of bugs is hard to track down too.

Example :
    /* Consider the following table with two rows */
    SET NOCOUNT ON
    CREATE TABLE #Table (i int, j varchar(10))
    INSERT INTO #Table (i, j) VALUES (1, 'Sunday')
    INSERT INTO #Table (i, j) VALUES (1, 'Monday')
    GO

    /* Following SELECT will return two rows, but the variable gets its value from one of those rows, without an error.
    you will never know that two rows existed for the condition, WHERE i = 1 */
    DECLARE @j varchar(10)
    SELECT @j = j FROM #Table WHERE i = 1
    SELECT @j
    GO

    /* If you rewrite the same query, but use SET instead, for variable initialization, you will see the following error */
    DECLARE @j varchar(10)
    SET @j = (SELECT j FROM #Table WHERE i = 1)
    SELECT @j

    Msg 512, Level 16, State 1, Line 4
    Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

Based on the above results, when using a query to populate variables, we should always use SET.
If you want to be sure that only one row is returned then only use SELECT, as shown below:

Example :
    DECLARE @j varchar(10)
    SELECT @j = (SELECT j FROM #Table WHERE i = 1)
    SELECT @j

5. This very feature of SELECT makes it a winner over SET, when assigning values to multiple variables. A single SELECT statement assigning values to 3 different variables, is much faster than 3 different SET statements assigning values to 3 different variables.
In this scenario, using a SELECT is at least twice as fast, compared to SET.

So, the conclusion is, if there is a loop in th stored procedure that manipulates the values of several variables, and if you want to squeeze as much performance as possible out of this loop, then do all variable manipulations in one single SELECT statement or group the related variables into few SELECT statements as show below:

Example :
    SELECT @Var1 = @Var1 + 1, @Var2 = @Var2 - 1, @CNT = @CNT + 1

Add a Web User Control Dynamically (At Runtime).

We can add a Web User Control Dynamically (At Runtime) in 2 steps.

Step 1 : Add a "PlaceHolder" in your aspx page.
Step 2 : Write the following code on page load (Code Behind).

Code :
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim UCDynamic As Control
    'WebUserControl.ascx : User Control path
    UCDynamic = LoadControl("WebUserControl.ascx")
    PlaceHolder1.Controls.Add(UCDynamic)

End Sub

To check a primary key exists or not in table

Use the following Query to check whether the Table does have "Primary Key" or not.

Lets assume its Employee Table here. the Query is as Follows :

Example :
    IF OBJECTPROPERTY( OBJECT_ID( '[dbo].[Employee]' ), 'TableHasPrimaryKey' ) = 1
        PRINT '[dbo].[Employee] table has a primary key.'
    ELSE
        PRINT '[dbo].[Employee] table has no primary key.'

Check for Valid Date using JavaScript

The following code is helpful to Validate a Date using javascript.
  1. Call the Function "OnClientClick" of Button (OnClientClick="ValidateForm())
  2. Write the Following Functions in Script tag of Head Section.
Javascript :
    // Declaring valid date character, minimum year and maximum year
    var dtCh= "/";
    var minYear=1900;
    var maxYear=2100;
    
    function isInteger(s)
    {
        var i;
        for (i = 0; i <>
        {
            // Check that current character is number.
            var c = s.charAt(i);
            if (((c < "0") || (c > "9"))) return false;
        }
        // All characters are numbers.
        return true;
    }

    function stripCharsInBag(s, bag)
    {
        var i;
        var returnString = "";
        // Search through string's characters one by one.
        // If character is not in bag, append to returnString.
        for (i = 0; i <>
        {
            var c = s.charAt(i);
            if (bag.indexOf(c) == -1) returnString += c;
        }
        return returnString;
    }

    function daysInFebruary (year)
    {
        // February has 29 days in any year evenly divisible by four,
        // EXCEPT for centurial years which are not also divisible by 400.
        return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
    }
    
    function DaysArray(n)
    {
        for (var i = 1; i <= n; i++)
        {
            this[i] = 31
            if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
            if (i==2) {this[i] = 29}
        }
        return this
    }

    function isDate(dtStr)
    {
        var daysInMonth = DaysArray(12)
        var pos1=dtStr.indexOf(dtCh)
        var pos2=dtStr.indexOf(dtCh,pos1+1)
        var strMonth=dtStr.substring(0,pos1)
        var strDay=dtStr.substring(pos1+1,pos2)
        var strYear=dtStr.substring(pos2+1)
        strYr=strYear
        if (strDay.charAt(0)=="0" && strDay.length>1)
        strDay=strDay.substring(1)
        if (strMonth.charAt(0)=="0" && strMonth.length>1)
        strMonth=strMonth.substring(1)
        
        for (var i = 1; i <= 3; i++)
        {
            if (strYr.charAt(0)=="0" && strYr.length>1)
            strYr=strYr.substring(1)
        }
        
        month=parseInt(strMonth)
        day=parseInt(strDay)
        year=parseInt(strYr)
        
        if (pos1==-1 || pos2==-1)
        {
            alert("The date format should be : mm/dd/yyyy")
            return false
        }
        if (strMonth.length<1>12)
        {
            alert("Please enter a valid month")
            return false
        }
        if (strDay.length<1>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
        {
            alert("Please enter a valid day")
            return false
        }
        if (strYear.length != 4 || year==0 || yearmaxYear)
        {
            alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
            return false
        }
        if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
        {
            alert("Please enter a valid date")
            return false
        }
        return true
    }

    function ValidateForm()
    {
        var dt;
        dt=document.getElementById('txtDate');
        if (isDate(dt.value)==false)
        {
            dt.focus()
            return false
        }
        return true
    }

Reflection

Reflection is one of the features of .Net framework and has greater importance during the development of large applications.

In brief it is a powerful way of collecting and manipulate information present in application's assemblies and its metadata. Metadata contain all the Type information used by the application. The ability to obtain information at run time also makes it even more advantageous.

When reflection is used along with system.type, it allows the developer to get the valuable information about all the types and about the assemblies. We can even create the instances and then invoke various types that are used across the application.

What is Reflection?
  • Reflection is the ability to find out information about objects, the application details (assemblies), its metadata at run-time.
  • This allows application to collect information about itself and also manipulate on itself.
  • It can be used effectively to find all the types in an assembly and/or dynamically invoke methods in an assembly.
  • This includes information about the type, properties, methods and events of an object and to invoke the methods of object Invoke method can be used too.
  • With reflection we can dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.
  • If Attributes (C#) are used in application, then with help of reflection we can access these attributes.
  • It can be even used to emit Intermediate Language code dynamically so that the generated code can be executed directly.
How to use Reflection in our applications?
  • System.Reflection namespace contains all the Reflection related classes. These classes are used to get information from any of the class under .NET framework.
  • The Type class is the root of all reflection operations. Type is an abstract base class that acts as means to access metadata though the reflection classes.
  • Using Type object, any information related to methods, implementation details and manipulating information can be obtained.
  • The types include the constructors, methods, fields, properties, and events of a class, along with this the module and the assembly in which these information are present can be accessed and manipulated easily.
  • we can use reflection to dynamically create an instance of any type, bind the type to an existing object, or get the type from an existing object.
  • Once this is done appropriate method can be invoked, access the fields and properties.
  • This can be done by specifying the Type of object or by specifying both assembly and Type of the object that needs to be created.
  • By this the new object created acts like any other object and associated methods, fields and properties can be easily accessed.
  • With reflection we can also find out about various methods associated with newly created object and how to use these object.
  • To find out the attributes and methods associated with an object we can use the abstract class MemberInfo, this class is available under the namespace System.Reflection.

Add Check Box in Calender Control and Select WeekEnds on click of Button

Format Calender Controls :

Add Check Boxes manually in Calender Control and Select Week Ends on click of Button.



Code :
Partial Class SelectWeekEnds
    Inherits System.Web.UI.Page

    Dim btnclick As Boolean = False

    Protected Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
        Dim ctl As New CheckBox
        ctl.ID = "chk" & e.Day.Date.ToString.Substring(0, 10)
        If btnclick = True Then
            If CInt(e.Day.Date.DayOfWeek) = 0 Or CInt(e.Day.Date.DayOfWeek) = 6 Then
                ctl.Checked = True
            End If
        End If
        e.Cell.Controls.Add(ctl)
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        btnclick = True
    End Sub

End Class

Add Check Box in Calender Control

Add Checkbox in Calender Control manually.


Code :
Partial Class SelectWeekEnds
  Inherits System.Web.UI.Page

  Protected Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
      Dim ctl As New CheckBox
      ctl.ID = "chk" & e.Day.Date.ToString.Substring(0, 10)
      e.Cell.Controls.Add(ctl)
  End Sub  

End Class



Regular Expression for Alphanumeric using Javascript

You can use Javascripts to validate this as followes.
Define the Javascript Function "isNumberEvt" on Head Section. and check it on keypress even of text box like "onkeypress="return isNumberEvt(event)"".

Javascript :
    function isNumberEvt(evt)
    {

        var charCode = (evt.which) ? evt.which : event.keyCode
        if ((charCode > 31 && charCode <> 57 && charCode <> 90 && charCode <> 122))
        return false;

        return true;

    }
aspx:

call function onkeypress="return isNumberEvt(event)" even of Textbox.
    asp:textbox id="TextBox1" onkeypress="return isNumberEvt(event)" runat="server" width="238px"

Check for Internet Connection availability in ASP. Net

Following code Check for Internet Connection availability in ASP. Net

Code :
Partial Class frmChkInternetConnection
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If fnChkInternetConn() Then
            Response.Write("Congrats ! Internet Connection is Available.")
        Else
            Response.Write("Sorry ! Internet Connection is not Available.")
        End If
    End Sub

    Public Function fnChkInternetConn() As Boolean
        Dim objreq As System.Net.HttpWebRequest
        Dim objres As System.Net.HttpWebResponse
        Try
            objreq = CType(System.Net.HttpWebRequest.Create("http://www.google.com"), System.Net.HttpWebRequest)
            objres = CType(objreq.GetResponse(), System.Net.HttpWebResponse)
            objreq.Abort()
            If objres.StatusCode = System.Net.HttpStatusCode.OK Then
                Return True
            End If
        Catch weberrt As System.Net.WebException
            Return False
        Catch except As Exception
            Return False
        End Try
    End Function
End Class