Wednesday, 29 June 2011

                                   use of    image captcha







Verification Code  sows in textbox




make


 default.aspx like this 
<%@ 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>Simple Captcha Example</title>
    <style type="text/css">
        td
            {
                font-family:Tahoma;
                font-size:11px;
                background-color:#EEEEEE;
            }
            
        span
            {
                font-family:Tahoma;
                font-size:14px;  
                color:Red;         
            }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table width="500">
            <tr>
                <td colspan="3">
                    <span></span><asp:Label ID="lblMessage" runat="server"></asp:Label></span>
                </td>
            </tr>
            <tr>
                <td>Image Code</td>
                <td colspan="2"><img src="Captcha.aspx" alt="Verification Code" /></td>
            </tr>
            <tr>
                <td>Enter Code</td>
                <td><asp:TextBox ID="txtCode" runat="server"></asp:TextBox></td>
                <td><asp:Button ID="btnVerify" runat="server" onclick="btnVerify_Click" 
                        Text="Verify" /></td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

 and do the following coding in .aspx

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;

public partial class _Default : System.Web.UI.Page 
{

    protected void btnVerify_Click(object sender, EventArgs e)
    {
        string user_code = txtCode.Text;
        string captcha_code = Session["CAPTCHA_CODE"].ToString();

        if (user_code != captcha_code)
        {
            lblMessage.Text = "Verification Failed.";
        }
        else
        {
            lblMessage.Text = "Verification Successful";
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}
now take another form named captcha and set html like this,

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

and do the following code ib capcha .aspx .cs like this,

using System;
using System.Collections;
using System.Configuration;
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.Drawing;
using System.Drawing.Drawing2D;


public partial class Captcha : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string strString = "abcdefghijklmnopqrstuvwxyz0123456789";

        Bitmap image = new Bitmap(Server.MapPath("image.png"));
        Graphics g = Graphics.FromImage(image);
        Rectangle area = new Rectangle(0,0,image.Width-1, image.Height-1);
        Pen pen = new Pen(Color.SkyBlue);

        LinearGradientBrush lgBrush = new LinearGradientBrush(area, Color.White, Color.FromArgb(220,220,220), LinearGradientMode.Vertical);
        
        g.FillRectangle(lgBrush, area);
        g.DrawRectangle(pen, area);

        SolidBrush brush = new SolidBrush(Color.Black);
        Random random = new Random();
        int randomCharIndex = 0;
        char randomChar;
        int charPosition = 10;

        string captcha = "";
        for (int i = 0; i < 7; i++)
        {
            randomCharIndex = random.Next(0, strString.Length);
            randomChar = strString[randomCharIndex];
            g.DrawString(Convert.ToString(randomChar), new Font("arial", random.Next(16,25)), brush, charPosition, 10);
            charPosition += 17;
            captcha += Convert.ToString(randomChar);
        }
        Session["CAPTCHA_CODE"] = captcha;
        Response.ContentType = "image/jpeg";
        image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    }
}

and take a folder name image and paste a i mage now code ready....



Saturday, 25 June 2011

lambda expression

                            lambda expression




lambada expression is nothing , it is a only a anonymous function that contain expression and statements,lambada
expression is only used to create delegates and expression tree type.




example for understanding -:




using System;
using System.Collections;
using System.Collections.Generic;


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;


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


        }


        protected void Button1_Click(object sender, EventArgs e)
        {


            if (IsPostBack == true)
            {
                List<string> nameCollection = new List<string>();
                //Add some values in collection object.
                nameCollection.Add("James");
                nameCollection.Add("Chow");
                nameCollection.Add("John");
                nameCollection.Add("Haider");
                nameCollection.Add("Amit");
                //Using annoymouse expression for checking name
                //string result = nameCollection.Find(delegate(string name)
                //{
                //    return name.Equals("John");
                //});
                //if (!string.IsNullOrEmpty(result))
                //    Response.Write(result);
                //else
                //    Response.Write("Specified record is not found.");
                //Using lambda expression for checking name
                string lambdaResult = nameCollection.Find(sname => sname.Equals("James"));    //Here we directly write name of variable like sname.
                if (!string.IsNullOrEmpty(lambdaResult))
                    Response.Write(lambdaResult);
                else
                    Response.Write("Specified record is not found.");
            }
            else
            {
                Response.Write("not postback");
            }
        }
    }
}

