【问题标题】:The users input can't save to database用户输入无法保存到数据库
【发布时间】:2021-01-29 16:39:53
【问题描述】:
<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 description=@description 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();
    }

我正在使用 gridview 来显示描述并让用户编辑和更新它。但是用户的输入(他们在文本框中输入)不会保存到数据库中。无论用户在文本框中输入什么内容,都会保存文本“System.Web.UI.WebControls.TextBox”。

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    代码将TextBox 实例作为参数传递给查询,而不是文本框的内容。 AddWithValue "helpfully" 尝试通过调用 ToString() 方法将任何它不理解的类型转换为文本。

    TextBox 不会重载ToString(),因此使用默认的Object.ToString() 方法,它返回类型名称System.Web.UI.WebControls.TextBox

    要传递内容,请使用TextBox.Text

    cmd.Parameters.AddWithValue("@description", description.Text);
    

    【讨论】:

      猜你喜欢
      • 2017-05-21
      • 2015-04-25
      • 2013-04-08
      • 2020-05-12
      • 2014-06-22
      • 1970-01-01
      • 2015-05-26
      • 2022-01-22
      • 1970-01-01
      相关资源
      最近更新 更多