Definition and Usage

Definition and Usage

Definition and Usage

The CompareValidator control is used to compare the value of one input control to the value of another input control or to a fixed value.

Note:If the input control is empty, the validation will succeed. Use the RequiredFieldValidator control to make the field required.

Properties

Property / Description
BackColor / The background color of the CompareValidator control
ControlToCompare / The name of the control to compare with
ControlToValidate / The id of the control to validate
Display / The display behavior for the validation control. Legal values are:
  • None (the control is not displayed. Used to show the error message only in the ValidationSummary control)
  • Static (the control displays an error message if validation fails. Space is reserved on the page for the message even if the input passes validation.
  • Dynamic (the control displays an error message if validation fails. Space is not reserved on the page for the message if the input passes validation

EnableClientScript / A Boolean value that specifies whether client-side validation is enabled or not
Enabled / A Boolean value that specifies whether the validation control is enabled or not
ErrorMessage / The text to display in the ValidationSummary control when validation fails.Note:This text will also be displayed in the validation control if the Text property is not set
ForeColor / The foreground color of the control
id / A unique id for the control
IsValid / A Boolean value that indicates whether the control specified by ControlToValidate is determined to be valid
Operator / The type of comparison to perform. The operators are:
  • Equal
  • GreaterThan
  • GreaterThanEqual
  • LessThan
  • LessThanEqual
  • NotEqual
  • DataTypeCheck

runat / Specifies that the control is a server control. Must be set to "server"
Text / The message to display when validation fails
Type / Specifies the data type of the values to compare. The types are:
  • Currency
  • Date
  • Double
  • Integer
  • String

ValueToCompare / A specified value to compare with

Small number:br />

asp:TextBox runat="server" id="txtSmallNumber"/<br /<br />

Big number:br />

asp:TextBox runat="server" id="txtBigNumber"/<br />

asp:CompareValidator runat="server" id="cmpNumbers" controltovalidate="txtSmallNumber" controltocompare="txtBigNumber" operator="LessThan" type="Integer" errormessage="The first number should be smaller than the second number!"/<br />

VB.Net Operator Precedence

Prededence / Operator / Meaning / Associativity
15 / () / Expression Grouping1 / Inside-to-outside
14 / . / Member evaluation / Left-to-right
13 / ^ / Exponentiation / Left-to-right
12 / - / Unary Minus / Left-to-right
12 / + / Unary Plus / Left-to-right
11 / * / Multiplication / Left-to-right
11 / / / Division / Left-to-right
10 / \ / Integer Division / Left-to-right
9 / Mod / Remainder / Left-to-right
8 / + / Addition / Left-to-right
8 / - / Subtraction / Left-to-right
8 / + / String Concatenation / Left-to-right
7 / String Concatenation / Left-to-right
6 / Bit Shift Left / Left-to-right
6 / Bit Shift Right / Left-to-right
5 / = / Equals / Left-to-right
5 / Not Equals / Left-to-right
5 / Less Than / Left-to-right
5 / <= / Less Than Or Equals / Left-to-right
5 / Greater Than / Left-to-right
5 / >= / Greater Than Or Equals / Left-to-right
5 / Like / String Pattern Matching / Left-to-right
5 / Is / Reference Equality / Left-to-right
5 / TypeOf..Is / Check Type / Left-to-right
4 / Not / Negation / Left-to-right
3 / And / Complete Evaluation And2 / Left-to-right
3 / AndAlso / Short Circuit And2 / Left-to-right
2 / Or / Complete Evaluation Or2 / Left-to-right
2 / OrElse / Short Circuit Or2 / Left-to-right
2 / XOr / Exclusive Or2 / Left-to-right
1 / = / Assignment / None3

1Parentheses are also used for subroutine and function evaluation and for array access.

2And, Or, and XOr can be either logical operators of Boolean or bitwise operators on Integer.

3Only one assignment operator is allowed per line.
Other assignment operators, new with VB.Net, are+=,-=,*=,/=,\=,^=, and&=.

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. VB.Net is rich in built-in operators and provides following type of commonly used operators:

  • Arithmetic Operators
  • Comparison Operators
  • Logical/Bitwise Operators
  • Bit Shift Operators
  • Assignment Operators
  • Miscellaneous Operators

This tutorial will explain the most commonly used operators.

Arithmetic Operators

Following table shows all the arithmetic operators supported by VB.Net. Assume variableAholds 2 and variableBholds 7 then:

Show Examples

Operator / Description / Example
^ / Raises one operand to the power of another / B^A will give 49
+ / Adds two operands / A + B will give 9
- / Subtracts second operand from the first / A - B will give -5
* / Multiply both operands / A * B will give 14
/ / Divide one operand by another and returns a floating point result / B / A will give 3.5
\ / Divide one operand by another and returns an integer result / B \ A will give 3
MOD / Modulus Operator and remainder of after an integer division / B MOD A will give 1

Comparison Operators

Following table shows all the comparison operators supported by VB.Net. Assume variableAholds 10 and variableBholds 20 then:

Show Examples

Operator / Description / Example
== / Checks if the value of two operands is equal or not, if yes then condition becomes true. / (A == B) is not true.
Checks if the value of two operands is equal or not, if values are not equal then condition becomes true. / (A > B) is true.
Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. / (A > B) is not true.
Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. / (A < B) is true.
>= / Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. / (A >= B) is not true.
<= / Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. / (A <= B) is true.

Apart from the above, VB.Net provides three more comparison operators, which we will be using in forthcoming chapters; however, we give a brief description here.

  • IsOperator - It compares two object reference variables and determines if two object references refer to the same object without performing value comparisons. If object1 and object2 both refer to the exact same object instance, result isTrue; otherwise, result is False.
  • IsNotOperator - It also compares two object reference variables and determines if two object references refer to different objects. If object1 and object2 both refer to the exact same object instance, result isFalse; otherwise, result is True.
  • LikeOperator - It compares a string against a pattern.

Logical/Bitwise Operators

Following table shows all the logical operators supported by VB.Net. Assume variable A holds Boolean value True and variable B holds Boolean value False then:

Show Examples

Operator / Description / Example
And / It is the logical as well as bitwise AND operator. If both the operands are true then condition becomes true. This operator does not perform short-circuiting, i.e., it evaluates both the expressions. / (A And B) is False.
Or / It is the logical as well as bitwise OR operator. If any of the two operands is true then condition becomes true. This operator does not perform short-circuiting, i.e., it evaluates both the expressions. / (A Or B) is True.
Not / It is the logical as well as bitwise NOT operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. / Not(A And B) is True.
Xor / It is the logical as well as bitwise Logical Exclusive OR operator. It returns True if both expressions are True or both expressions are False; otherwise it returns False. This operator does not perform short-circuiting, it always evaluates both expressions and there is no short-circuiting counterpart of this operator / A Xor B is True.
AndAlso / It is the logical AND operator. It works only on Boolean data. It performs short-circuiting. / (A AndAlso B) is False.
OrElse / It is the logical OR operator. It works only on Boolean data. It performs short-circuiting. / (A OrElse B) is True.
IsFalse / It determines whether an expression is False.
IsTrue / It determines whether an expression is True.

Bit Shift Operators

We have already discussed the bitwise operators. The bit shift operators perform the shift operations on binary values. Before coming into the bit shift operators, let us understand the bit operations.

Bitwise operators work on bits and perform bit by bit operation. The truth tables for &, |, and ^ are as follows:

p / q / p & q / p | q / p ^ q
0 / 0 / 0 / 0 / 0
0 / 1 / 0 / 1 / 1
1 / 1 / 1 / 1 / 0
1 / 0 / 0 / 1 / 1

Assume if A = 60; and B = 13; Now in binary format they will be as follows:

A = 0011 1100

B = 0000 1101

------

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A = 1100 0011

We have seen that the Bitwise operators supported by VB.Net are And, Or, Xor and Not. The Bit shift operators are > and < for left shift and right shift respectively.

Assume that the variable A holds 60 and variable B holds 13 then:

Show Examples

Operator / Description / Example
And / Bitwise AND Operator copies a bit to the result if it exists in both operands. / (A AND B) will give 12 which is 0000 1100
Or / Binary OR Operator copies a bit if it exists in either operand. / (A Or B) will give 61 which is 0011 1101
Xor / Binary XOR Operator copies the bit if it is set in one operand but not both. / (A Xor B) will give 49 which is 0011 0001
Not / Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. / (Not A ) will give -60 which is 1100 0011
Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. / A < 2 will give 240 which is 1111 0000
Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. / A > 2 will give 15 which is 0000 1111

Assignment Operators

There are following assignment operators supported by VB.Net:

Show Examples

Operator / Description / Example
= / Simple assignment operator, Assigns values from right side operands to left side operand / C = A + B will assign value of A + B into C
+= / Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand / C += A is equivalent to C = C + A
-= / Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand / C -= A is equivalent to C = C - A
*= / Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand / C *= A is equivalent to C = C * A
/= / Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand(floating point division) / C /= A is equivalent to C = C / A
\= / Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand (Integer division) / C \= A is equivalent to C = C \A
^= / Exponentiation and assignment operator. It raises the left operand to the power of the right operand and assigns the result to left operand. / C^=A is equivalent to C = C ^ A
<= / Left shift AND assignment operator / C <= 2 is same as C = C < 2
>= / Right shift AND assignment operator / C >= 2 is same as C = C > 2
&= / Concatenates a String expression to a String variable or property and assigns the result to the variable or property. / Str1 &= Str2 is same as
Str1 = Str1 & Str2

Miscellaneous Operators

There are few other important operators supported by VB.Net.

Show Examples

Operator / Description / Example
AddressOf / Returns the address of a procedure. / AddHandler Button1.Click,
AddressOf Button1_Click
Await / It is applied to an operand in an asynchronous method or lambda expression to suspend execution of the method until the awaited task completes. / Dim result As res
= Await AsyncMethodThatReturnsResult()
Await AsyncMethod()
GetType / It returns a Type object for the specified type. The Type object provides information about the type such as its properties, methods, and events. / MsgBox(GetType(Integer).ToString())
Function Expression / It declares the parameters and code that define a function lambda expression. / Dim add5 = Function(num As
Integer) num + 5
'prints 10
Console.WriteLine(add5(5))
If / It uses short-circuit evaluation to conditionally return one of two values. The If operator can be called with three arguments or with two arguments. / Dim num = 5
Console.WriteLine(If(num >= 0,
"Positive", "Negative"))

Operators Precedence in VB.Net

Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator:

For example x = 7 + 3 * 2; Here x is assigned 13, not 20 because operator * has higher precedence than + so it first get multiplied with 3*2 and then adds into 7.

Here operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

Show Examples

Operator / Precedence
Await / Highest
Exponentiation (^)
Unary identity and negation (+, -)
Multiplication and floating-point division (*, /)
Integer division (\)
Modulus arithmetic (Mod)
Addition and subtraction (+, -)
Arithmetic bit shift (<, >)
All comparison operators (=, >, <, <=, >, >=, Is, IsNot, Like, TypeOf...Is)
Negation (Not)
Conjunction (And, AndAlso)
Inclusive disjunction (Or, OrElse)
Exclusive disjunction (Xor) / Lowest
How to Add Global.asax File in Asp.Net
Posted Date :3/28/2013No Of Visit :115
Introduction :In this article i will show you how you can add a global.asax file in asp.net. In this we will also learn to add how to add Global.asax.cs file in asp.net or how to add Code behind file for Global.asax in asp.net.
Other Related Tag
Asp.Net
C#.Net
In this article i will show you how you can add a global.asax file in asp.net. In this we will also learn to add how to add Global.asax.cs file in asp.net or how to add Code behind file for Global.asax in asp.net.
Here is the steps.
First Create a new asp.net application.
global
Now right click on the project file and select new item.
global
Now as you click on the add new item a window will open.
global
In above window you will see the global file now select the file and click on Add button .
global
Now in your Global.asax.cs file youe will get the below code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
namespace WebApplication5
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
}
protected void Session_Start(object sender, EventArgs e)
{
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
}
protected void Application_End(object sender, EventArgs e)
{
}
}
}

