Note that there are some explanatory texts on larger screens.

plurals
  1. POasp.net update gridview problem
    primarykey
    data
    text
    <p>I have a problem with handling the function to update my gridview. Until now I've been using DataSource control but this time I need to build everything from scratch. I want to be able to update just the very last data column of the gridview. My problems are: 1) When I click edit button all data fields in the row convert into textboxes - I would like only last one to do so 2) When I click update I get "Object reference not set to an instance of an object." error, and it seems that "TextBox tProgress = (TextBox)grdMilestones.Rows[e.RowIndex].Cells[3].FindControl("mstCompletition");" just can't find a correct value because when I substitute it with a number it works fine.</p> <p>This is my gridview</p> <pre><code>&lt;asp:GridView ID="grdMilestones" runat="server" Width="940px" DataKeyNames="mstNo" OnRowEditing="grdMilestones_RowEditing" OnRowCancelingEdit="grdMilestones_RowCancelingEdit" OnRowUpdating="grdMilestones_RowUpdating" AutoGenerateColumns="False" CssClass="milestonesGrid"&gt; &lt;Columns&gt; &lt;asp:BoundField DataField="proName" HeaderText="Project Name" /&gt; &lt;asp:BoundField DataField="mstDescription" HeaderText="Milestone Description" /&gt; &lt;asp:BoundField DataField="mstPersResp" HeaderText="Milestone Leader" /&gt; &lt;asp:BoundField DataField="mstCompletition" HeaderText="Progress" DataFormatString{0:F1}%" /&gt; &lt;asp:CommandField ButtonType="Button" ShowEditButton="true" EditText="Update progress" /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; </code></pre> <p>And a code behind</p> <pre><code>protected void grdMilestones_RowUpdating(object sender, GridViewUpdateEventArgs e) { int id = Int32.Parse(grdMilestones.DataKeys[e.RowIndex].Value.ToString()); TextBox tProgress = (TextBox)grdMilestones.Rows[e.RowIndex].Cells[3].FindControl("mstCompletition"); int prog = Convert.ToInt32(tProgress.Text.Trim()); //ds.Rows[row.DataItemIndex]["mstCompletition"] = ((TextBox)(row.Cells[1].Controls[0])).Text; string updateSQL = "UPDATE milestonesPM SET mstCompletition = @mstCompletition WHERE mstNo = @mstNo"; SqlConnection con = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand(updateSQL, con); cmd.Parameters.Add("@mstNo", SqlDbType.Int).Value = id; cmd.Parameters.Add("@mstCompletition", SqlDbType.SmallInt).Value = prog; con.Open(); cmd.ExecuteNonQuery(); grdMilestones.EditIndex = -1; grdMilestones.DataBind(); con.Close(); fillGrid(); } </code></pre> <p>Regards</p> <p>Bartosz</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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