Monday, 19 March 2012

Email Validation using regular expression(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 runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" language="javascript">
    function validate()
    {
        var email=document.getElementById("txtemailid").value;
        var phone=document.getElementById("txtphno").value;
        var regexp1=/^([a-zA-Z0-9]+[\_\-\.]?)+@([a-zA-Z0-9]+\.)+[a-zA-Z0-9]+$/;
        var regexp2=/^[0-9]+$/;
        var Restr = /^[a-z]([\.\_]?[a-z\d]+)*\@[a-z\d]+([\.\_\-][a-z\d]+)+$/;        
        if (!regexp1.test(email))
        {
            alert("Please Enter Valid E-mail Adress");
           return false;
        } 
        else
        {
            alert("Valid Email address");
           return true;
        }
        
        if(!regexp2.test(phone))
        {          
            alert("phone number must contain only numbers");
           return false; 
        }
        if(phone.length!=10)
        {
            alert("Phone number length should be 10");
            return false;     
        }

        return true;
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <h2 style=" color:blue; font-style:normal ">  
           Email Javascript Validation
            <br /> in asp.net  
        </h2> 
    <div style="text-align:center">
    <table>
    <tr>
    <td><asp:Label ID="lblemailid" runat="server" Text="Email ID"></asp:Label></td>
    <td><asp:TextBox ID="txtemailid" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
    <td><asp:Label ID="lblphno" runat="server" Text="Phone Number"></asp:Label></td>
    <td><asp:TextBox ID="txtphno" runat="server"></asp:TextBox></td>
    </tr>
    </table>
    </div>
     <div style="text-align:center; margin-right:78%;">
     <asp:Button ID="btnsubmit" Text="Submit"  runat="server"/>
    </div>
    </form>
</body>
</html>

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)
    {
        btnsubmit.Attributes.Add("onclick", "return validate();");
    }
}




Gridview Delete rows using checkboxs 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">
<script  type="text/javascript">
function selectAll(invoker) 
{
        var inputElements = document.getElementsByTagName('input');


        for (var i = 0 ; i < inputElements.length ; i++) 
        {

            var myElement = inputElements[i];
               if (myElement.type === "checkbox") 
               {
                myElement.checked = invoker.checked;
                }

        }
        return true;
 }    
   
   
</script>
    <title>DataGridView  Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <h2 style="color: blue; font-style: normal">
        Gridview checkBox
        <br />
        in asp.net
    </h2>
    <div>
        <table>
            <tr>
                <td>
                </td>
                <td>
                </td>
                <td>
                    <asp:Button ID="btndel" runat="server" Text="Delete" OnClick="btndel_Click" />
                </td>
            </tr>
            <tr>
                <td colspan="3">
                    <asp:GridView runat="server" ID="gridviewchk" AutoGenerateColumns="False" AllowPaging="true"
                        OnPageIndexChanging="gridviewchk_PageIndexChanging" DataKeyNames="UserID">
                        <Columns>
                            <asp:TemplateField HeaderText="ALL">
                                <HeaderTemplate>
                                    <asp:CheckBox ID="CheckBoxAll" runat="server" Text="ALL" onClick=" return selectAll(this)" />
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="UserID" HeaderText="UserID" SortExpression="UserID" />
                            <asp:BoundField DataField="Password" HeaderText="Password" SortExpression="Password" />
                            <asp:BoundField DataField="counts" HeaderText="count" SortExpression="count" />
                        </Columns>
                    </asp:GridView>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

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.Data.SqlClient;
using System.Collections;

