Click here to Skip to main content
16,006,440 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table align="center"><tr><td>
        <asp:GridView ID="gv1" runat="server"
            AutoGenerateColumns="False" AutoGenerateDeleteButton="True" BackColor="LightGoldenrodYellow"
            BorderColor="Tan" BorderWidth="1px" CellPadding="2"
            GridLines="None" onrowdeleting="gv1_RowDeleting" ForeColor="Black"
            AutoGenerateEditButton="True" onrowcancelingedit="gv1_RowCancelingEdit"
            onrowediting="gv1_RowEditing" onrowupdating="gv1_RowUpdating">
    <Columns>
    <asp:BoundField HeaderText="Dept Id" DataField="DeptId"/>
        <asp:BoundField HeaderText="Dept Name" DataField="DepartmentName"/>


    </Columns>
        <FooterStyle BackColor="Tan" />
        <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
                HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
        <HeaderStyle BackColor="Tan" Font-Bold="True" />
        <AlternatingRowStyle BackColor="PaleGoldenrod" />
    </asp:GridView></td></tr></table>
    <table align="center" bgcolor="#ff9900"><tr><td><asp:Label ID="lblDEptId" Text="Dept Id" runat="server"></asp:Label> </td>
    <td><asp:TextBox ID="txtlblDEptId" runat="server"></asp:TextBox></td></tr>
    <tr><td><asp:Label ID="LblDeptName" Text="Dept Name" runat="server"></asp:Label> </td>
    <td><asp:TextBox ID="TxtDeptName" runat="server"></asp:TextBox></td></tr>
   </table>
        <table align="center"><tr><td><asp:Button ID="btnsave" Text="Save" runat="server"
                onclick="btnsave_Click" /></td>
        <td><asp:Button ID="Btncancel" Text="Cancel" runat="server"
                onclick="Btncancel_Click" /></td></tr></table>

    </div>
    </form>
</body>
</html>


protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            GetData();
        }
    }
    private void GetData()
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from Department", con);
        DataSet ds = new DataSet();
        da.Fill(ds, "Department");
        gv1.DataSource = ds.Tables[0];
        gv1.DataBind();
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        string s = "insert into Department values('" + txtlblDEptId.Text + "','" + TxtDeptName.Text + "')";
        SqlCommand cmd = new SqlCommand(s, con);
        cmd.CommandType = CommandType.Text;
        con.Open();
        int i = cmd.ExecuteNonQuery();
        con.Close();
        GetData();
    }
    protected void gv1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Response.Write(e.RowIndex);
        string s = gv1.Rows[e.RowIndex].Cells[1].Text;
        Response.Write(s);
        da = new SqlDataAdapter("delete from Department where DeptId=" + s, con);
        ds = new DataSet();
        da.Fill(ds, "Department");
        GetData();
    }
    protected void Btncancel_Click(object sender, EventArgs e)
    {
        txtlblDEptId.Text = "";
        TxtDeptName.Text = "";
       
    }
    protected void gv1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gv1.EditIndex = e.NewEditIndex;
        GetData();  
    }
    protected void gv1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gv1.EditIndex = -1;
        GetData();
    }
    protected void gv1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow r = gv1.Rows[e.RowIndex];
        int mdeptid; 
        string mdeptname;
        
        TextBox t1 = (TextBox)r.Cells[1].Controls[0];
        mdeptid = Convert.ToInt32(t1.Text);
        t1 = (TextBox)r.Cells[2].Controls[0];
        mdeptname = (t1.Text);

        string s = "update Department set DepartmentName='" + mdeptname + "' where DeptId=" + mdeptid;
        SqlCommand cmd = new SqlCommand(s, con);
        cmd.CommandType = CommandType.Text;
        con.Open();
        int i = cmd.ExecuteNonQuery();
        con.Close();
        gv1.EditIndex = -1;
        GetData();     
    }
}
Posted
Updated 31-May-11 20:06pm
v2

1 solution

Dear, I think first you have to learn what is the three tier architecture and do it by itself and then if you are facing any issue then post here your issue. I don't think so any one will convert your code into 3 Tier Architecture.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Jun-11 14:15pm    
Agree, a 5.
--SA
Parwej Ahamad 1-Jun-11 14:24pm    
Thanks Dear, Really feel good if someone posting comment like you great person. Thanks once again.
thatraja 1-Jun-11 23:42pm    
Yes, he is. BTW if you want to comment for his comment, use Reply link on the comment so it will notify him. I did the same way now you have my reply & also in mail.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900