Samreen Farooqi Samreen Farooqi - 2 months ago 19
ASP.NET (C#) Question

Editable Grid view update is not working

i am trying to update gridview data using editable gridview but i am unable to get the edit template values in code behind.Here is the code to update grid view

protected void mGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string name="";
string email="";
string password="";
string phone="";
string address="";
string id="";
try
{
password = ((TextBox)mGrid.Rows[e.RowIndex].FindControl("txtMPass")).Text;
phone = ((TextBox)mGrid.Rows[e.RowIndex].FindControl("txtMphone")).Text;
address = ((TextBox)mGrid.Rows[e.RowIndex].FindControl("txtMaddress")).Text;

name = ((Label)mGrid.Rows[e.RowIndex].FindControl("lblMname")).Text;
email = ((Label)mGrid.Rows[e.RowIndex].FindControl("lblMemail")).Text;

id = getUID(email, name);

com.CommandText = "UPDATE Users SET userPass=@pass,userPh=@ph,userAdd=@add WHERE userID=@id and userType='Manager'";
com.Parameters.AddWithValue("@pass", password);
com.Parameters.AddWithValue("@ph", phone);
com.Parameters.AddWithValue("@add", address);
com.Parameters.AddWithValue("@id",id );
com.ExecuteNonQuery();
}
catch(Exception ex)
{
Response.Write(ex.ToString());
}

mGrid.EditIndex = -1;
loadGrid();
Response.Write(name + email + password + phone + address+"");
}

private void loadGrid()
{
com.CommandText = "select * from Users where userType='Manager'";
try
{
SqlDataReader r = com.ExecuteReader();
mGrid.DataSource = r;
mGrid.DataBind();
r.Close();
}
catch (Exception exp)
{
Response.Write(exp.ToString());
}
}


in Response.Write(---) i am getting the old values instead of the values i changed inside the grid

here is my gridview

<div class="col-md-9 table-responsive" style="margin- top:10%;float:right;margin-right:6%;font-family:Calibri;font-size:13px;">
<asp:GridView ID="mGrid" runat="server" AutoGenerateColumns="false" OnRowUpdating="mGrid_RowUpdating"
CssClass="table table-striped" onrowediting="EditCustomer" GridLines="None"
OnRowCancelingEdit="CancelEdit" >
<Columns>
<asp:TemplateField>
<ItemTemplate >
<asp:Image ImageUrl="~/images/manager.png" Width="30" Height="30" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate >
<asp:Label ID="lblMname" runat="server" Text='<%# Eval("userName")%>' ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblMname" runat="server" Text='<%# Eval("userName")%>' ></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label ID="lblMemail" runat="server" Text='<%# Eval("userEmail")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblMemail" runat="server" Text='<%# Eval("userEmail")%>'></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password" >
<ItemTemplate>
<asp:Label ID="lblMPass" runat="server" Text='<%# Eval("userPass")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtMPass" runat="server" Text='<%# Eval("userPass")%>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Phone">
<ItemTemplate>
<asp:Label ID="lblMphone" runat="server" Text='<%# Eval("userPh")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtMphone" runat="server" Text='<%# Eval("userPh")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:Label ID="lblMaddress" runat="server" Text='<%# Eval("userAdd")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtMaddress" runat="server" Text='<%# Eval("userAdd")%>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkRemove" runat="server" CommandArgument = '<%# Eval("userID")%>'
OnClientClick = "return confirm('Do you want to delete?')"
Text = "Delete" OnClick="lnkRemove_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
</div>


if someone can help me out?

Answer

You have to put the initial loading of the GridView values inside a IsPostBack check, otherwise the default values will be loaded and will overwrite the submitted ones.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        loadGrid();
    }
}