Tuesday, July 22, 2014

GridView Edit Update Cancel

<%@ 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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="ID"
            OnRowCancelingEdit="GridView1_RowCancelingEdit" CellPadding="4" ForeColor="#333333"
            OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="LkB1" runat="server" CommandName="Edit">Edit</asp:LinkButton>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:LinkButton ID="LB2" runat="server" CommandName="Update">Update</asp:LinkButton>
                        <asp:LinkButton ID="LB3" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txt_Name" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Branch">
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("Branch") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txt_Branch" runat="server" Text='<%# Eval("Branch") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="City">
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Eval("City") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txt_City" runat="server" Text='<%# Eval("City") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        </asp:GridView>
    </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.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(@"Data Source=KCLINK-PC;Initial Catalog=Naresh;Integrated Security=True");

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView();
        }
    }
    //method for binding GridView
    protected void BindGridView()
    {
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter("Select ID, Name,Branch,City from tbl_student", con);
        con.Open();
        da.Fill(dt);
        con.Close();

        if (dt.Rows.Count > 0)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
    // row edit event
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindGridView();

    }
    // row update event
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        // find student id of edit row
        string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
        // find updated values for update
        TextBox name = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_Name");
        TextBox branch = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_Branch");
        TextBox city = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_City");

        SqlCommand cmd = new SqlCommand("update tbl_student set Name='" + name.Text + "',Branch='" + branch.Text + "', City='" + city.Text + "' where ID=" + id + "", con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();

        GridView1.EditIndex = -1;
        BindGridView();
    }
    // cancel row edit event
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindGridView();
    }
}
===========================================================================
data base

CREATE TABLE [dbo].[tbl_student](
      [Id] [int] NULL,
      [Name] [varchar](50) NULL,
      [Branch] [varchar](50) NULL,
      [City] [varchar](50) NULL)


Error Message In PopUp Window

ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");

Store In Xml to Bind Gridview



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