public partial class _Default : System.Web.UI.Page 
{
    SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["LoginDetails"].ToString());
    
    protected void Page_Load(object sender, EventArgs e)
    {
       if(!IsPostBack)
            bind();
            
    }
   
    protected void btndel_Click(object sender, EventArgs e)
    {
        ArrayList a = new ArrayList();
        for (int i = 0; i < gridviewchk.Rows.Count; i++)
        {
            CheckBox chkbx = (CheckBox)gridviewchk.Rows[i].Cells[0].FindControl("CheckBox1");

            if (chkbx.Checked)
            {
                a.Add(gridviewchk.Rows[i].Cells[1].Text);
            }

        }

        deleterecord(a);
        bind();

    }
    public void bind()
    {
        SqlCommand cmd = new SqlCommand("select * from login", con);
       
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        gridviewchk.DataSource = ds;
        gridviewchk.DataBind();
    }
    public void deleterecord(ArrayList ar)
    {
        con.Open();
        foreach(object i in ar)
        {
            string str="delete from login where UserID='"+i+"'";
            SqlCommand cmd = new SqlCommand(str, con);
             cmd.CommandType = CommandType.Text;
     
                cmd.ExecuteNonQuery();
           
           
        }
        con.Close();
    }

    protected void gridviewchk_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gridviewchk.PageIndex = e.NewPageIndex;
        bind(); 
    }
    private void RememberOldValues()
    {
        ArrayList users = new ArrayList();
        string userid = null;
        foreach (GridViewRow row in gridviewchk.Rows)
        {
            userid= (string)gridviewchk.DataKeys[row.RowIndex].Value;
            CheckBox c = row.FindControl("CheckBox1") as CheckBox;
            bool result = c.Checked;         
            if (Session["CHECKED_ITEMS"] != null)
                users = (ArrayList)Session["CHECKED_ITEMS"];
            if (result)
            {
                if (!users.Contains(userid))
                    users.Add(userid);
            }
            else
                users.Remove(userid);
        }
        if (users != null && users.Count > 0)
            Session["CHECKED_ITEMS"] = users;
    }
    private void RePopulateValues()
    {
        ArrayList users = (ArrayList)Session["CHECKED_ITEMS"];
        if (users != null && users.Count > 0)
        {
            foreach (GridViewRow row in gridviewchk.Rows)
            {
                string userid = (string)gridviewchk.DataKeys[row.RowIndex].Value;
                if (users.Contains(userid))
                {
                    CheckBox myCheckBox = (CheckBox)row.FindControl("CheckBox1");
                    myCheckBox.Checked = true;
                }
            }
        }
    }
}



Simple login 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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <h2 style="color: blue; font-style: normal">
        Simple login form
        <br />
        in asp.net
    </h2>
    <div>
        <table>
            <tr>
                <td>
                    <label>
                        UserName</label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    <label>
                        Password</label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server" TextMode="Password" />
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <asp:Button ID="Button1" runat="server" Text="Login" OnClick="Unnamed1_Click" />
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <asp:Label ID="label1" Text="Invalid Password" runat="server" Visible="false"></asp:Label>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>


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.Data.SqlClient;

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

    }
    protected void Unnamed1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["test"].ToString());
        SqlDataAdapter da = new SqlDataAdapter("select * from login",con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        int c=0;

        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            string str1 = ds.Tables[0].Rows[i].ItemArray[0].ToString();
            string str2 = ds.Tables[0].Rows[i].ItemArray[1].ToString();
            if (TextBox1.Text == str1 && TextBox2.Text == str2)
            {
                Response.Redirect("Default2.aspx");
                c = 1;
            }
            
        }

        if (c == 0)
        {
            TextBox1.Text = "";
            TextBox2.Text = "";
            label1.Visible = true;
        }

    }
}


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

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <h2 style=" color:blue; font-style:normal ">  
           SIMPLE LOGIN FORM
            <br /> in asp.net  
        </h2> 
    <div>
    Welcome to .NET World
    </div>
    </form>
</body>
</html>
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;

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

    }
}

