Wednesday, January 29, 2014

abc

cs file

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 Default2 : System.Web.UI.Page
{
    private SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Documents and Settings\\Administrator\\My Documents\\Rating.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindEmployeeDetails();
        }
    }

    protected void BindEmployeeDetails()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from inclusion where ttid=2", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            GridView1.DataSource = ds;
            GridView1.DataBind();
            int columncount = GridView1.Rows[0].Cells.Count;
            GridView1.Rows[0].Cells.Clear();
            GridView1.Rows[0].Cells.Add(new TableCell());
            GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
            GridView1.Rows[0].Cells[0].Text = "No Records Found";
        }

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["serialno"].ToString());
        Response.Write(userid);
        con.Open();
        SqlCommand cmd = new SqlCommand("delete from inclusion where serialno=" + userid, con);
        int result = cmd.ExecuteNonQuery();
        con.Close();
        if (result == 1)
        {
            BindEmployeeDetails();
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindEmployeeDetails();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
        int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["serialno"].ToString());
        TextBox txtdesc = (TextBox)GridView1.Rows[e.RowIndex].FindControl("et1");
        TextBox txtthin = (TextBox)GridView1.Rows[e.RowIndex].FindControl("et2");
        TextBox txtthick = (TextBox)GridView1.Rows[e.RowIndex].FindControl("et3");
        con.Open();
        SqlCommand cmd = new SqlCommand("update inclusion set description='" + txtdesc.Text + "',thin='" + txtthin.Text + "',thick='" + txtthick.Text + "' where serialno=" + userid, con);
        cmd.ExecuteNonQuery();
        con.Close();
     
        GridView1.EditIndex = -1;
        BindEmployeeDetails();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1 .EditIndex = -1;
        BindEmployeeDetails();
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("AddNew"))
        {
            TextBox txtdesc = (TextBox)GridView1.FooterRow.FindControl("t1");
            TextBox txtthin = (TextBox)GridView1.FooterRow.FindControl("t2");
            TextBox txtthick = (TextBox)GridView1.FooterRow.FindControl("t3");
            con.Open();
            SqlCommand cmd =
                new SqlCommand(
                    "insert into inclusion(ttid,description,thin,thick) values(" + 2 + ",'" + txtdesc.Text + "','" +
                    txtthin.Text + "','" + txtthick.Text + "')", con);
            int result = cmd.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                BindEmployeeDetails();
            }
            else
            {
               
            }


        }
    }
           

}

aspx file

<%@ 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">
    <div>
    
    </div>
    <asp:GridView ID="GridView1" DataKeyNames="serialno"  runat="server" ShowFooter ="true" ValidationGroup="va" AutoGenerateColumns="false" 
    OnRowCommand="GridView1_RowCommand" OnRowDeleting="GridView1_RowDeleting"
    OnRowEditing ="GridView1_RowEditing" OnRowUpdating ="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit">
    <Columns >
        <asp:TemplateField >
            <EditItemTemplate >
                <asp:ImageButton ID="imgbtnUpdate" CommandName ="Update" ImageUrl ="~/Images/update.jpg" runat="server" Width="20px" Height="20px" />
                <asp:ImageButton ID="imgbtnCancel" CommandName ="Cancel" ImageUrl ="~/Images/Cancel.jpg" runat="server" Width="20px" Height ="20px" />
            </EditItemTemplate>
            <ItemTemplate >
                  <asp:ImageButton ID="imgbtnEdit" CommandName ="Edit" ImageUrl ="~/Images/Edit.jpg" runat="server" Width="20px" Height ="20px" />
                    <asp:ImageButton ID="imgbtnDelete" CommandName ="Delete" ImageUrl ="~/Images/delete.jpg" runat="server" Width="20px" Height ="20px" />
            </ItemTemplate>
            <FooterTemplate >
                  <asp:ImageButton ID="imgbtnAdd" CommandName ="AddNew" ImageUrl ="~/Images/AddNewitem.jpg" runat="server" Width="30px" Height ="30px" />
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Srno" >
            <ItemTemplate >
                    <%# Container .DataItemIndex + 1 %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText ="Descripation ">
            <EditItemTemplate >
                <asp:TextBox ID="et1" runat="server" Text='<%#Eval("description") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate >
                <asp:Label ID="il1" runat="server" Text='<%#Eval("description") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="t1" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Text="*" ControlToValidate ="t1" ValidationGroup="va"></asp:RequiredFieldValidator>
            </FooterTemplate>
        </asp:TemplateField>
         <asp:TemplateField HeaderText ="Thin ">
            <EditItemTemplate >
                <asp:TextBox ID="et2" runat="server" Text='<%#Eval("thin") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate >
                <asp:Label ID="il2" runat="server" Text='<%#Eval("thin") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="t2" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="r2" runat="server" Text="*" ControlToValidate ="t2" ValidationGroup="va"></asp:RequiredFieldValidator>
            </FooterTemplate>
        </asp:TemplateField>
         <asp:TemplateField HeaderText ="Thick ">
            <EditItemTemplate >
                <asp:TextBox ID="et3" runat="server" Text='<%#Eval("thick") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate >
                <asp:Label ID="il3" runat="server" Text='<%#Eval("thick") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="t3" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="r3" runat="server" Text="*" ControlToValidate ="t3" ValidationGroup="va"></asp:RequiredFieldValidator>
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
    </asp:GridView>
    </form>
</body>
</html>

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete