Wednesday, March 21, 2012

How to use javascript inside aspx.cs

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('Invalid Username and Password')", true);

Saturday, March 17, 2012

send mail from asp.net to gmail

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;

// Now open your .aspx page replace the page_load with below code

protected void Page_Load(object sender, EventArgs e)
{
MailMessage message = new MailMessage();

try
{
MailAddress fromAddress = new MailAddress("umapathi19@gmail.com", "Umapathi");
message.From = fromAddress;
message.To.Add(" umapathi19@gmail.com ");
message.Subject = "Feedback";
message.IsBodyHtml = false;
message.Body = "Hello Umapathi";
sendmails(message);
Label1.Text = "Email successfully sent.";
}
catch (Exception ex)
{
Label1.Text = "Send Email Failed." + ex.Message;
}
}

public void sendmails(MailMessage mm)
{
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; 
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = " umapathi19@gmail.com ";
NetworkCred.Password = "youe actual gmail password for the mail id mentionaed in the above code"; ;
smtp.UseDefaultCredentials = true; 
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm); 

}


Smtp settings:

note: smtp server should be installed in your computer.your system must have internet connection
1.go to start>run
2.type "inetmgr" in the run press ok
3.in the newly opened internet service manager navigate to "default smtp virtual server"
4.go to domain right click new>domain
5.select remote and click next"smtp.gmail.com" and click finish.
6.now go to "default smtp virtual server" right click choose "Properties" and click.
7.in the ip address dropdown choose your system ip.
8.go to "access" tab click on "Relay"
9.selectradio buton "only the list below" and also check "allow all the computers which success fully authenticate to relay.regardless of the list above" click ok.
10.in the connection select "only the list below" click ok.
11.click on "Authentication" button choose "Anonymous Acess" click ok.
12.close your iis window.

JavaScript Validations

Default.aspx Page Start
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Registration Form</title>
<script language="javascript" type="text/javascript">
function fnValidate()
{
//FName Validation.
fnValidateFirstName();
//LName Validation
fnValidateLastName();
//Mobile validation
fnValidateMobileNumber();
//PEmail validation
fnValidatePrimaryEmail();
//SEmail Validation
fnValidateSecondaryEmail();
//Counttry Validation
fnValidateCountries();
}
function fnValidateSecondaryEmail()
{
var semail=document.getElementById("txtsemail").value;
var semailregex=/^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/;
if(!semail.match(semailregex))
{
alert("enter valid emailid");
} 
}
function fnValidatePrimaryEmail()
{
var pemailno=document.getElementById("txtPemail").value;
if(pemailno=="")
{
alert("email id is mandatory");
}
var pemailregex=/^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/;
if(!pemailno.match(pemailregex))
{
alert("enter valid emailid");
} 
}
function fnValidateMobileNumber()
{
var mobile=document.getElementById("txtMobile").value;
if(mobile=="")
{
alert("Mobile number is mandatory");
}
var mno= /^((\+)?(\d{2}[-]))?(\d{10}){1}?$/;
if(!mobile.match(mno))
{
alert("1.Mobile Number should Have Only Digits .\n2.Mobile Number should contain 10 digits");
return false;
}
else
{
return true;
}
}
function fnValidateLastName()
{
var lname=document.getElementById("txtlname").value;
var alphaExp= /^[a-zA-Z]+$/;
if(!lname.match(alphaExp))
{
alert("Last name should have only alphabets");
return false;
}
else 
{
return true;
}
}
function fnValidateFirstName()
{
var fname=document.getElementById("txtfname").value;
if(fname=="")
{
alert("First Name Cannot be Blank.");
return false;
}
else
{
var alphaExp = /^[a-zA-Z]+$/;

if(!fname.match(alphaExp))
{
alert("First Name should contain Only Alphabets.");
return false;
}
return true;
}
}
function fnValidateCountries()
{
var index=document.getElementById("ddlCountry").selectedIndex;
alert(index);
if(index==0)
{
alert("Please Select a Country.");
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="100%">
<tr>
<td width="15%"></td>
<td width="70%">
<table>
<tr>
<td align="right">FirstName:</td>
<td><asp:TextBox ID="txtfname" runat="server" /></td>
<td align="right">Mobile:</td>
<td><asp:TextBox ID="txtMobile" runat="server" /></td>
</tr>
<tr>
<td align="right">LastName:</td>
<td><asp:TextBox ID="txtlname" runat="server" /></td>
<td align="right">Phone:</td>
<td><asp:TextBox Width="10%" ID="tbCountryCode" runat="server" />

<asp:TextBox Width="10%" ID="tbCityCode" runat="server" />

<asp:TextBox Width="29%" ID="tbPhone" runat="server" />
</td>
</tr>
<tr>
<td align="right">PrimaryMail: </td>
<td> 
<asp:TextBox ID="txtPemail" runat="server" />
</td>
<td align="right">SecondaryMail:</td>
<td>
<asp:TextBox ID="txtsemail" runat="server" />
</td>
</tr>
<tr>
<td align="right">Address1:</td>
<td>
<asp:TextBox ID="txtaddress1" runat="server" TextMode="MultiLine" rows="5"/>
</td>
<td></td>
<td></td>
</tr>
<tr>
<td>Country:</td>
<td><asp:DropDownList ID="ddlCountry" runat="server"
width="80%"></asp:DropDownList></td>
<td></td>
<td><asp:Button ID="btnSubmit" runat="server" Text="Submit" 
OnClientClick="return fnValidate()" /></td>
</tr>
</table>
</td>
<td width="15%"></td>
</tr>
</table>
</div>
</form>
</body>
</html>
Default.aspx Page End


Default.aspx.cs Page Start

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillCountries();
        }
    }

    private void FillCountries()
    {
        ArrayList alCountries = new ArrayList();
        alCountries.Add("India");
        alCountries.Add("Pakistan");
        alCountries.Add("Srilanka");
        alCountries.Add("Australia");
        ddlCountry.DataSource = alCountries;
        ddlCountry.DataBind();
        ddlCountry.Items.Insert(0, "--Select--");
    }
}


