selva kumar

selva kumar

  • NA
  • 557
  • 188.7k

Gridview Update command does not working properly?

Jan 27 2014 3:18 AM
Hi friends, i am using gridview insert,update,delete.All are working fine except update command.when i click update command it updates with existing value.can any one help me out.i m stuck up with this problem on past 4 days...

Here is my code:

Asp:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" ShowFooter="True" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal">
                        <Columns>
                       
                        <asp:TemplateField>
                        <ItemTemplate>
                        <asp:Label ID="lblid" runat="server" Text='<%#Eval("T_ID") %>' Visible="false"></asp:Label>
                        </ItemTemplate>
                        </asp:TemplateField>
                       
                        <asp:TemplateField HeaderText="Branch">
                        <ItemTemplate>
                        <asp:Label ID="lblbranch" runat="server" Text='<%#Eval("Branch") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("Branch") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                        <asp:TextBox ID="txtaddbranch" runat="server"></asp:TextBox>
                        </FooterTemplate>
                        </asp:TemplateField>
                       
                        <asp:TemplateField HeaderText="Username">
                        <ItemTemplate>
                        <asp:Label ID="lblusername" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("Name") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                        <asp:TextBox ID="txtaddusername" runat="server"></asp:TextBox>
                        </FooterTemplate>
                        </asp:TemplateField>
                       
                        <asp:TemplateField HeaderText="Password">
                        <ItemTemplate>
                        <asp:Label ID="lblpassword" runat="server" Text='<%#Eval("Password") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                        <asp:TextBox ID="TextBox3" runat="server" Text='<%#Eval("Password") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                        <asp:TextBox ID="txtaddpassword" runat="server"></asp:TextBox>
                        </FooterTemplate>
                        </asp:TemplateField>
                       
                        <asp:TemplateField>
                        <FooterTemplate>
                        <p style="text-align:center">
                        <asp:LinkButton ID="linkadd" runat="server" Font-Bold="true" ForeColor="#3F62B4" CommandName="Add" Text="Add" Width="100px"></asp:LinkButton>
                        </p>
                        </FooterTemplate>
                        </asp:TemplateField>
                       
                        </Columns>
                        <RowStyle BackColor="White" ForeColor="#333333" />
                        <FooterStyle BackColor="White" ForeColor="#333333" />
                        <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
                        <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
                    </asp:GridView>

c#:

public partial class Admin_UserRole : System.Web.UI.Page
{
    SqlConnection Con;
    SqlCommand Cmd;
    SqlDataAdapter Sdap;
    protected void Page_Load(object sender, EventArgs e)
    {
        Con = new SqlConnection(@"Data Source=SMAART-1F1D3825\SQLEXPRESS;Initial Catalog=SundarTyres;Integrated Security=True");
        if (!IsPostBack)
        {
            gvbind();
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Add")
            {
                TextBox txtaddbranch = (TextBox)GridView1.FooterRow.FindControl("txtaddbranch");
                TextBox txtaddusername = (TextBox)GridView1.FooterRow.FindControl("txtaddusername");
                TextBox txtaddpassword = (TextBox)GridView1.FooterRow.FindControl("txtaddpassword");

                Con.Open();
                Cmd = new SqlCommand("Insert Into Admin_Login Values('"+txtaddbranch.Text+"','"+txtaddusername.Text+"','"+txtaddpassword.Text+"')", Con);
                Cmd.ExecuteNonQuery();
            }
        }
        catch
        {
        }
        finally
        {
            Con.Close();
            gvbind();
        }
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex];
        Label id = (Label)row.FindControl("lblid");
        int TID = Convert.ToInt32(id.Text);

        Con.Open();
        TextBox Branch = (TextBox)row.FindControl("TextBox1");
        TextBox Name = (TextBox)row.FindControl("TextBox2");
        TextBox Password = (TextBox)row.FindControl("TextBox3");

        Cmd = new SqlCommand("Update Admin_Login Set Branch='" + Branch.Text + "',Name='" + Name.Text + "',Password='" + Password.Text + "' Where T_ID=" + TID + "", Con);
        Cmd.ExecuteNonQuery();
        Con.Close();

        GridView1.EditIndex = -1;
        gvbind();
       
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex];
        Label lblid = (Label)row.FindControl("lblid");
        int T_ID = Convert.ToInt32(lblid.Text);
        Con.Open();
        Cmd = new SqlCommand("Delete From Admin_Login Where T_ID="+T_ID+"",Con);
        Cmd.ExecuteNonQuery();
        Con.Close();
        GridView1.EditIndex = -1;
        gvbind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        gvbind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        gvbind();
    }
    public void gvbind()
    {
        try
        {
            Con.Open();
            Sdap = new SqlDataAdapter("Select * From Admin_Login", Con);
            DataSet ds = new DataSet();
            Sdap.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        catch
        {
        }
        finally
        {
            Con.Close();
        }
    }
}


Answers (5)