Tuesday, 21 June 2011

insert through linq in database


   insert through linq in database and showing in gridview in asp.net





rollno.
name


rollno
name
243
mukjesh
345
raju

 first of  all  make  a class name bl ,
and do following code,



using System;
using System.Data;
using System.Configuration;
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.Data.Linq;
using System.Data.Linq.Mapping;


namespace linqentity
{
    [Table(Name = ("emp"))]
    public class bl
    {
        [Column(Name="rollno",IsPrimaryKey=true)]
        public int rollno { get; set; }
        [Column(Name="Name1")]
        public string name { get; set; }
    }
}


now make   another class name dal,
then do following code,


using System;
using System.Data;
using System.Configuration;
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.Data.Linq;        //here 
using System.Data.Linq.Mapping;  // these two namespace is necessaru foe linq table mapping with databse

namespace linqentity
{
    public class dal:DataContext
    {

        public dal(string myc)
            : base(myc)
        {
        }
        public Table<bl> st;//association
    }
}
 now open .aspx page and take two textbox , and one  button and a gridview 
 and do following code in button click, 
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;

namespace linqentity
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            bl b = new bl();
            b.rollno = Convert.ToInt32(TextBox1.Text);
            b.name = TextBox2.Text;
            dal d=new dal (@"database=msdb;data source=FAMILY-A990589E\SQLEXPRESS; integrated security=true;");
            d.st.InsertOnSubmit(b);
            d.SubmitChanges();//commit

            GridView1.DataSource=d.st;
            GridView1.DataBind();
        }
    }
}








Sunday, 19 June 2011

applying Javascript in asp.net





                                             Applying javascript 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 id="Head1" runat="server">

<title>Untitled Page</title>

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

function validate() {

var summary = "";

summary += isvalidFirstname();

summary += email();

summary += age();

summary += mobileno();

summary += licence();



if (summary != "") {

alert(summary);

return false;

}

else {

return true;

}

}

function licence() {

var uid;

var temp = document.getElementById("<%=txtlicence.ClientID %>");

uid = temp.value;

var re = /^[0-9a-zA-Z ]+$/;

var digits = /\d(10)/;

if (uid == "") {

return ("Please enter licence no." + "\n");

}

else if (re.test(uid)) {

return "";







}

else {

return ("please enter valid licence no . in alphanumeric" + "\n");

}

}



function mobileno()

{



var uid;

var temp = document.getElementById("<%=Txtmobile.ClientID %>");

uid = temp.value;

var re = /^([0-9]{10})$/;

if (uid == "") {

return ("Please enter mobile" + "\n");

}

else if (re.test(uid)) {

return "";







}

else {

return ("please enter valid mobile no 10 digit" + "\n");

}



}

function age()

{

var uid;

var temp = document.getElementById("<%=Txtage.ClientID %>");

uid = temp.value;

var re = /^[0-9]+$/;

if (uid == "") {

return ("Please enter age" + "\n");

}

else if (re.test(uid)) {

return "";







}

else {

return ("please enter age in integer" + "\n");

}

}

function email()

{

var uid;

var temp = document.getElementById("<%=Textemial.ClientID %>");

uid = temp.value;

var re = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;

if (uid == "") {

return ("Please enter email" + "\n");

}

else if (re.test(uid)) {

return "";







}

else {

return ("please enter valid email" + "\n");

}

}