<!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">
        .ButtonStyle
        {
            background-color: #9EBEDE;
            color: Black;
            font-family: verdana;
            font-size: 8pt;
        }
        .BarStyle
        {
            background-color: #996633;
        }
        .TablePollResultFoot
        {
            background-color: #B0C4DE;
            font-weight: bold;
            height: 30px;
            font-size: 13px;
        }
        .gridview
        {
            border: solid 1px #CCCCCC;
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table style="border: 1px solid #9EBEDE" align="center" cellpadding="0" cellspacing="0"
            width="100%">
            <tr>
                <td align="left">
                    <b><span style="color: #FF6600">Online Poll Example with XML </span></b>
                </td>
            </tr>
            <tr>
                <td height="10px">
                </td>
            </tr>
            <tr>
                <td>
                    <b>which one is best browser?</b>
                </td>
            </tr>
            <tr>
                <td height="5px">
                </td>
            </tr>
            <tr>
                <td align="left">
                    <asp:RadioButtonList ID="radVote" runat="server">
                        <asp:ListItem>Mozilla</asp:ListItem>
                        <asp:ListItem>Internet Explorer</asp:ListItem>
                        <asp:ListItem>Google Chrome</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
            </tr>
            <tr>
                <td>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnVote" runat="server" Text="Vote" CssClass="ButtonStyle" OnClick="btnVote_Click" />
                    <asp:Button ID="btnResult" runat="server" Text="Reult" CssClass="ButtonStyle" OnClick="btnResult_Click" />
                    <br />
                    <asp:Label ID="lblStatus" runat="server" />
                </td>
            </tr>
            <tr>
                <td align="left">
                    <asp:GridView runat="server" ID="gvResult" BackColor="White" CellPadding="4" EnableModelValidation="True"
                        AutoGenerateColumns="false" OnRowDataBound="gvResult_RowDataBound" EmptyDataText="No one submit votes"
                        Style="margin-bottom: 82px">
                        <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                        <HeaderStyle BackColor="#4682B4" Font-Bold="True" ForeColor="#FFFFCC" />
                        <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
                        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                        <Columns>
                            <asp:TemplateField HeaderText="Options" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="20%">
                                <ItemTemplate>
                                    <asp:Label ID="lblOptions" runat="server" Text='<%#Bind("OPTION_NAME") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Votes" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="10%">
                                <ItemTemplate>
                                    <asp:Label ID="lblVotes" runat="server" Text='<%#Bind("VOTES") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="%" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="15%">
                                <ItemTemplate>
                                    <asp:Label ID="lblpercentage" runat="server" Text='<%#Bind("PERCENTAGE","{0:f2}") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Bar" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="55%">
                                <ItemTemplate>
                                    <table runat="server" id="tblBar">
                                        <tr class="BarStyle">
                                            <td height="8px">
                                            </td>
                                        </tr>
                                    </table>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
 ==================================================================
<?xml version="1.0" encoding="utf-8"?>
<VotesInformation>
  <Vote>
    <Choice>Mozilla</Choice>
  </Vote>
  <Vote>
    <Choice>Internet Explorer</Choice>
  </Vote>
  <Vote>
    <Choice>Google Chrome</Choice>
  </Vote>
  <Vote>
    <Choice>Mozilla</Choice>
  </Vote>
  <Vote>
    <Choice>Mozilla</Choice>
  </Vote>
  <Vote>
    <Choice>Internet Explorer</Choice>
  </Vote>
  <Vote>
    <Choice>Google Chrome</Choice>
  </Vote>
  <Vote>
    <Choice>Google Chrome</Choice>
  </Vote>
  <Vote>
    <Choice>Internet Explorer</Choice>
  </Vote>
  <Vote>
    <Choice>Internet Explorer</Choice>
  </Vote>
  <Vote>
    <Choice>Google Chrome</Choice>
  </Vote>
  <Vote>
    <Choice>Internet Explorer</Choice>
  </Vote>
  <Vote>
    <Choice>Internet Explorer</Choice>
  </Vote>
  <Vote>
    <Choice>Mozilla</Choice>
  </Vote>
  <Vote>
    <Choice>Google Chrome</Choice>
  </Vote>
  <Vote>
    <Choice>Mozilla</Choice>
  </Vote>
  <Vote>
    <Choice>Google Chrome</Choice>
  </Vote>
  <Vote>
    <Choice>Internet Explorer</Choice>
  </Vote>
  <Vote>
    <Choice>Mozilla</Choice>
  </Vote>
  <Vote>
    <Choice>Google Chrome</Choice>
  </Vote>
  <Vote>
    <Choice>Internet Explorer</Choice>
  </Vote>
  <Vote>
    <Choice>Internet Explorer</Choice>
  </Vote>
  <Vote>
    <Choice>Mozilla</Choice>
  </Vote>
  <Vote>
    <Choice>Internet Explorer</Choice>
  </Vote>
  <Vote>
    <Choice>Internet Explorer</Choice>
  </Vote>
  <Vote>
    <Choice>Internet Explorer</Choice>
  </Vote>
  <Vote>
    <Choice>Internet Explorer</Choice>
  </Vote>
  <Vote>
    <Choice>Internet Explorer</Choice>
  </Vote>
  <Vote>
    <Choice>Internet Explorer</Choice>
  </Vote>
  <Vote>
    <Choice>Internet Explorer</Choice>
  </Vote>
  <Vote>
    <Choice>Internet Explorer</Choice>
  </Vote>
  <Vote>
    <Choice>Google Chrome</Choice>
  </Vote>
  <Vote>
    <Choice>Google Chrome</Choice>
  </Vote>
  <Vote>
    <Choice>Internet Explorer</Choice>
  </Vote>
  <Vote>
    <Choice>Google Chrome</Choice>
  </Vote>
</VotesInformation>