Gridview Order 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">
<script  type="text/javascript">
     
     function emptytxtboxvalidation(a,b,c)
     {
       
                if(document.getElementById(a).value=="")
                {
                     alert("enter student id");
                      document.getElementById(a).focus();
                     return false;
                }
                    
               if(document.getElementById(b).value=="")
                {
                     alert("enter student name");
                      document.getElementById(b).focus();
                    return false;
                }
                
              if(document.getElementById(c).value=="")
                {
                    alert("enter student marks");
                      document.getElementById(c).focus();
                      return false;
                } 
                if (document.getElementById(a).value!="" || document.getElementById(b).value!="" ||document.getElementById(c).value!="" )
                {
                    alert("Correct data-You can Now Insert the data");
                    return false;
                }
 
            return true;

     }
    
    
    </script>
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <h2 style="color: blue; font-style: normal">
        Order gridview
        <br />
        in asp.net
    </h2>
    <div style="text-align: center">
        <table style="height: 450px; width: 616px">
            <tr>
                <td>
                    <table>
                        <tr>
                            <td>
                                <asp:LinkButton ID="lnkAddNewRow" runat="server" OnClick="lnkAddNewRow_Click">AddNewRow</asp:LinkButton>
                                <asp:Button ID="btnIns" runat="server" Text="Insert" CommandName="Insert" OnClick="btnIns_Click" />
                                <asp:Button ID="btnvalidate" runat="server" Text="Validate" />
                                <asp:Button ID="btnCancelInsert" runat="server" Text="Cancel Insertion" Visible="false"
                                    OnClick="btnCancelInsert_Click" />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td>
                    <table width="500px" style="height: 327px">
                        <tr>
                            <td>
                                <asp:GridView ID="grdOrdering" runat="server" DataKeyNames="sid" AutoGenerateColumns="False"
                                    Width="555px" OnRowDataBound="grdOrdering_RowDataBound" OnRowCommand="grdOrdering_RowCommand">
                                    <Columns>
                                        <asp:TemplateField>
                                            <ItemTemplate>
                                                <asp:Button ID="BtnDel" runat="server" Text="Delete" CommandName="DeleteOne" />
                                                <asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName="UpdateOne" Visible="false" />
                                                <asp:Button ID="BtnCancel" runat="server" Text="Cancel" CommandName="CancelOne" Visible="false" />
                                                <asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="EditOne" />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField>
                                            <HeaderTemplate>
                                                Student ID<asp:TextBox ID="tid" runat="server" Visible="false"></asp:TextBox>
                                            </HeaderTemplate>
                                            <ItemTemplate>
                                                <asp:Label ID="stuid" runat="server" Text=' <%#Eval("sid")%>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField>
                                            <HeaderTemplate>
                                                Student Name<asp:TextBox ID="tn" runat="server" Visible="false"></asp:TextBox>
                                            </HeaderTemplate>
                                            <ItemTemplate>
                                                <asp:TextBox ID="txtname" runat="server" Text=' <%#Eval("sname")%>' Visible="false"></asp:TextBox>
                                                <asp:Label ID="stuname" runat="server" Text=' <%#Eval("sname")%>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField>
                                            <HeaderTemplate>
                                                Marks<asp:TextBox ID="tm" runat="server" Visible="false"></asp:TextBox>
                                            </HeaderTemplate>
                                            <ItemTemplate>
                                                <asp:TextBox ID="txtmrks" runat="server" Text=' <%#Eval("smarks")%>' Visible="false"></asp:TextBox>
                                                <asp:Label ID="stumarks" runat="server" Text=' <%#Eval("smarks")%>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Order">
                                            <ItemTemplate>
                                                <asp:ImageButton ID="imbup" runat="server" ImageUrl="~/Images/a-icon-alt-arrow-up.png"
                                                    CommandName="moveup" />
                                                <asp:ImageButton ID="imbdown" runat="server" ImageUrl="~/Images/a-icon-alt-arrow-down.png"
                                                    CommandName="movedown" />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                </asp:GridView>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
        <asp:Label ID="lblmsg" runat="server" Text="Label" Visible="false" ForeColor="Green"
            Font-Bold="true"></asp:Label>
    </div>
    </form>
