Click here to Skip to main content
15,917,320 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In aspx, i have:

HTML
<asp:GridView ID="gvDetails" AutoGenerateColumns="False" AllowPaging="True" PageSize="4"
    DataKeyNames="ID" runat="server" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
    BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal" OnPageIndexChanging="gvDetails_PageIndexChanging"
    OnRowCancelingEdit="gvDetails_RowCancelingEdit" OnRowEditing="gvDetails_RowEditing"
    OnRowUpdating="gvDetails_RowUpdating" OnRowDataBound="gvDetails_RowDataBound"
    OnRowDeleting="gvDetails_RowDeleting">
   <Columns>
    <asp:CommandField ButtonType="Link" HeaderText="Action" ShowEditButton="true" ShowDeleteButton="true"
        ShowCancelButton="true" />
        <asp:TemplateField HeaderText="Name">
    <EditItemTemplate>
        <asp:TextBox ID="TextBoxEditName" runat="server" Text='<%# Bind("Name") %>' />
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="LabelName" runat="server" Text='<%# Bind("Name") %>' />
    </ItemTemplate>
    <FooterTemplate>
        <asp:TextBox ID="TextBoxName" runat="server" Text='<%# Bind("Name") %>' />
    </FooterTemplate>
</asp:TemplateField> 
   </Columns>
                        <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
                        <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
                        <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
                    </asp:GridView>


In CS:
C#
{
    SqlCommand cmd, upd;
    SqlConnection con;
    SqlDataAdapter da;
    DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
        con.Open();
        if (Request.Cookies["Username"] == null || Request.Cookies["Password"] == null)
        {
            Response.Redirect("../../Registration/Login.aspx");
        }
        else
        {
            lblUser.Text = Request.Cookies["Username"].Value.ToString();
            lblUser.ForeColor = System.Drawing.Color.White;
            Session["Username"] = Request.Cookies["Username"].Value.ToString();
        }
        if (!IsPostBack)
        {
            bindresults();
            gvDetails.DataSource = dt;
            gvDetails.DataBind();
        }

    }
    public DataTable bindresults()
    {
        cmd = new SqlCommand("select * from Registration", con);
        da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        gvDetails.DataSource = dt;
        gvDetails.DataBind();
        return dt;
    }
    protected void gvDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvDetails.PageIndex = e.NewPageIndex;
        bindresults();
        gvDetails.DataSource = dt;
        gvDetails.DataBind();
    }
    protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvDetails.EditIndex = e.NewEditIndex;
        bindresults();
        gvDetails.DataSource = dt;
        gvDetails.DataBind();
    }
    protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvDetails.EditIndex = -1;
        bindresults();
        gvDetails.DataSource = dt;
        gvDetails.DataBind();
    }
    protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int ID = int.Parse(gvDetails.DataKeys[e.RowIndex].Value.ToString());
        //string Username = ((TextBox)gvDetails.Rows[e.RowIndex].Cells[0].FindControl("TextBoxEditUsername")).Text;
        string Name = ((TextBox)gvDetails.Rows[e.RowIndex].Cells[1].FindControl("TextBoxEditName")).Text;
        string Gender = ((TextBox)gvDetails.Rows[e.RowIndex].Cells[2].FindControl("TextBoxEditGender")).Text;
        string Occupation = ((TextBox)gvDetails.Rows[e.RowIndex].Cells[3].FindControl("TextBoxEditOccupation")).Text;
        string Address = ((TextBox)gvDetails.Rows[e.RowIndex].Cells[4].FindControl("TextBoxEditAddress")).Text;
        upd = new SqlCommand("update Registration set Name = '" + Name + "', Gender = '" + Gender + "', Occupation = '" + Occupation + "', Address = '" + Address + "' where ID = '" + ID + "'", con);
        upd.ExecuteNonQuery();
        gvDetails.EditIndex = -1;
        bindresults();
        gvDetails.DataSource = dt;
        gvDetails.DataBind();
    }
    protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int ID = int.Parse(gvDetails.DataKeys[e.RowIndex].Value.ToString());
        cmd = new SqlCommand("delete from Registration where ID = " + ID + "", con);
        cmd.ExecuteNonQuery();
        bindresults();
        gvDetails.DataSource = dt;
        gvDetails.DataBind();
    }
    protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (!DataBinder.Eval(e.Row.DataItem, "Username").Equals(Session["Username"].ToString()))
            {
                LinkButton btn = (LinkButton)e.Row.Cells[0].Controls[0];
                LinkButton btn1 = (LinkButton)e.Row.Cells[0].Controls[2];

                if (btn != null && btn1 != null)
                {
                    btn.Visible = false;
                    btn1.Visible = false;
                }
            }
        }
    }
}

It is working fine, but its really lengthy. I was wondering if someone could provide a less code. I know i may be sounding foolish but for many, this may be worth asking.
Posted
Updated 7-Sep-12 22:34pm
v2

1 solution

Well, every operation has one method for itself.

For update or delete, you got few lines that gets the data and update it. There are not too many lines. There is no other way to reduce it. It looks lengthy you you as there are lots of operations defined here.
 
Share this answer
 

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