What is ADO.NET?

  • ADO.NET is a part of the .NET Framework
  • ADO.NET consists of a set of classes used to handle data access
  • ADO.NET is entirely based on XML
  • ADO.NET has, unlike ADO, no Recordset object

Create a Database Connection

We are going to use the Northwind database in our examples.

First, import the "System.Data.OleDb" namespace. We need this namespace to work with Microsoft Access and other OLE DB database providers. We will create the connection to the database in the Page_Load subroutine. We create a dbconn variable as a new OleDbConnection class with a connection string which identifies the OLE DB provider and the location of the database. Then we open the database connection:

<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
sub Page_Load
dim dbconn
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
data source=" & server.mappath("northwind.mdb"))
dbconn.Open()
end sub
</script>

Note:The connection string must be a continuous string without a line break!

Create a Database Command

To specify the records to retrieve from the database, we will create a dbcomm variable as a new OleDbCommand class. The OleDbCommand class is for issuing SQL queries against database tables:

<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
sub Page_Load
dim dbconn,sql,dbcomm
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
data source=" & server.mappath("northwind.mdb"))
dbconn.Open()
sql="SELECT * FROM customers"
dbcomm=New OleDbCommand(sql,dbconn)
end sub
</script>

Create a DataReader

The OleDbDataReader class is used to read a stream of records from a data source. A DataReader is created by calling the ExecuteReader method of the OleDbCommand object:

<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
sub Page_Load
dim dbconn,sql,dbcomm,dbread
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
data source=" & server.mappath("northwind.mdb"))
dbconn.Open()
sql="SELECT * FROM customers"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
end sub
</script>

Bind to a Repeater Control

Then we bind the DataReader to a Repeater control:

Example

<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
sub Page_Load
dim dbconn,sql,dbcomm,dbread
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
data source=" & server.mappath("northwind.mdb"))
dbconn.Open()
sql="SELECT * FROM customers"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
customers.DataSource=dbread
customers.DataBind()
dbread.Close()
dbconn.Close()
end sub
</script>
<html>
<body>
<form runat="server">
<asp:Repeater id="customers" runat="server">
<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>Companyname</th>
<th>Contactname</th>
<th>Address</th>
<th>City</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td<%#Container.DataItem("companyname")%</td>
<td<%#Container.DataItem("contactname")%</td>
<td<%#Container.DataItem("address")%</td>
<td<%#Container.DataItem("city")%</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>

Show example »

Close the Database Connection