The DataReader object represents the results of a database query. You get a DataReader by calling a Command object’s ExecuteReader() method.
Whether a DataReader represents any rows by checking the HasRows property or calling the Read() method. The Read() method returns true when the DataReader can advance to a new row.
The DataReader represents a single row of data at a time. To get the next row of data, you need to call the Read() method. When you get to the last row, the Read() method returns False.
There are multiple ways to refer to the columns returned by a DataReader.
If you want to retrieve the value of the Title column for the current row represented by a DataReader, then you can use any of the following methods:
string title = (string)reader[“<FieldName>”];
string title = (string)reader[0];
string title = reader.GetString(0);
SqlString title = reader.GetSqlString(0);
The first method returns the Title column by name. The value of the Title column is returned as an Object. Therefore, you must cast the value to a string before you can assign the value to a string variable.
The second method returns the Title column by position. It also returns the value of the Title column as an Object, so you must cast the value before using it.
The third method returns the Title column by position. However, it retrieves the value as a String value. You don’t need to cast the value in this case.
The fourth method returns the Title column by position. However, it returns the value as a SqlString rather than a normal String. A SqlString represents the value using the specialized data types defined in the System.Data.SqlTypes namespace.
Whether a DataReader represents any rows by checking the HasRows property or calling the Read() method. The Read() method returns true when the DataReader can advance to a new row.
The DataReader represents a single row of data at a time. To get the next row of data, you need to call the Read() method. When you get to the last row, the Read() method returns False.
There are multiple ways to refer to the columns returned by a DataReader.
If you want to retrieve the value of the Title column for the current row represented by a DataReader, then you can use any of the following methods:
string title = (string)reader[“<FieldName>”];
string title = (string)reader[0];
string title = reader.GetString(0);
SqlString title = reader.GetSqlString(0);
The first method returns the Title column by name. The value of the Title column is returned as an Object. Therefore, you must cast the value to a string before you can assign the value to a string variable.
The second method returns the Title column by position. It also returns the value of the Title column as an Object, so you must cast the value before using it.
The third method returns the Title column by position. However, it retrieves the value as a String value. You don’t need to cast the value in this case.
The fourth method returns the Title column by position. However, it returns the value as a SqlString rather than a normal String. A SqlString represents the value using the specialized data types defined in the System.Data.SqlTypes namespace.
The ASP.NET parameter object has the following properties:
- ConvertEmptyStringToNull : When true, if a parameter represents an empty string then the empty string is converted to the value Nothing (null) before the associated command is executed.
- DefaultValue : When a parameter has the value Nothing (null), the DefaultValue is used for the value of the parameter.
- Direction : Indicates the direction of the parameter. Possible values are Input, InputOutput, Output, and ReturnValue.
- Name : Indicates the name of the parameter. Do not use the @ character when indicating the name of an ASP.NET parameter.
- Size : Indicates the data size of the parameter.
- Type : Indicates the .NET Framework type of the parameter. You can assign any value from the TypeCode enumeration to this property.
Variables of reference types, referred to as objects, store references to the actual data. This section introduces the following keywords used to declare reference types:
- class
- interface
- delegate
- object
- string
The XMLHttpRequest object uses JavaScript to make requests to the server and process the response, thereby minimizing the postback delays. It can be called as an API that can be used by JavaScript to transfer XML and other formats of data between the server and client using HTTP. Microsoft invented this object; it was initially designed to load XML documents from JavaScript. The name XMLHttpRequest might be misleading because of the term XML, but, in fact, you can transfer the data in XML or other textbased formats. These formats can be plain text, HTML, or JSON. JSON is an acronym for JavaScript Object Notation. The XMLHttpRequest object plays an important role in the development of Ajax-style applications to implement responsive and dynamic web applications.
Creating the XMLHttpRequest Object
You can create the object for ActiveX by passing the name of the object to its constructor:
var xmlHttp = new ActiveXObject(“Microsoft.XMLHTTP”);
The preceding syntax works for IE browsers; however, for Mozilla and Safari browsers, you can just make a call to the constructor of the XMLHttpRequest class without any arguments:
var xmlHttp = new XmlHttpRequest();
For Mozilla, Safari, and Opera browsers, this object is a native JavaScript object, as compared to IE 5.0 and 6.0 versions, where this comes as an ActiveXObject.Ironically, IE 7.0 has also made the XMLHttpRequest object a native JavaScript object. Because this object has different implementations for different browsers, and because this object is used in every web page for performing request and response, we can encapsulate all this into a single JavaScript file and expose a method called getXmlHttpRequestObject().
Let’s put this method and the variable xmlHttp in a JavaScript file called xmlhttp.js and include this JavaScript file in every web page. The later hours that use the XMLHttpRequest object will use this JavaScript file. The variable xmlHttp now holds the instance of the XMLHttpRequest object.
The following is the code snippet in the xmlhttp.js JavaScript file:
var xmlHttp = false;
function getXmlHttpRequestObject() {
// check for native XMLHttpRequest object
if(window.XMLHttpRequest && !(window.ActiveXObject)) {
try {
xmlHttp = new XMLHttpRequest();
}
catch(e) {
xmlHttp = false;
}
}
// check for IE/Windows ActiveX version
else if(window.ActiveXObject) {
try {
xmlHttp = new ActiveXObject(“Msxml2.XMLHTTP”);
}
catch(e) {
try {
xmlHttp = new ActiveXObject(“Microsoft.XMLHTTP”);
}
catch(e) {
xmlHttp = false;
}
}
}
}
The getXmlHttpRequestObject() method encapsulates the functionality of instantiating the XMLHttpRequest object for different browsers. To perform any request using this object, we simply check the status of the variable xmlHttp. If it validates to false, then the object has not been instantiated—or else you get an instance of the XMLHttpRequest in this variable for use. The function getXmlHttpRequest() first checks for the native XMLHttpRequest object. Refer to the following code snippet:
if(window.XMLHttpRequest && !(window.ActiveXObject))
If window.XMLHttpRequest validates to true and window.ActiveXObject validates to false, then the native object instance is created and set to the variable xmlHttp.
This is executed for the Mozilla, Safari, Opera, and IE 7.0 browsers. The “else if” block checks for the existence of ActiveXObject. Refer to the following code snippet:
try {
xmlHttp = new ActiveXObject(“Msxml2.XMLHTTP”);
}
catch(e) {
try {
xmlHttp = new ActiveXObject(“Microsoft.XMLHTTP”);
}
catch(e) {
xmlHttp = false;
}
If the browser type is IE, an instance of type ActiveXObject is created and is set to the variable xmlHttp.
Creating the XMLHttpRequest Object
You can create the object for ActiveX by passing the name of the object to its constructor:
var xmlHttp = new ActiveXObject(“Microsoft.XMLHTTP”);
The preceding syntax works for IE browsers; however, for Mozilla and Safari browsers, you can just make a call to the constructor of the XMLHttpRequest class without any arguments:
var xmlHttp = new XmlHttpRequest();
For Mozilla, Safari, and Opera browsers, this object is a native JavaScript object, as compared to IE 5.0 and 6.0 versions, where this comes as an ActiveXObject.Ironically, IE 7.0 has also made the XMLHttpRequest object a native JavaScript object. Because this object has different implementations for different browsers, and because this object is used in every web page for performing request and response, we can encapsulate all this into a single JavaScript file and expose a method called getXmlHttpRequestObject().
Let’s put this method and the variable xmlHttp in a JavaScript file called xmlhttp.js and include this JavaScript file in every web page. The later hours that use the XMLHttpRequest object will use this JavaScript file. The variable xmlHttp now holds the instance of the XMLHttpRequest object.
The following is the code snippet in the xmlhttp.js JavaScript file:
var xmlHttp = false;
function getXmlHttpRequestObject() {
// check for native XMLHttpRequest object
if(window.XMLHttpRequest && !(window.ActiveXObject)) {
try {
xmlHttp = new XMLHttpRequest();
}
catch(e) {
xmlHttp = false;
}
}
// check for IE/Windows ActiveX version
else if(window.ActiveXObject) {
try {
xmlHttp = new ActiveXObject(“Msxml2.XMLHTTP”);
}
catch(e) {
try {
xmlHttp = new ActiveXObject(“Microsoft.XMLHTTP”);
}
catch(e) {
xmlHttp = false;
}
}
}
}
The getXmlHttpRequestObject() method encapsulates the functionality of instantiating the XMLHttpRequest object for different browsers. To perform any request using this object, we simply check the status of the variable xmlHttp. If it validates to false, then the object has not been instantiated—or else you get an instance of the XMLHttpRequest in this variable for use. The function getXmlHttpRequest() first checks for the native XMLHttpRequest object. Refer to the following code snippet:
if(window.XMLHttpRequest && !(window.ActiveXObject))
If window.XMLHttpRequest validates to true and window.ActiveXObject validates to false, then the native object instance is created and set to the variable xmlHttp.
This is executed for the Mozilla, Safari, Opera, and IE 7.0 browsers. The “else if” block checks for the existence of ActiveXObject. Refer to the following code snippet:
try {
xmlHttp = new ActiveXObject(“Msxml2.XMLHTTP”);
}
catch(e) {
try {
xmlHttp = new ActiveXObject(“Microsoft.XMLHTTP”);
}
catch(e) {
xmlHttp = false;
}
If the browser type is IE, an instance of type ActiveXObject is created and is set to the variable xmlHttp.