10 September 2010

Option Explicit statement and Option Strict Statement

Option Explicit statement and Option Strict Statement

Option Explicit Statement

The Option Explicit has two modes. On and Off mode.

If Option Explicit mode in ON , you have to declare all the variable before you use it in the program . If not , it will generate a compile-time error whenever a variable that has not been declared is encountered .If the Option Explicit mode is OFF , Vb.Net automatically create a variable whenever it sees a variable without proper declaration.
By default the Option Explicit is On

Option Strict Statement
Visual Basic allows conversions of many data types to other data types. Data loss can occur when the value of one data type is converted to a data type with less precision or smaller capacity. A run-time error occurs if such a narrowing conversion fails. Option Strict ensures compile-time notification of these narrowing conversions so they can be avoided.
On
Optional. Enables Option Strict checking.
Off
Optional. Disables Option Strict checking. If On or Off is not specified, the default is Off.

Difference Between Int32.Parse(), Convert.ToInt32(), and Int32.TryParse()

Difference Between Int32.Parse(), Convert.ToInt32(), and Int32.TryParse()


Int32.parse(string)
Int32.Parse (string s)
method converts the string representation of a number to its 32-bit signed integer equivalent. When s is a null reference, it will throw ArgumentNullException. If s is other than integer value, it will throw FormatException. When s represents a number less than MinValue or greater than MaxValue, it will throw OverflowException. For example:

string s1 = "1234";
string s2 = "1234.65";
string s3 = null;
string s4 = "123456789123456789123456789123456789123456789";

int result;
bool success;

result = Int32.Parse(s1); //-- 1234
result = Int32.Parse(s2); //-- FormatException
result = Int32.Parse(s3); //-- ArgumentNullException
result = Int32.Parse(s4); //-- OverflowException

Convert.ToInt32(string)

Convert.ToInt32(string s) method converts the specified string representation of 32-bit signed integer equivalent. This calls in turn Int32.Parse () method. When s is a null reference, it will return 0 rather than throw ArgumentNullException. If s is other than integer value, it will throw FormatException. When s represents a number less than MinValue or greater than MaxValue, it will throw OverflowException.

For example:
result = Convert.ToInt32(s1); //-- 1234
result = Convert.ToInt32(s2); //-- FormatException
result = Convert.ToInt32(s3); //-- 0
result = Convert.ToInt32(s4); //-- OverflowException

Int32.Parse(string, out int) method converts the specified string representation of 32-bit signed integer equivalent to out variable, and returns true if it is parsed successfully, false otherwise. This method is available in C# 2.0. When s is a null reference, it will return 0 rather than throw ArgumentNullException. If s is other than an integer value, the out variable will have 0 rather than FormatException. When s represents a number less than MinValue or greater than MaxValue, the out variable will have 0 rather than OverflowException. For example:

success = Int32.TryParse(s1, out result); //-- success => true; result => 1234
success = Int32.TryParse(s2, out result); //-- success => false; result => 0
success = Int32.TryParse(s3, out result); //-- success => false; result => 0
success = Int32.TryParse(s4, out result); //-- success => false; result => 0
Convert.ToInt32 is better than Int32.Parse since it returns 0 rather than an exception. But again, according to the requirement, this can be used. TryParse will be the best since it always handles exceptions by itself.

9 September 2010

Java script validations

Java script validations
<--script type ="text/javascript">

//Validating the user inpus in the form
function Validate() {
if (document.getElementById("<%=txtName.ClientID%>").value == "")
{
alert("Name is a mandatory field and it can not be empty");
document.getElementById("<%=txtName.ClientID%>").focus();
return false;
}
else if (document.getElementById("<%=txtAddress.ClientID%>").value == "")
{
alert("Address is a Mandatory field and it can not be empty");
document.getElementById("<%=txtAddress.ClientID%>").focus();
return false;
}
else if (document.getElementById("<%=txtLandMark.ClientID%>").value == "")
{
alert("LandMark is Mandatory field and it can not be empty");
document.getElementById("<%=txtLandMark.ClientID%>").focus();
return false;
}
else if (document.getElementById("<%=txtDOB.ClientID%>").value == "")
{
alert("Date of Birth is a Mandatory field and it can not be empty");
document.getElementById("<%=txtDOB.ClientID%>").focus();
return false;
}
else if (document.getElementById("<%=ddlTitle.ClientID%>").value == "-select-")
{
alert("please select the title field");
document.getElementById("<%=ddlTitle.ClientID%>").focus();
return false;
}

}

/* address validation function */
function addressCheck()
{

var msg = " Address field can accept only \n alpha numerics \n white spaces and the following special characters \n";
msg = msg+ " . / # - ,";
var a = 1;
var iChars = "~`!@$%^&*()+=[]\';{}|\":<>?";
var txtval = '';
var strField = document.getElementById("<%=txtAddress.ClientID%>").value;
for (var i = 0; i < strField.length; i++)
{
if (iChars.indexOf(strField.charAt(i)) != -1)
{
a = 1;
break;
}
else
a = 2;
}

if (a == 1)
{
alert(msg);
document.getElementById("<%=txtAddress.ClientID%>").focus();
return false;
}
else if (a == 2)
{
return true;
}

}

