menu

Sunday, August 1, 2010

Get more than one value from a function

Function will give three result SUM, SUB, MUL at one call.

class MyProgram
{
public static void Main()
{
int a = 10, b = 20, c = 0, d = 0, e = 0;

Func(a, b, ref c, ref d, ref e);

Console.WriteLine("Sum= {0}", c);
Console.WriteLine("Sub= {0}", d);
Console.WriteLine("Mul= {0}", e);
Console.ReadLine();
}

public static void Func(int p, int q, ref int r, ref int s, ref int t)
{
r = p + q;
s = p - q;
t = p * q;
}
}

Get more than one result from a function

Function will give three result SUM, SUB, MUL at one call.

class MyProgram
{
public static void Main()
{
int a = 10, b = 20, c = 0, d = 0, e = 0;

Func(a, b, ref c, ref d, ref e);

Console.WriteLine("Sum= {0}", c);
Console.WriteLine("Sub= {0}", d);
Console.WriteLine("Mul= {0}", e);
Console.ReadLine();
}

public static void Func(int p, int q, ref int r, ref int s, ref int t)
{
r = p + q;
s = p - q;
t = p * q;
}
}

Sunday, July 4, 2010

How to Create WCF Application?

Step 1:

Open Visual Studio 2008 and click File -> New -> Project (a new (New Project) window will open, select Project Type: WCF(WCF Service Library)). Look at image given below.



Step 2:

Write the code inside interface IService1 given below

public interface IService1
{
[OperationContract]
int Sum(int value1, int value2);
[OperationContract]
int Mult(int value1, int value2);
}

Step 3:

Write this code in side class Service1.cs

public class Service1 : IService1
{
#region IService1 Members

int IService1.Sum(int value1, int value2)
{
return value1 + value2;
}

int IService1.Mult(int value1, int value2)
{
return value1 * value2;
}

#endregion
}

Run the service (press F5). Now a new window named WCF Test Client will open(Don't close it because this will give service to client). Double click on Config file and a XML Code will be shown right side, copy highlighted code(end point address)(Ex. http://localhost:8731/Design_Time_Addresses/WcfServiceLibrary2/Service1/).



Now your service is started.

Step 4:

Now create a website site to call WCF.

Right click on Solution Explorer -> Add Service Reference (A new window will open).
Paste link copied from Config File of started WCF (Ex. http://localhost:8731/Design_Time_Addresses/WcfServiceLibrary2/Service1/)
Click GO then OK.(Your service is added now).



Add two TextBox and a Button. On button click create the object of class which is in WCF, and call method of that class.

ServiceReference1.Service1Client ms = new ServiceReference1.Service1Client();
Label1.Text = ms.Mult(int.Parse(TextBox1.Text), int.Parse(TextBox2.Text)).ToString();
Label2.Text = ms.Sum(int.Parse(TextBox1.Text), int.Parse(TextBox2.Text)).ToString();

Thursday, July 1, 2010

Email id validation through JavaScript (Using Expression)

Write this javascript code inside "HEAD" - "SCRIPT" tag

function checkEmail(myForm)
{
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.TextBox1.value))
{
return (true)
}
else
{
alert("Enter valid Email id please.")
TextBox1.focus()
return (false)
}
}

call this function in form's onsubmit event

form id="myForm" runat="server" onsubmit="return checkEmail(this)"

Email id validation through JavaScript (core coding)

Write this javascript code inside "HEAD" - "SCRIPT" tag

function echeck(str) {

var at="@"
var dot="."
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(at,(lat+1))!=-1){
alert("Invalid E-mail ID")
return false
}

if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(dot,(lat+2))==-1){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(" ")!=-1){
alert("Invalid E-mail ID")
return false
}

return true
}

function ValidateForm(){
var TextBox1=document.form1.TextBox1

if ((TextBox1.value==null)||(TextBox1.value=="")){
alert("Please Enter your Email ID")
TextBox1.focus()
return false
}
if (echeck(TextBox1.value)==false){
TextBox1.value=""
TextBox1.focus()
return false
}
return true
}

call this function in form's onsubmit event

form id="form1" runat="server" onsubmit="return ValidateForm()"

Sunday, June 27, 2010

Password field validation (match) through javascript

Write this javascript code inside "HEAD" - "SCRIPT" tag

function chkPass()
{
if(form1.TextBox1.value.length < 5)
{
alert("Password must be of more than 5 and less char");
return (false);
}
else if(form1.TextBox1.value != form1.TextBox2.value)
{
alert("Both Password are not the same");
return (false);
}
}

call this function in form's onsubmit event

form id="form1" runat="server" onsubmit="return chkPass()"

Saturday, June 26, 2010

Textbox validation for special characters

Write this javascript code inside "HEAD" -> "SCRIPT" tag

function validtForSpclChar()
{
var chkStr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var chkTxt = form1.TextBox1.value;
var allValid = true;
for (i = 0; i <>
{
ch = chkTxt.charAt(i);
for (j = 0; j <>
if (ch == chkStr.charAt(j))
break;
if (j == chkStr.length)
{
allValid = false;
break;
}
}
if (!allValid)
{
alert("Enter only letter and numeric characters in the \"TextBox\" field.");
form1.TextBox1.focus();
return (false);
}
}

call this function in form's onsubmit event

form id="form1" runat="server" onsubmit="return validtForSpclChar(this)"

Required text field validator using javascript

Write this javascript code inside to "HEAD" tag of .aspx file

function FormValidator(theForm)
{
if (theForm.TextBox1.value == "")
{
alert("You must enter somthing");
theForm.TextBox1.focus();
return (false);
}
}

call this function in form's onsubmit event

form id="form1" runat="server" onsubmit="return FormValidator(this)"

Tuesday, June 22, 2010

How to check if a particular session is alive

Suppose we have a login page and a home page, if login is success redirect to home page. And if session is expire on home page it again redirects to login page.

code in login.aspx.cs page

protected void Page_Load(object sender, EventArgs e)
{
Session["sessionid"] = Session.SessionID;
}

code in web.config file

--
--
--
--

where timeout="20" is in minutes.

code in home.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
if (Session["sessionid"]==null)
{
Response.Redirect("login.aspx");
}
}