</body>
</html>

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.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["studentmarks"].ToString());
    DataSet ds = new DataSet();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            bind();
        lblmsg.Visible = false;
        btnIns.Visible = false;
        btnvalidate.Visible = false;
        btnCancelInsert.Visible = false;
        displayGridNormally();
    }

    protected void bind()
    {
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from studentmarks order by recordorder", con);
        da.Fill(ds);
        grdOrdering.DataSource = ds;
        grdOrdering.DataBind();
        con.Close();
    }

    protected int getRowsCount()
    {
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from studentmarks order by recordorder", con);
        da.Fill(ds);
        con.Close();
        return ds.Tables[0].Rows.Count;
    }

    protected void displayGridNormally()
    {
        for (int i = 0; i < grdOrdering.Rows.Count; i++)
        {
            TextBox tname = (TextBox)grdOrdering.Rows[i].FindControl("txtname");
            TextBox tmarks = (TextBox)grdOrdering.Rows[i].FindControl("txtmrks");
            tname.Visible = false;
            tmarks.Visible = false;
            Label lblname = (Label)grdOrdering.Rows[i].FindControl("stuname");
            Label lblmarks = (Label)grdOrdering.Rows[i].FindControl("stumarks");
            lblname.Visible = true;
            lblmarks.Visible = true ;
            Button btnupdate = (Button)grdOrdering.Rows[i].Cells[0].FindControl("btnUpdate");
            btnupdate.Visible = false;
            Button btnedit = (Button)grdOrdering.Rows[i].Cells[0].FindControl("btnEdit");
            btnedit.Visible =true;
            Button btncancel = (Button)grdOrdering.Rows[i].Cells[0].FindControl("BtnCancel");
            btncancel.Visible = false;
            Button btndelete = (Button)grdOrdering.Rows[i].Cells[0].FindControl("BtnDel");
            btndelete.Visible = true;
        }
    }

    protected void grdOrdering_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button btndel = (Button)e.Row.FindControl("BtnDel"); //Use the button id of your button
            if (btndel != null)
            {
                btndel.CommandArgument = e.Row.RowIndex.ToString();
            }
            Button btnupdate = (Button)e.Row.FindControl("btnUpdate");
            if (btnupdate != null)
            {
                btnupdate.CommandArgument = e.Row.RowIndex.ToString();
            }
            Button btnedit = (Button)e.Row.FindControl("btnEdit");
            if (btnedit != null)
            {
                btnedit.CommandArgument = e.Row.RowIndex.ToString();
            }
            Button btncancel = (Button)e.Row.FindControl("BtnCancel");
            if (btncancel != null)
            {
                btncancel.CommandArgument = e.Row.RowIndex.ToString();
            }
            ImageButton imbup = (ImageButton)e.Row.FindControl("imbup");
            if (imbup != null)
            {
                imbup.CommandArgument = e.Row.RowIndex.ToString();
                if (Convert.ToInt32(e.Row.RowIndex.ToString()) == 0)
                    imbup.Visible = false;
            }
            ImageButton imbdown = (ImageButton)e.Row.FindControl("imbdown");
            if (imbdown != null)
            {
                imbdown.CommandArgument = e.Row.RowIndex.ToString();
                if (Convert.ToInt32(e.Row.RowIndex.ToString()) == (ds.Tables[0].Rows.Count - 1))
                    imbdown.Visible = false;
            }

        }
    }
    protected void grdOrdering_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToString() == "DeleteOne")
        {
            int rowIndex = int.Parse(e.CommandArgument.ToString());
            DataKey dk = grdOrdering.DataKeys[rowIndex];
            int sid = int.Parse(dk.Values["sid"].ToString());
            con.Open();
            SqlCommand cmd = new SqlCommand("delete from studentmarks where sid='" + sid + "'", con);
            cmd.ExecuteNonQuery();
            con.Close();
            lblmsg.Visible = true;
            lblmsg.Text = "Deleted Successfully";
            bind();
            
        } 
        else if (e.CommandName.ToString() == "moveup")
        {
            int rowIndex = int.Parse(e.CommandArgument.ToString());
            DataKey dk1 = grdOrdering.DataKeys[rowIndex];
            int sid1 = int.Parse(dk1.Values["sid"].ToString());
            DataKey dk2 = grdOrdering.DataKeys[rowIndex - 1];
            int sid2 = int.Parse(dk2.Values["sid"].ToString());
            con.Open();
            SqlCommand cmd = new SqlCommand("update studentmarks set recordorder="+(rowIndex - 1)+" where sid="+sid1, con);
            cmd.ExecuteNonQuery();
            cmd = new SqlCommand("update studentmarks set recordorder="+rowIndex+" where sid="+sid2, con);
            cmd.ExecuteNonQuery();
            con.Close();
            bind();
            
        }
        else if (e.CommandName.ToString() == "movedown")
        {
            int rowIndex = int.Parse(e.CommandArgument.ToString());
            DataKey dk1 = grdOrdering.DataKeys[rowIndex];
            int sid1 = int.Parse(dk1.Values["sid"].ToString());
            DataKey dk2 = grdOrdering.DataKeys[rowIndex+1];
            int sid2 = int.Parse(dk2.Values["sid"].ToString());
            con.Open();
            SqlCommand cmd = new SqlCommand("update studentmarks set recordorder=" + (rowIndex + 1) + " where sid=" + sid1, con);
            cmd.ExecuteNonQuery();
            cmd = new SqlCommand("update studentmarks set recordorder=" + rowIndex + " where sid=" + sid2, con);
            cmd.ExecuteNonQuery();
            con.Close();
            bind();
        }
        else if (e.CommandName.ToString() == "UpdateOne")
        {
            int rowIndex = int.Parse(e.CommandArgument.ToString());
            DataKey dk = grdOrdering.DataKeys[rowIndex];
            int sid = int.Parse(dk.Values["sid"].ToString());
            TextBox tname = (TextBox)grdOrdering.Rows[rowIndex].FindControl("txtname");
            TextBox tmarks = (TextBox)grdOrdering.Rows[rowIndex].FindControl("txtmrks");
            con.Open();
            SqlCommand cmd = new SqlCommand("update studentmarks set sname='" + tname.Text + "',smarks='" + tmarks.Text + "' where sid=" + sid, con);
            cmd.ExecuteNonQuery();
            con.Close();
            grdOrdering.EditIndex = -1;
            lblmsg.Visible = true;
            lblmsg.Text = "updated Successfully";
            bind();
        }
        else if (e.CommandName.ToString() == "EditOne")
        {
            int rowIndex = int.Parse(e.CommandArgument.ToString());
            TextBox tname = (TextBox)grdOrdering.Rows[rowIndex].FindControl("txtname");
            TextBox tmarks = (TextBox)grdOrdering.Rows[rowIndex].FindControl("txtmrks");
            tname.Visible = true;
            tmarks.Visible = true;
            Label lblname = (Label)grdOrdering.Rows[rowIndex].FindControl("stuname");
            Label lblmarks = (Label)grdOrdering.Rows[rowIndex].FindControl("stumarks");
            lblname.Visible = false;
            lblmarks.Visible = false;
            tname.Text = lblname.Text;
            tmarks.Text = lblmarks.Text;
            Button btnupdate = (Button)grdOrdering.Rows[rowIndex].Cells[0].FindControl("btnUpdate");
            btnupdate.Visible = true;
            Button btnedit = (Button)grdOrdering.Rows[rowIndex].Cells[0].FindControl("btnEdit");
            btnedit.Visible = false;
            Button btncancel = (Button)grdOrdering.Rows[rowIndex].Cells[0].FindControl("BtnCancel");
            btncancel.Visible = true;
            Button btndelete = (Button)grdOrdering.Rows[rowIndex].Cells[0].FindControl("BtnDel");
            btndelete.Visible = false;
        }
        else if (e.CommandName.ToString() == "CancelOne")
        {
            grdOrdering.EditIndex = -1;
        }


    }

   
    protected void btnIns_Click(object sender, EventArgs e)
    {
        btnIns.Visible = true;
        TextBox sid = grdOrdering.HeaderRow.FindControl("tid") as TextBox;
        TextBox sname = grdOrdering.HeaderRow.FindControl("tn") as TextBox;
        TextBox smarks = grdOrdering.HeaderRow.FindControl("tm") as TextBox;
        btnvalidate.Attributes.Add("OnClick", "return emptytxtboxvalidation('" + Convert.ToString(sid.ClientID) + "','" + Convert.ToString(sname.ClientID) + "','" + Convert.ToString(smarks.ClientID) + "');");
        if (sid.Text != "" && sname.Text != "" && smarks.Text != "")
        {
            btnvalidate.Visible = false;
            int rowcount = getRowsCount();            
            con.Open();
            string str1 = "insert into studentmarks values (' " + sid.Text + " ' , ' " + sname.Text + " ', ' " + smarks.Text + " ','" + (rowcount - 1) + "')";
            SqlCommand cmd = new SqlCommand(str1, con);
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
            con.Close();
            lnkAddNewRow.Visible = true;
            lblmsg.Visible = true;
            lblmsg.Text = "Data Inserted Successfully";
            bind();
            btnIns.Visible = false;
        }
        else
        {
            btnvalidate.Visible = true;
            btnvalidate.Focus();

        }

    }


    protected void lnkAddNewRow_Click(object sender, EventArgs e)
    {
        TextBox t1 = grdOrdering.HeaderRow.FindControl("tid") as TextBox;
        TextBox t2 = grdOrdering.HeaderRow.FindControl("tn") as TextBox;
        TextBox t3 = grdOrdering.HeaderRow.FindControl("tm") as TextBox;
        t1.Visible = true;
        t2.Visible = true;
        t3.Visible = true;
        btnCancelInsert.Visible = true;
        lnkAddNewRow.Visible = false;
        btnIns.Visible = true;
    }

    protected void btnCancelInsert_Click(object sender, EventArgs e)
    {
        lnkAddNewRow.Visible = true;
        btnIns.Visible = false;
        btnvalidate.Visible = false;
        btnCancelInsert.Visible = false;
        TextBox t1 = grdOrdering.HeaderRow.FindControl("tid") as TextBox;
        TextBox t2 = grdOrdering.HeaderRow.FindControl("tn") as TextBox;
        TextBox t3 = grdOrdering.HeaderRow.FindControl("tm") as TextBox;
        t1.Visible = false;
        t2.Visible = false;
        t3.Visible = false;
        
    }
}

