<ItemTemplate>
<asp:Label ID="lbldescription" runat="server" width="175px" Text='<%#Eval("description") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtdescription" runat="server"></asp:TextBox>
</EditItemTemplate>
protected void gvManage_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string topicID = gvtopic.DataKeys[e.RowIndex].Values["topicID"].ToString();
TextBox description = (TextBox)gvtopic.Rows[e.RowIndex].FindControl("txtdescription");
string sql = "Update topic SET <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f692938595849f86829f9998cbb692938595849f86829f9998">[email protected]</a> WHERE topicID= @topicID";
var cmd = new MySqlCommand(sql, con);
con.Open();
cmd.Parameters.AddWithValue("@topicID",topicID);
cmd.Parameters.AddWithValue("@description", description);
cmd.ExecuteNonQuery();
con.Close();
gvtopic.EditIndex = -1;
GVbind();
}
I’m using the gridview to display the description and let users edit and update it. But the user’s input(they enter at textbox) won’t save to the database. No matter what the users type in the textbox, the text “System.Web.UI.WebControls.TextBox” will be saved.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The code is passing a TextBox instance as a parameter to the query, not the textbox’s contents. AddWithValue “helpfully” tries to convert any type it doesn’t understand into text by calling its ToString() method.
TextBox doesn’t overload ToString() though, so the default Object.ToString() method is used instead, which returns the type name, System.Web.UI.WebControls.TextBox.
To pass the contents, use TextBox.Text :
cmd.Parameters.AddWithValue("@description", description.Text);
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0