Why .NET is better than Java

There are couple things we need to understand before comparing Java and .NET.

1.Usually softwares are developed to automate any business and to reduce human intervention.
2.Let us Say Palle Technologies wanted to develop a software for XYZ hospital. Since XYZ hospital wanted to automate the Registration,Admission,Scheduling etc process and they decided to develop a software which makes their daily work faster and increases Revenue.
3.They gave contract of developing an application to Umapathi Technologiies and asked to come with feasible choice of softwares and they gave us 6 months time for development for what ever the technology they choose.
4.We analysed the requirement and estimated development and maintainence effort for Both Java and .NET
5. We saw significant notable differences between JAVA and .NET
Note: Assumed Application Life time = 15 years.
 Look at the Estimated Costs Required for creating the application for XYZ company.
Estimation for Java Application :
  Java Total Resources Required for Development (6 months)= 6 resources
  Cost of Each Resource= 6* 20000 INR/Month
 Total Resources Cost During Development=720000 INR
Note: We assumed XYZ company is going to use this software for 15 years
Cost of Maintaining the Application for 1 year =3*20000 INR/Person/Month*12 Months=720000
Cost of Maintaining the Application for 15 years=10800000 INR.
Cost of Softwares and Servers = 60000 INR.
Total Cost=11580000 INR.
Estimation for .NET Application :

 .NET Total Resources Required for Development (6 months)= 5 resources 
  Cost of Each Resource= 5* 20000 INR/Month
 Total Resources Cost During Development=700000 INR
Note: We assumed XYZ company is going to use this software for 15 years
Cost of Maintaining the Application for 1 year with 2 Resources=2*20000 INR/Person/Month*12 Months=480000
Cost of Maintaining the Application for 15 years=7200000 INR. 
Cost of Softwares and Servers = 100000 INR.
Total Cost=8000000 INR.