/* land mark validation function */
function landMarkCheck()
{
var msg = " LandMark field can accept only \n alpha numerics \n white spaces and the following special characters \n";
msg = msg + " . ,";
var a = 1;
var iChars = "~`!#@$%^&*()+=-[]\\\';/{}|\":<>?";
var txtval = '';
var strField = document.getElementById("<%=txtLandMark.ClientID%>").value;
for (var i = 0; i < strField.length; i++) {
if (iChars.indexOf(strField.charAt(i)) != -1)
{
a = 1;
break;
}
else
a = 2;
}

if (a == 1)
{
alert(msg);
document.getElementById("<%=txtLandMark.ClientID%>").focus();
return false;
}
else if (a == 2) {
return true;
}
}
/* date validation */
function dateCheck() {
var s = document.getElementById("<%=txtDOB.ClientID%>").value;
var y = s.substring(6, 10);
var d = s.substring(0, 2);
var m = s.substring(3, 5);
var cdate = new Date();
var yy = cdate.getFullYear() - 1;
//checking the date is between 1910 to current date
if ((y >= 1900) && (y <= yy))
{
return true;
}
else if(y==yy+1)
{
if (m <= cdate.getMonth() + 1)
{
if (m == cdate.getMonth() + 1)
{
if (d <= cdate.getDate())
{
if (d == cdate.getDate());
return true;
}
else {
alert("Date of birth should be from 1910 to current date and also in the specified format ");
document.getElementById("<%=txtDOB.ClientID%>").focus();
return false;
}
}

}
else {
alert("Date of birth should be from 1910 to current date and also in the specified format");
document.getElementById("<%=txtDOB.ClientID%>").focus();
return false;

}
}
else {
alert("Date of birth should be from 1910 to current date and also in the specified format");
document.getElementById("<%=txtDOB.ClientID%>").focus();
return false;
}


}

<--script type="text/javascript">




1. write scripts with in tag
2. call the functions from code behind using script registration.

example:

page scripts of : add or edit page



----------------------------------------------------------------------------------------------------------

script registration in code behind for add or edit page

protected void Page_Load(object sender, EventArgs e)
{
//checking page is
if (!Page.IsPostBack)
{
//here we can add the jscript functions to the required control's events
btnSubmit.Attributes.Add("OnClick", "return Validate();");
txtAddress.Attributes.Add("onblur", "return addressCheck();");
txtLandMark.Attributes.Add("onblur", "return landMarkCheck();");
txtDOB.Attributes.Add("onblur", "return dateCheck();");
}
}

---------------------------------------------------------------------------------------------------------
page script for : modify page



---------------------------------------------------------------------------------------------------------

script registration in code behind for modify page


if (!Page.IsPostBack)
{
if (gvTestDetails.Rows.Count != 0)
{ btnDelete.Attributes.Add("OnClick", "return pop();"); }
else
{
btnDelete.Attributes.Add("OnClick", "return pop1();");
btnEdit.Attributes.Add("OnClick", "return pop2();");

}
}

E-mail Validation Using REGEX

E-mail Validation Using REGEX

string email = txtEmail.Text.ToString();
string pattern = @"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|" + @"0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z]" + @"[a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";
System.Text.RegularExpressions.Match match = Regex.Match(email, pattern, RegexOptions.IgnoreCase);
if (match.Success)
{
}
Else
{
{
MessageBox.Show("Don't Leave the Field Empty", "Name Entry Error",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}

VB.NET VALIDATION USING ASCII

VB.NET VALIDATION USING ASCII


For Numeric

Public Function OnlyNumeric(ByVal Key As String) As Boolean
If (Key >= 48 And Key <= 57) Or Key = 8 Then
OnlyNumeric = False
Else
OnlyNumeric = True
End If
End Function
--------------------------------------------------------------------------------------------


For Character

Public Function OnlyCharacter(ByVal key As String) As Boolean
If (key >= 65 And key <= 90) Or (key >= 97 And key <= 122) Or key = 8 Then
OnlyCharacter = False

Else
OnlyCharacter = True
MsgBox("Enter only characters")
End If
End Function


-----------------------------------------------------------------------------------------------


create this function and than call this function into that particular "textbox"(KeyPress event) like this way


Private Sub txtcitycode_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
e.Handled = OnlyNumeric(Asc(e.KeyChar))
End Sub

Private Sub txtcityname_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtcityname.KeyPress
e.Handled = OnlyCharacter(Asc(e.KeyChar))
End Sub

C# VALIDATION USING ASCII

C# VALIDATION USING ASCII

//for ONLY NUMBERS

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{

if ((e.KeyChar >= 48 && e.KeyChar <= 57 || e.KeyChar == 8))
{
e.Handled = false;
}
else
{
e.Handled = true;
MessageBox.Show("Please Enter a Valid Number", "Number Validation",
MessageBoxButtons.OK);
}

}
-----------------------------------------------------------------------------------------------
//for ONLY CHARCTERS

private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= 65 && e.KeyChar <= 90) || (e.KeyChar >= 97 && e.KeyChar<=122) || e.KeyChar==8)
{
e.Handled = false;
}
else
{
e.Handled = true;
MessageBox.Show("Please Enter a Character", "Number Validation",
MessageBoxButtons.OK);
}
}

Query Getting the Maximum salary of the employee by department wise SUBQUERY Example

Query Getting the Maximum salary of the employee by department wise

SUBQUERY Example


select
E.fname +' '+ E.lname as EmpName
,T.Salary
,D.depname
,D.depid
from
Employenew E,
departnew D
,(select max(salary) as Salary ,depid from Employenew group by depid) as T
where
E.depid=D.depid
and T.depid=D.depid
and E.Salary =T.Salary

Implementing OAuth validation in a Web API

 I mplementing OAuth validation in a Web API Implementing OAuth validation in a Web API using C# typically involves several key steps to sec...