在将 varchar 值 'System.Web.UI.WebControls.TextBox' 转换成数据类型 int 时失败。
运行时出现这个错误“在将 varchar 值 'System.Web.UI.WebControls.TextBox' 转换成数据类型 int 时失败。”请高手帮忙
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{//获取配置文件中的数据库连接配置
}
protected void btInsert_Click(object sender, EventArgs e)
{//获取配置文件中的数据库连接配置
string myStr = ConfigurationManager.AppSettings["constr"].ToString();
//建立数据库连接
SqlConnection myconn = new SqlConnection(myStr);
//定义查询Sql语句
string sqlStr = "update NamePrice set Name='" + txtName + "',Price='" + txtPrice + "'where id=1";
//初始化查询命令
SqlCommand mycomm = new SqlCommand(sqlStr, myconn);
//打开数据库连接
if (myconn.State == System.Data.ConnectionState.Closed)
{ myconn.Open(); }
int records=Convert.ToInt32(mycomm.ExecuteNonQuery());
if (records > 0)
{ Response.Write("成功修改了" + mycomm.ExecuteNonQuery().ToString() + "条数据"); }
else
{ Response.Write("<script>alert('失败');</script>"); }
mycomm.Dispose();
myconn.Close();
}
}