Tuesday, 6 March 2012

Xml values saved in database in asp.net

<?xml version="1.0" encoding="utf-8" ?>
<employee>
  <empdetails>
  <name>santosh</name>
  <age>24</age>
  <salary>20000</salary>
  </empdetails>
  <empdetails>
    <name>srinu vas</name>
    <age>21</age>
    <salary>30000</salary>
  </empdetails>
  <empdetails>
    <name>anil</name>
    <age>25</age>
    <salary>40000</salary>
  </empdetails>
</employee>

USE [log]
GO
/****** Object:  Table [dbo].[employe]    Script Date: 03/06/2012 16:35:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[employe](
 [id] [int] IDENTITY(1,1) NOT NULL,
 [name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [age] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [salary] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="xmlinsert.aspx.cs" Inherits="xmlinsert" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <h2 style="color: blue; font-style: normal">
        Xml values saved in table
        <br />
        in asp.net
    </h2>
    <div>
        <asp:Button ID="Button1" runat="server" Text="Insert" OnClick="Button1_Click" /><br />
        <asp:Label ID="lb1" runat="server" Text="" ForeColor="Blue"></asp:Label>
    </div>
    </form>
</body>
</html>

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.Data.SqlClient;

public partial class xmlinsert : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["xmltest"].ConnectionString);
    string name = "";
    string age = "";
    string salary = "";
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("App_Data/testxml.xml"));
        for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
        {
            name = ds.Tables[0].Rows[i][0].ToString();
            age = ds.Tables[0].Rows[i][1].ToString();
            salary = ds.Tables[0].Rows[i][2].ToString();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "INSERT INTO [employe] values(@name,@age,@salary)";
            cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = name;
            cmd.Parameters.Add("@age", SqlDbType.VarChar).Value = age;
            cmd.Parameters.Add("@salary", SqlDbType.VarChar).Value = salary;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            
        }
        lb1.Text = "insert sucessfully";
    }
}



Xml values Show in Row and column wise in asp.net

<?xml version="1.0" encoding="utf-8" ?>
<employee>
  <empdetails>
  <name>santosh</name>
  <age>24</age>
  <salary>20000</salary>
  </empdetails>
  <empdetails>
    <name>srinu vas</name>
    <age>21</age>
    <salary>30000</salary>
  </empdetails>
  <empdetails>
    <name>anil</name>
    <age>25</age>
    <salary>40000</salary>
  </empdetails>
</employee>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="xmldisp.aspx.cs" Inherits="xmlinsert" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <h2 style="color: blue; font-style: normal">
        Xml values Show in Row wise
        <br />
        in asp.net
    </h2>
    <div>
        <asp:Button ID="Button1" runat="server" Text="Read" OnClick="Button1_Click" />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>
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;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("App_Data/testxml.xml"));
        int i = 0;

        for (i = 0; i <= (ds.Tables[0].Rows.Count - 1); i++)
        {
            int j = 0;
            for (j = 0; j <= (ds.Tables[0].Rows[i].ItemArray.Length) - 1; j++)
            {
             Response.Write("row:" + i.ToString() + " col:" + j.ToString() + " >>> " + ds.Tables[0].Rows[i].ItemArray[j].ToString() + "<br />");
            }
        }
    }
}




xml file read in asp.net

<?xml version="1.0" encoding="utf-8" ?>
<employee>
  <empdetails>
  <name>santosh</name>
  <age>24</age>
  <salary>20000</salary>
  </empdetails>
  <empdetails>
    <name>srinu vas</name>
    <age>21</age>
    <salary>30000</salary>
  </empdetails>
  <empdetails>
    <name>anil</name>
    <age>25</age>
    <salary>40000</salary>
  </empdetails>
</employee>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="xml2.aspx.cs" Inherits="xml2" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <h2 style="color: blue; font-style: normal">
        Xml values Show in lable
        <br />
        in asp.net
    </h2>
    <div>
        <asp:Button ID="Button1" runat="server" Text="READ" OnClick="Button1_Click" />
        <br />
        <asp:Label ID="Label1" runat="server" Text="" ForeColor="Blue"></asp:Label>
    </div>
    </form>
</body>
</html>

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.Xml;

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

    }
    public void Button1_Click(object sender, EventArgs e)
    {
        XmlTextReader reader = new XmlTextReader(Server.MapPath("App_Data/testxml.xml"));
        reader.Read();
        while (reader.Read())
        {
            Label1.Text = reader.Name.ToString() + " <br/>" + reader.Value.ToString();
        }


    }
}



gridview using xmldatasourse in asp.net

<?xml version="1.0" encoding="utf-8" ?>
<employee>
  <empdetails>
  <name>santosh</name>
  <age>24</age>
  <salary>20000</salary>
  </empdetails>
  <empdetails>
    <name>srinu vas</name>
    <age>21</age>
    <salary>30000</salary>
  </empdetails>
  <empdetails>
    <name>anil</name>
    <age>25</age>
    <salary>40000</salary>
  </empdetails>
</employee>
<%@ 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>
</head>
<body>
    <form id="form1" runat="server">
    <h2 style=" color:blue; font-style:normal ">  
           Xml values Show in Gridview 
            <br /> in asp.net  
        </h2> 
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
            ForeColor="#333333" GridLines="None">
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#EFF3FB" />
            <Columns>
                <asp:BoundField DataField="name" HeaderText="Emp Name" />
                <asp:BoundField DataField="age" HeaderText="Emp Age" />
                <asp:BoundField DataField="salary" HeaderText="Emp salary" />
            </Columns>
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>
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)
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("App_Data/testxml.xml"));
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();

    }
}