在将 varchar 值 'System.Web.UI.WebControls.TextBox' 转换成数据类型 int 时失败。

Fly_ana_Sky 2012-05-22 08:55:33
运行时出现这个错误“在将 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();
}
}
...全文
186 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
EnForGrass 2012-05-22
  • 打赏
  • 举报
回复
注意Name是关键字,加上[],该有空格的地方留空格,你这个主要错误是用控件ID去赋值,改成如下

//定义查询Sql语句
string sqlStr = "update NamePrice set [Name]='" + txtName.Text.Trim() + "',Price='" + Convert.ToInt32(txtPrice.Text.Trim()) + "' where id=1";
bdmh 2012-05-22
  • 打赏
  • 举报
回复
string sqlStr = "update NamePrice set Name='" + txtName.Text + "',Price='" + txtPrice.Text + "'where id=1";
theillusion 2012-05-22
  • 打赏
  • 举报
回复
改成这样试试

string sqlStr = "update NamePrice set Name='" + txtName.Text + "',Price='" + txtPrice.Text + "'where id=1";

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