Note that there are some explanatory texts on larger screens.

plurals
  1. POGridView RowUpdating returns old values after PostBack
    text
    copied!<p>I got a problem with my GridView. When I try to edit my GridView, I only get the old values in return.</p> <p>Here's the RowUpdating event:</p> <pre><code>protected void grid_RowUpdating(object sender, GridViewUpdateEventArgs e) { GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex]; TextBox nVorname = (TextBox)row.FindControl("newVorname"); TextBox nNachname = (TextBox)row.FindControl("newNachname"); TextBox nTelnr = (TextBox)row.FindControl("newTelnr"); TextBox nEmail = (TextBox)row.FindControl("newEmail"); HiddenField tid = (HiddenField)row.FindControl("id"); grid.EditIndex = -1; SqlConnection sqlConn = new SqlConnection("server=localhost;Integrated Security=true;database=Telefonbuch;"); sqlConn.Open(); SqlCommand cmd = new SqlCommand("update dasOertliche set vorname= @vorname, nachname=@nachname, telefonnr =@telnr, email =@email where id = @id", sqlConn); cmd.Parameters.Add("@vorname", SqlDbType.VarChar); cmd.Parameters["@vorname"].Value = nVorname; cmd.Parameters.Add("@nachname", SqlDbType.VarChar); cmd.Parameters["@nachname"].Value = nNachname.Text; cmd.Parameters.Add("@email", SqlDbType.VarChar); cmd.Parameters["@email"].Value = nEmail.Text; cmd.Parameters.Add("@telnr", SqlDbType.VarChar); cmd.Parameters["@telnr"].Value = nTelnr.Text; cmd.Parameters.Add("@id", SqlDbType.Int); cmd.Parameters["@id"].Value = tid.Value; cmd.ExecuteNonQuery(); sqlConn.Close(); bind(); } </code></pre> <p>The TemplateField from the .aspx:</p> <pre><code> &lt;Columns&gt; &lt;asp:TemplateField HeaderText = "Vorname"&gt; &lt;ItemTemplate&gt; &lt;%#Eval ("vorname") %&gt;&lt;/ItemTemplate&gt; &lt;EditItemTemplate&gt; &lt;asp:TextBox ID="newVorname" runat="server" Text='&lt;%#Eval ("vorname") %&gt;'&gt; &lt;/asp:TextBox&gt; &lt;/EditItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/columns&gt; </code></pre> <p>And my GridView code:</p> <pre><code>&lt;asp:GridView runat="server" ID="grid" BorderWidth="0px" CellPadding="10" CellSpacing="10" HorizontalAlign="Center" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" onrowcancelingedit="grid_RowCancelingEdit" onrowediting="grid_RowEditing" onrowupdating="grid_RowUpdating" AutoGenerateColumns="False"&gt; </code></pre> <p>Like I said, it always returns the old values. Any suggestions?</p> <p>My Page_Load:</p> <pre><code>protected void Page_Load(object sender, EventArgs e) { bind(); if (!Page.IsPostBack) { bind(); } } </code></pre> <p>My bind():</p> <pre><code> public void bind() { SqlConnection sqlConn = new SqlConnection("server=localhost;Integrated Security=true;database=Telefonbuch;"); sqlConn.Open(); SqlDataAdapter sqlComm = new SqlDataAdapter("SELECT id, vorname AS 'Vorname', nachname AS 'Nachname', telefonnr, email AS 'E-Mail' FROM dasOertliche ORDER BY nachname ASC", sqlConn); DataSet ds = new DataSet(); sqlComm.Fill(ds, "dasOertliche"); grid.DataSource = ds.Tables[0]; grid.DataBind(); sqlConn.Close(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload