Gridview not showing new updated values when rowupdating event is triggered

asp.net

am using a gridview. When I click on the edit button the update and cancel button appear. Upon modifying the values in textbox which come from EditItemTemplate , the new values dont show in the event handler rowupdating(), instead I get the values which appear when the page was rendered. How do I grab the new values from these textboxes and proceed further? Here is the code.

aspx page

    <%@ Page Language="C#" MasterPageFile="~/Master Pages/FacultyMasterpage.master" AutoEventWireup="true" CodeFile="FacultyQuestion.aspx.cs" Inherits="Faculty_FacultyQuestion" Title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="Content10" Runat="Server">
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <br />
    <br />
    <asp:GridView ID="GridView1" runat="server" BackColor="White" 
        BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" 
        AllowPaging="True" AutoGenerateEditButton="True" 
        onrowcancelingedit="GridView1_RowCancelingEdit" 
        onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
        <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
        <RowStyle BackColor="White" ForeColor="#330099" />
        <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
    </asp:GridView>
    <br />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Content20" Runat="Server">
</asp:Content>

cs file coading

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;

public partial class Faculty_FacultyQuestion : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindData();
    }
    private void BindData()
    {
        DataTable dt = Class3.QuestionForFaculty(Convert.ToInt32(Session["rid"]));
        GridView1.DataSource = dt;
        GridView1.DataBind();

    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        e.Cancel = true;
        GridView1.EditIndex = -1;
        BindData();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        {

                //Retrieve the table from the session object.

            DataTable dt = Class3.QuestionForFaculty(Convert.ToInt32(Session["rid"]));
            GridView1.DataSource = dt;
            GridView1.DataBind();

                //Update the values.
                GridViewRow row = GridView1.Rows[e.RowIndex];

                dt.Rows[row.DataItemIndex]["ans"] = ((TextBox)(row.Cells[2].Controls[0])).Text;

                GridView1.EditIndex = -1;
               BindData();
        }
    }
}

class 3 coading

using System;
using System.Data;
using System.Configuration;
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;
using System.ComponentModel;

[DataObject]
/// <summary>
/// Summary description for Class3
/// </summary>
public class Class3
{

     private static string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
    [DataObjectMethod(DataObjectMethodType.Select)]
    public static DataTable getall()
    {
        SqlDataAdapter dp = new SqlDataAdapter("Select * from Question", conn);
        DataSet ds = new DataSet();
        dp.Fill(ds, "Question");
        return ds.Tables["Question"];
    }
    [DataObjectMethod(DataObjectMethodType.Delete)]
    public static int delete(int qid)
    {
        SqlConnection con = new SqlConnection(conn);
        SqlCommand cmd = new SqlCommand("Delete into Question qid = @a", con);
        cmd.Parameters.Add("@a", SqlDbType.VarChar).Value = qid;

        con.Open();
        int i = cmd.ExecuteNonQuery();
        con.Close();
        return(i);

    }
     [DataObjectMethod(DataObjectMethodType.Select)]
    public static DataTable QuestionForFaculty(int rid)
    {
        SqlDataAdapter dp = new SqlDataAdapter("SELECT Question.question, Question.ans,Question.qid, Subject.subjectname, Question.posteddate, Registration.name, Registration.role, Registration.rid FROM Question INNER JOIN Registration ON Question.rid = Registration.rid INNER JOIN Subject ON Question.sid = Subject.subjectid WHERE (Question.fid = '"+rid+"')", conn);
        DataSet ds = new DataSet();
        dp.Fill(ds, "Question");
        return ds.Tables["Question"];
    }


}

Best Answer

Why are you databinding the GridView at the start of the OnRowUpdating event? That right there is your problem! Just remove this:

GridView1.DataSource = dt;
GridView1.DataBind();

UPDATE: first add this to your Class3 (purely based on your sample code - not tested):

[DataObjectMethod(DataObjectMethodType.Update)]
public static int UpdateAnswer(int qid, string ans)
{
    var con = new SqlConnection(conn);
    var cmd = new SqlCommand("UPDATE Question SET ans = @ans WHERE qid = @qid", con);
    cmd.Parameters.Add("@qid", SqlDbType.VarChar).Value = qid;
    cmd.Parameters.Add("@ans", SqlDbType.VarChar).Value = ans;
    con.Open();
    int i = cmd.ExecuteNonQuery();
    con.Close();
    return(i);
}

Then change OnRowUpdating event to:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    Class3.UpdateAnswer(row.DataItemIndex, ((TextBox)row.Cells[2].Controls[0]).Text);
    GridView1.EditIndex = -1;
    BindData();
}
Related Topic