What are user controls?
User controls are custom, reusable controls, and they use the same techniques
that are employed by HTML and Web server controls. They offer an easy way
to partition and reuse common user interfaces across ASP.NET Web applications.
They use the same Web Forms programming model on which a Web Forms page works.
How to create a user control
The syntax you use to create a user control is similar to the syntax you use to create
a Web Forms page (.aspx). The only difference is that a user control does not include
the <html>, <body>, and <form> elements since a Web Forms page hosts the user control.
To create a user control, follow these steps:
the properties, methods, and events.
2.Create a user interface for the user control.
How to use a user control in a Web Forms page
How to create an instance of a user control programmatically in the code behind fileof a Web Forms pageThe previous example instantiated a user control declaratively in a Web Forms page using the@ Register directive. However, you can instantiate a user control dynamically and add it to the page. Here are the steps for doing that:
How a user control is processedWhen a page with a user control is requested, the following occurs:
as well as the code written inside the .ascx file. What are custom controls?Custom controls are compiled code components that execute on the server, expose the object model,and render markup text, such as HTML or XML, as a normal Web Form or user control does. How to choose the base class for your custom controlTo write a custom control, you should directly or indirectly derive the new class from theSystem.Web.UI.Control class or from the System.Web.UI.WebControls.WebControl class:
derive the new class with these existing classes and can change their default behavior. In brief, the Control class provides the basic functionality by which you can place it in the control tree for a Page class. The WebControl class adds the functionality to the base Control class for displaying visual content on the client computer. For example, you can use the WebControl class to control the look and styles through properties like font, color, and height. How to create and use a simple custom control that extends from System.Web.UI.Control usingVisual Studio
What are the basic differences between user controls and custom controls?Now that you have a basic idea of what user controls and custom controls are and how to create them, let's take a quick look at the differences between the two.
|
Wednesday, 13 March 2013
Difference between User controls and Custom controls in asp.net
Wednesday, 6 March 2013
What are the different ways to replace NULL values in SQL Server
What are the different ways to replace NULL values in SQL Server
we can use COALESCE() to replace NULL values SQL Server. That's very good answer, but do you know of any other way?
Apart from using COALESCE(), there are 2 other ways to replace NULL values in SQL Server. Let's understand this with an example.
I have a Table tblEmployee, as shown in the diagram below. Some of the Employees does not have gender. All those employees who does not have Gender, must have a replacement value of 'No Gender' in your query result. Let's explore all the 3 possible options we have.
Option 1 : Replace NULL values in SQL Server using ISNULL() function.
Select Name, ISNULL(Gender,'No Gender') as Gender
From tblEmployee
Option 2 : Replace NULL values in SQL Server using CASE.
Select Name, Case When Gender IS NULL Then 'No Gender' Else Gender End as Gender
From tblEmployee
Option 3 : Replace NULL values in SQL Server using COALESCE() function.
Select Name, Coalesce(Gender, 'No Gender') as Gender
From tblEmployee
Apart from using COALESCE(), there are 2 other ways to replace NULL values in SQL Server. Let's understand this with an example.
I have a Table tblEmployee, as shown in the diagram below. Some of the Employees does not have gender. All those employees who does not have Gender, must have a replacement value of 'No Gender' in your query result. Let's explore all the 3 possible options we have.
![]() |
Option 1 : Replace NULL values in SQL Server using ISNULL() function.
Select Name, ISNULL(Gender,'No Gender') as Gender
From tblEmployee
Option 2 : Replace NULL values in SQL Server using CASE.
Select Name, Case When Gender IS NULL Then 'No Gender' Else Gender End as Gender
From tblEmployee
Option 3 : Replace NULL values in SQL Server using COALESCE() function.
Select Name, Coalesce(Gender, 'No Gender') as Gender
From tblEmployee
What is the use of COALESCE in SQL Server
What is the use of COALESCE in SQL Server
Let us understand the use of COALESCE with the help of an example.
In this example, the Candidate table is shown to include three columns with information about a Candidate:
1. Candidate_id
2. PrimaryEmail
3. SecondaryEmail
COALESCE in the SELECT statement below, selects the PrimaryEmail if it is not null. If the PrimaryEmail is null then SecondaryEmail will be selected. If both PrimaryEmail and SecondaryEmail is present then only PrimaryEmail is selected. So, COALESCE returns the first nonnull column among the list of columns passed. If both PrimaryEmail and SecondaryEmail is NULL, COALESCE returns NULL.
COALESCE can also be used in joins as shown in the example below. If the Candidate table has a non null value in the Email column, then the value is selected. If the Email column is null in the Candidate Table then, CompanyEmail from CandidateCompany Table is selected.
Let us understand the use of COALESCE with the help of an example.
In this example, the Candidate table is shown to include three columns with information about a Candidate:
1. Candidate_id
2. PrimaryEmail
3. SecondaryEmail
COALESCE in the SELECT statement below, selects the PrimaryEmail if it is not null. If the PrimaryEmail is null then SecondaryEmail will be selected. If both PrimaryEmail and SecondaryEmail is present then only PrimaryEmail is selected. So, COALESCE returns the first nonnull column among the list of columns passed. If both PrimaryEmail and SecondaryEmail is NULL, COALESCE returns NULL.
![]() |
![]() |
Monday, 4 March 2013
base keyword in c#
base keyword in c#
In c#, to access members and functionalities of the base class, the ‘base’ keyword is used within a derived class. In the given code, we have used ‘base’ keyword to call the constructor of the Account class.
class Account { private string mCode; private string mName; private string mDescription; private double mBalance; public Account(string code, string name, string description, double balance) { mCode = code; mName = name; mDescription = description; mBalance = balance; } public Account() { }
In the below code, the derived class constructor – ‘PartyAccount’ receives the values first and which are then passed to the base class constructor.
class PartyAccount :Account { private string mAddress; private string mPhone; public PartyAccount(string code, string name, string description, double balance, string address, string phone) : base(code, name, description, balance) { mAddress = address; mPhone = phone; } public PartyAccount() : base() { }
Note : The ‘MyBase’ keyword in vb.net is the counterpart for the ‘base’ keyword in c#.
Connected and DisConnected Architecture of ADO.NET
Connected Architecture of ADO.NET
The architecture of ADO.net, in which connection must be opened to access the data retrieved from database is called as connected architecture. Connected architecture was built on the classes connection, command, datareader and transaction.

Connection : in connected architecture also the purpose of connection is to just establish a connection to database and it self will not transfer any data.
DataReader : DataReader is used to store the data retrieved by command object and make it available for .net application. Data in DataReader is read only and within the DataReader you can navigate only in forward direction and it also only one record at a time. To access one by one record from the DataReader, call Read() method of the DataReader whose return type is bool. When the next record was successfully read, the Read() method will return true and otherwise returns false.
Disconnected Architecture in ADO.NET
The architecture of ADO.net in which data retrieved from database can be accessed even when connection to database was closed is called as disconnected architecture. Disconnected architecture of ADO.net was built on classes connection, dataadapter, commandbuilder and dataset and dataview.

Connection : Connection object is used to establish a connection to database and connection it self will not transfer any data.
DataAdapter : DataAdapter is used to transfer the data between database and dataset. It has commands like select, insert, update and delete. Select command is used to retrieve data from database and insert, update and delete commands are used to send changes to the data in dataset to database. It needs a connection to transfer the data.
CommandBuilder : by default dataadapter contains only the select command and it doesn’t contain insert, update and delete commands. To create insert, update and delete commands for the dataadapter, commandbuilder is used. It is used only to create these commands for the dataadapter and has no other purpose.
DataSet : Dataset is used to store the data retrieved from database by dataadapter and make it available for .net application.
To fill data in to dataset fill() method of dataadapter is used and has the following syntax.
Da.Fill(Ds,”TableName”);
When fill method was called, dataadapter will open a connection to database, executes select command, stores the data retrieved by select command in to dataset and immediately closes the connection.
As connection to database was closed, any changes to the data in dataset will not be directly sent to the database and will be made only in the dataset. To send changes made to data in dataset to the database, Update() method of the dataadapter is used that has the following syntax.
Da.Update(Ds,”Tablename”);
When Update method was called, dataadapter will again open the connection to database, executes insert, update and delete commands to send changes in dataset to database and immediately closes the connection. As connection is opened only when it is required and will be automatically closed when it was not required, this architecture is called disconnected architecture.
A dataset can contain data in multiple tables.
DataView : DataView is a view of table available in DataSet. It is used to find a record, sort the records and filter the records. By using dataview, you can also perform insert, update and delete as in case of a DataSet.
What are Value Types and Reference Types in .Net
What are Value Types and Reference Types in .Net
According to MSDN, A data type is a value type if it holds the data within its own memory allocation. A reference type contains a pointer to another memory location that holds the data.
All the data types in .net are classified in to value types and reference types.
Value types include the following:
----------------------Reference types include the following:
All the data types in .net are classified in to value types and reference types.
- The data types whose values are directly stored in stack memory area are called as value types and the data types whose values are stored in heap memory area and its address is stored in a variable in stack memory area are called as reference types.
- Among all built in data types of .net string and object are reference type and all other data types are value types.
- Among user defined data types, class, interface, delegate and arrays are reference type while structure and enumeration are value type.
Value types include the following:
- All numeric data types
- Boolean, Char, and Date
- All structures, even if their members are reference types
- Enumerations, since their underlying type is always SByte, Short, Integer, Long, Byte, UShort, UInteger, or ULong
----------------------Reference types include the following:
- String
- All arrays, even if their elements are value types
- Class types
- Delegates
Calling base class constructor in C#
Calling base class constructor in C#
If we derive a class from a base class and want to pass data from the constructor of the derived class to the constructor of the base class, it is necessary to call base constructor .
In the inheritance hierarchy, always the base class constructor is called first. In c#, the base keyword is used to access the base class constructor as shown below.
In the below code we declare a constructor in a derived class. We have used the ‘:base(...)’ keyword after the constructor declaration with a specific parameter list.
The above two constructors are written to initialize base class members. The parameterless constructor of the base class is executed when we instantiate the class without passing any parameters. The other base class constructor is executed when we pass parameters while instantiating the object.
If we derive a class from a base class and want to pass data from the constructor of the derived class to the constructor of the base class, it is necessary to call base constructor .
In the inheritance hierarchy, always the base class constructor is called first. In c#, the base keyword is used to access the base class constructor as shown below.
In the below code we declare a constructor in a derived class. We have used the ‘:base(...)’ keyword after the constructor declaration with a specific parameter list.
class Account { private string mCode; private string mName; private string mDescription; private double mBalance; public Account(string code, string name, string description, double balance) { mCode = code; mName = name; mDescription = description; mBalance = balance; } public Account() { } class PartyAccount :Account { private string mAddress; private string mPhone; public PartyAccount(string code, string name, string description, double balance, string address, string phone) : base(code, name, description, balance) { mAddress = address; mPhone = phone; } public PartyAccount() : base() { }
The above two constructors are written to initialize base class members. The parameterless constructor of the base class is executed when we instantiate the class without passing any parameters. The other base class constructor is executed when we pass parameters while instantiating the object.
Subscribe to:
Posts (Atom)