Share Blog

Friday, August 08, 2014

How To Find Name Of The Day from Date 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.Data;
using System.Drawing;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DateTime dt = new DateTime(2014, 8, 8);
       Response.Write(dt.ToString("dddddddddd"));
    }
  
}


How To Evaluate Mathematical/Arithmatic Expression string Expression In asp.net

<%@ 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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <fieldset style=" width:300px">
    <legend>Evaluate String Arithmetic Expression</legend>  
        <asp:TextBox ID="TxtEquation" runat="server"></asp:TextBox>
        <asp:Button ID="BtnCalculate" runat="server" Text="Calculate"
            onclick="btnCalculate_Click" />
              
        <asp:Button ID="btnReset" runat="server" Text="Reset"
            onclick="btnReset_Click" />  
        <br />
        <asp:Label ID="lblResult" runat="server" Text=""></asp:Label>
        </fieldset>
    </div>
    </form>
</body>

</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     
    }
    private object EvaluateExpression(string Equation)
    {
        DataTable dt = new DataTable();
        var Result = dt.Compute(Equation, string.Empty);
        return Result;
    }
 
    protected void btnReset_Click(object sender, EventArgs e)
    {
        TxtEquation.Text = "";
        TxtEquation.Focus();
    }
    protected void btnCalculate_Click(object sender, EventArgs e)
    {
        try
        {
            var Result = Convert.ToString(EvaluateExpression(TxtEquation.Text.Trim()));
            lblResult.Text = " Your Result" + Result;
            lblResult.ForeColor = Color.Blue;
        }
        catch (Exception ex)
        {
            lblResult.Text = "Ooops!" + ex.Message.ToString();
            lblResult.ForeColor = Color.Red;
        }
    }
}

Thursday, August 07, 2014

Custom Validator For Email And Mobile Number in Asp.net

<%@ 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></title> 
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <table>
           <tr>
           <td >
          Email</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                    <asp:CustomValidator ID="CustomValidator1" runat="server"
                        ControlToValidate="TextBox3" ErrorMessage="Enter Valid Email" Font-Bold="True"
                        ForeColor="Red" onservervalidate="Validate_Email"></asp:CustomValidator>
                </td>
            </tr>
            <tr>
                <td>
                    Mobile</td>
                <td>
                    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                    <asp:CustomValidator ID="CustomValidator2" runat="server"
                        ControlToValidate="TextBox4" Display="Dynamic"
                        ErrorMessage="Enter Valid mobile " ForeColor="Red"
                        onservervalidate="Validate_Mobile"></asp:CustomValidator>
                </td>
            </tr>
            <tr>
                  <td >
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Save" />
                </td>
                <td></td>
            </tr>
        </table>
        </div>
    </form>
</body>

</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


    }
  
    protected void Validate_Email(object source, ServerValidateEventArgs e)
    {
        //System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex("^[0-9]+$");
        System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
        e.IsValid = r.IsMatch(TextBox3.Text);
    }
    protected void Validate_Mobile(object source, ServerValidateEventArgs e)
    {
        System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(@"^[987]+\d{9}$");
        e.IsValid = r.IsMatch(TextBox4.Text);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
       
    }
}


Wednesday, August 06, 2014

How To Generate Random Password in Asp.net And C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Auto_Generate_password : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    
    }
    public static string NewRandomPassword(int PasswordLength)
    {
        string _allowedChars = "0123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ";
        Random randNum = new Random();
        char[] chars = new char[PasswordLength];
        int allowedCharCount = _allowedChars.Length;
        for (int i = 0; i < PasswordLength; i++)
        {
            chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())];
        }
        return new string(chars);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = NewRandomPassword(10);
    }
}


Monday, August 04, 2014

How To Create your own captcha image generator in asp.net using c#.net


The below example will generate a captcha code with numbers and alphabets (small and capital letters). You can change the variable  “combination” value according to your requirement. 

Create two pages Captcha_image.aspx and Registration.aspx

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

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div></div>
    </form>
</body>
</html>


Captcha_image.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Data.SqlClient;
using System.Text;

public partial class Captcha_image : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["captcha"]!= null)
        {
            int height = 30;
            int width = 100;
            Bitmap bmp = new Bitmap(width, height);
            RectangleF rectf = new RectangleF(10, 5, 0, 0);
            Graphics g = Graphics.FromImage(bmp);
            g.Clear(Color.White);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            g.DrawString(Session["captcha"].ToString(), new Font("Thaoma", 12, FontStyle.Italic), Brushes.Green, rectf);
            g.DrawRectangle(new Pen(Color.Red), 1, 1, width - 2, height - 2);
            g.Flush();
            Response.ContentType = "image/jpeg";
            bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
            g.Dispose();
            bmp.Dispose();
        }
    }
  
}

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

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="SM1" runat="server">
        </asp:ScriptManager>
        <table style="border: solid 1px black; padding: 20px; position: relative; top: 50px;"
            align="center">
            <tr>
                <td>
                    Email ID :
                </td>
                <td>
                    <asp:TextBox ID="txtEmailID" runat="server" Width="200px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Password :
                </td>
                <td>
                    <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" Width="200px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Confirm Password :
                </td>
                <td>
                    <asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password" Width="200px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Enter Below Code :
                </td>
                <td>
                    <asp:TextBox ID="txtCaptcha" runat="server" Width="200px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td valign="middle">
                    <asp:UpdatePanel ID="UP1" runat="server">
                        <ContentTemplate>
                            <table>
                                <tr>
                                    <td style="height: 50px; width:100px;">
                                        <asp:Image ID="imgCaptcha" runat="server" />
                                    </td>
                                    <td valign="middle">
                                        <asp:Button ID="btnRefresh" runat="server" Text="Refresh" OnClick="btnRefresh_Click" />
                                    </td>
                                </tr>
                            </table>
                        </ContentTemplate>
                    </asp:UpdatePanel>
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <asp:Button ID="btnRegiser" runat="server" Text="Register" OnClick="btnRegister_Click" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

Registration.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Data.SqlClient;
using System.Text;

public partial class Registration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillCapctha();
        }
    }
    void FillCapctha()
    {
        try
        {
            Random random = new Random();
            string combination = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
            StringBuilder captcha = new StringBuilder();
            for (int i = 0; i < 6; i++)
                captcha.Append(combination[random.Next(combination.Length)]);
            Session["captcha"] = captcha.ToString();
            imgCaptcha.ImageUrl = "Captcha_image.aspx?" + DateTime.Now.Ticks.ToString();
        }
        catch
        {

            throw;
        }
    }
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        if (Session["captcha"].ToString() != txtCaptcha.Text)
            Response.Write("Invalid Captcha Code");
        else
            Response.Write("Valid Captcha Code");
        FillCapctha();
    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        FillCapctha();
    }

}

RESULT