function isvalidFirstname() {

var uid;

var temp = document.getElementById("<%=txtfname.ClientID %>");

uid = temp.value;

var re = /^[a-zA-Z ]+$/

if (uid == "") {

return ("Please enter firstname" + "\n");

}

else if (re.test(uid)) {

return "";







}

else {

return ("FirstName accepts Characters and spaces only" + "\n");

}

}











</script>

</head>

<body>

<form id="form1" runat="server">

<div>



<asp:TextBox ID="txtfname" runat="server" ></asp:TextBox>

&nbsp; name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<asp:TextBox ID="Textemial" runat="server"></asp:TextBox>

&nbsp; email&nbsp;&nbsp;&nbsp;

<br />

<asp:TextBox ID="Txtage" runat="server"></asp:TextBox>

&nbsp; age&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<asp:TextBox ID="Txtmobile" runat="server"></asp:TextBox>

&nbsp; mobile no.<br />

<asp:TextBox ID="txtlicence" runat="server"></asp:TextBox>

&nbsp; licence no.<br />

<br />

</div>

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" OnClientClick="javascript:validate()" />

</form>

</body>

</html>

Saturday, 11 June 2011

working with linq to sql

                                      linq to sql working process


open visual studio, now open new website and right  click on solution explorer and add new item and find linq to sql  add this one and now .dbml class willbe added into solution explorer  now on left end corner above we find server explorer  click on this and find data connection then find add connection
now add database of sql server which you want  and drag the table on which you want to perform the operations


now open .aspx page take one gridview and a button
code look like this



<%@ 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>Untitled Page</title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            height: 36px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        <asp:GridView ID="GridView1" runat="server" BackColor="White"
            BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3"
            GridLines="Vertical">
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="#DCDCDC" />
        </asp:GridView>
 
    </div>
    <table class="style1">
        <tr>
            <td>
                <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
            </td>
         
        </tr>
        <tr>
            <td class="style2">
            </td>
            <td class="style2">
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
now comes to aspx.cs
 and do following code


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;

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        DataClassesDataContext db = new DataClassesDataContext();
        var t = from p in db.Ids
               select new
                {
                    Dep_Name = p.Dep_Name,
               
                };
           
        GridView1.DataSource = t;
        GridView1.DataBind();


    }
}
now output look like this,
Dep_Name
CS
IT
MBA
MCA

button

continue.....more

woking with procedure simple Example


stored procedure-simple example to understand the functionality procedure this example will give the calculation of  items when we put it id and item name 

create proc pp  //'Aoo3','biscuit',''
(
@id char(5),
@prname varchar(12),
@price float=null
)
as
begin

declare @comm as int,@grossamount as float, @tax as numeric(5,2),@productprname as varchar(12),@productprice as float
declare @totaltax as int

set @productprname=(select productname from prduct where product_id=@id)
set @productprice=(select productprice from prduct where product_id=@id)
if(@prname='colgate')
begin
set @comm=2
set @totaltax=5
set @grossamount=@productprice-(@productprice*@comm/100)+@totaltax
select @grossamount
end
else
    if(@prname='soap')
     begin
     set @comm=1
     set @totaltax=2
 set  @grossamount=@productprice-(@productprice*@comm/100)+@totaltax
     select @grossamount as Totalcost
     end
else
     if(@prname='biscuit')
     begin
     set @comm=3
     set @totaltax=4
 set  @grossamount=@productprice-(@productprice*@comm/100)+@totaltax
    select @grossamount as Totalcost
     end
else
    if(@prname='oil')
     begin
     set @comm=7
     set @totaltax=2
 set  @grossamount=@productprice-(@productprice*@comm/100)+@totaltax
    select @grossamount as Totalcost
     end
else
    if(@prname='tobaco')
     begin
     set @comm=5
     set @totaltax=5
 set  @grossamount=@productprice-(@productprice*@comm/100)+@totaltax
    select @grossamount as Totalcost
     end
end


continue....more