Monday, January 16, 2012

How to develop STOP WATCH in asp.net.


HOW TO GENERATE SELF TIMER IN ASP.NET  : -
========================================
   .cs file code:-
-----------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Timers;

public partial class Stop_Watch : System.Web.UI.Page
{
    public int time_second;
    public int time_min;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
            Timer1.Enabled = false;
     
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Timer1.Enabled = true;
        btnpause.Visible = true;
        btnreset.Visible = true;
   
       
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
     
        time_second = int.Parse(lblsec1.Text);
        time_second = time_second + 1;
        lblsec1.Text = time_second.ToString();
        if (lblsec1.Text == "10")
        {
            lblsec2.Visible = false;
        }
        if (lblmin1.Text == "10")
        {
            lblmin2.Visible = true;
        }

        if (lblsec1.Text == "60")
        {
            lblsec2.Visible = true;
            time_min = int.Parse(lblmin1.Text);
            lblsec1.Text = "0";
            time_min = time_min + 1;
            lblmin1.Text = time_min.ToString();
        }

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
       Timer1.Enabled = false;
   
    }



    protected void Button3_Click(object sender, EventArgs e)
    {
        time_second = 0;
        time_min = 0;
        lblmin1.Text = "0";
        lblsec1.Text = "0";
        lblsec2.Visible = true;
        Timer1.Enabled = false;
    }
}
----------------------------------------------------------------------------------

.aspx page code :-
------------------

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

<!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></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 140px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">

/* First you have to insert this SCRIPT MANAGER after form tag. without this, any ajax control doesn't work. */
                    </asp:ScriptManager>
    <div>
   
        <table class="style1">
            <tr>
                <td class="style2">
                    <asp:UpdatePanel ID="UpdatePanel1" runat="server">    

 /* You have to insert this UPLOAD PANEL.The timer will insert under this UPLOAD  PANEL.This will help to refresh only partially like Google search box. */

                        <ContentTemplate>
                            <asp:Timer ID="Timer1" runat="server" Enabled="false" Interval="1000"
                                ontick="Timer1_Tick">
                            </asp:Timer>
                            <asp:Label ID="lblmin2" runat="server" Text="0"></asp:Label>
                            <asp:Label ID="lblmin1" runat="server" Text="0"></asp:Label>
                            <asp:Label ID="Label3" runat="server" Text=":"></asp:Label>
                            <asp:Label ID="lblsec2" runat="server" Text="0"></asp:Label>
                            <asp:Label ID="lblsec1" runat="server" Text="0"></asp:Label>
                        </ContentTemplate>
                    </asp:UpdatePanel>
                   
                </td>
                <td>
                    <asp:Button ID="btnstart" runat="server" OnClientClick ="startTimer()" Text="Start"
                        Width="160px" onclick="Button1_Click" />
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    <asp:Button ID="btnpause" runat="server" Text="Pause"
                        OnClientClick = "stopTimer()" onclick="Button2_Click" Width="76px"  />
                    <asp:Button ID="btnreset" runat="server" Text="Reset" onclick="Button3_Click"
                        Width="79px" />
                </td>
            </tr>
        </table>
   
    </div>
    </form>
    <script type = "text/javascript">
    function startTimer() {

       return $find('Timer1')._startTimer();

    }
    function stopTimer() {
       return $find('Timer1')._stopTimer();
    }
    </script>
</body>
</html>
=============================================================================

Saturday, January 14, 2012

How to Send E-Mail Through C# in asp.net.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
using System.Data.SqlClient;

public partial class Email : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(@"data source=localhost;initial catalog=Email;integrated security=true");
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        con.Open();
        cmd=new SqlCommand("insert into Gmail values('"+DropDownList1.Text+"','"+DropDownList2.Text+"','"+txtusername.Text+"','"+txtpassword.Text+"','"+txtmessage.Text+"')",con);
        cmd.ExecuteNonQuery();
        con.Close();
        Response.Write("Done Successfully");
        MailMessage message = new MailMessage();

        string s1;
        string s2;
        string s3;
        s1 = DropDownList2.SelectedItem.Text;
        s2 = "vibhutimishra58@gmail.com";
        MailAddress fromAddress = new MailAddress(s2);
        s3 = txtusername.Text;
        MailAddress toAddress = new MailAddress(s3);
        message.From = fromAddress;
        message.To.Add(toAddress);
        message.Subject = "Mail Send";
        message.Body = "Message:" + "</br>" + txtmessage.Text;
        if (DropDownList2.SelectedItem.Text == "smtp.gmail.com")
        {
            SmtpClient smtp = new SmtpClient();
            smtp.Port = 587;
            smtp.Host = s1;
            smtp.Credentials = new System.Net.NetworkCredential(s2, "confirm12m*");
            smtp.EnableSsl = true;
            smtp.Send(message);
            txtmessage.Text = "";
        }
        else if (DropDownList2.SelectedItem.Text == "smtp.mail.yahoo.com")
        {
            SmtpClient smtp = new SmtpClient();
            smtp.Port = 465;
            smtp.Host = s1;
            smtp.Credentials = new System.Net.NetworkCredential("Your Email Id","Your password");
            smtp.EnableSsl = true;
            smtp.Send(message);
            txtmessage.Text = "";
        }
    }
}