首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

求教在将 varchar 值转换成数据类型 int 时失败,该怎么处理

2012-10-13 
求教在将 varchar 值转换成数据类型 int 时失败异常详细信息: System.Data.SqlClient.SqlException: 在将

求教在将 varchar 值转换成数据类型 int 时失败
异常详细信息: System.Data.SqlClient.SqlException: 在将 varchar 值 'zhangsan' 转换成数据类型 int 时失败。

源错误: 


行 92: string sql = "insert NewsRemark values(" + b + ",'" + a + "','" + this.txContent.Value.Trim() + "','" + DateTime.Now + "')";
行 93: SqlCommand com = new SqlCommand(sql, con);
行 94: com.ExecuteNonQuery();
行 95: Response.Write("<script>alert('发布成功!!');window.location.href='RemarkList.aspx?NewsID=" + b + "';</script>");
行 96: con.Close();
 

源文件: e:\新建文件夹\OATest\UserWork\NewsView.aspx.cs 行: 94 

堆栈跟踪: 


[SqlException (0x80131904): 在将 varchar 值 'zhangsan' 转换成数据类型 int 时失败。]
  System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +1951450
  System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +4849003
  System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +194
  System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2394
  System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) +192
  System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +317
  System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +137
  UserWork_NewsView.bnSave_ServerClick(Object sender, EventArgs e) in e:\新建文件夹\OATest\UserWork\NewsView.aspx.cs:94
  System.Web.UI.HtmlControls.HtmlInputButton.OnServerClick(EventArgs e) +111
  System.Web.UI.HtmlControls.HtmlInputButton.RaisePostBackEvent(String eventArgument) +109
  System.Web.UI.HtmlControls.HtmlInputButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
  System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
  System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
  System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

 
源代码如下
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class UserWork_NewsView : System.Web.UI.Page
{
  public string Titles;
  public string TypeId;
  public string Contents;
  public string names;
  public string Pubdate;
  protected void Page_Load(object sender, EventArgs e)
  {

  if (!IsPostBack)
  {
  names = useram();
  TypeId = TypeName();
  int NewsID = Convert.ToInt32(Request["NewsID"].ToString());
  SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
  con.Open();
  string sql = "select * from News where NewsID="+NewsID+" ";
  SqlCommand com = new SqlCommand(sql,con);
  SqlDataReader dr = com.ExecuteReader();
  if (dr.Read())
  {
  Titles = dr["title"].ToString();
  Contents = dr["Content"].ToString();
  Pubdate=dr["Pubdate"].ToString();


  }
  con.Close();
  }
  }
  public string useram()
  {
  string Re="";
  int NewsID = Convert.ToInt32(Request["NewsID"].ToString());
  SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
  con.Open();
  string sql = "select Name from Employee where Username=(select username from News where NewsID=" + NewsID + ")";
  SqlCommand com = new SqlCommand(sql,con);
  SqlDataReader dr = com.ExecuteReader();
  if (dr.Read())
  {
  Re = dr["Name"].ToString();
  }
  return Re;

  }
  public string TypeName()
  {


  string Re="";
  int NewsID = Convert.ToInt32(Request["NewsID"].ToString());
  SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
  con.Open();
  string sql = "select type from NewsType where NTID=(select TypeId from news where NewsID=" + NewsID + ")";
  SqlCommand com = new SqlCommand(sql,con);
  SqlDataReader dr = com.ExecuteReader();
  dr.Read();
  return Re = dr["type"].ToString();

  }
  protected void bnSave_ServerClick(object sender, EventArgs e)
  {
  if (CheckBox1.Checked == true)
  {
  int b = Convert.ToInt32(Request["NewsID"].ToString());
  string a = "署名";
  SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
  con.Open();
  string sql = "insert NewsRemark values(" + b + ",'" + a + "','" + this.txContent.Value.Trim() + "','" + DateTime.Now + "')";
  SqlCommand com = new SqlCommand(sql, con);
  com.ExecuteNonQuery();
  Response.Write("<script>alert('发布成功!!');window.location.href='RemarkList.aspx?NewsID=" + b + "';</script>");
  con.Close();
  }
  else
  {
  int b = Convert.ToInt32(Request["NewsID"].ToString());
  string a = Session["UserName"].ToString();
  SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
  con.Open();
  string sql = "insert NewsRemark values(" + b + ",'" + a + "','" + this.txContent.Value.Trim() + "','" + DateTime.Now + "')";
  SqlCommand com = new SqlCommand(sql, con);
  com.ExecuteNonQuery();
  Response.Write("<script>alert('发布成功!!');window.location.href='RemarkList.aspx?NewsID=" + b + "';</script>");
  con.Close();
  }
 
  }

[解决办法]
string sql = "insert NewsRemark values(" + b + ",'" + a + "','" + this.txContent.Value.Trim() + "','" + DateTime.Now + "')";
你把你SQL语句要插入的字段在前面一一列出来
要插入的值 跟表字段值不一致 你插入的是STRING 而里头是INT
insert into NewRemark(field1,field2,...)values()
[解决办法]
string sql = "insert NewsRemark values(" + b + ",'" + a + "','" + this.txContent.Value.Trim() + "','" + DateTime.Now + "')";



强烈建议在插入的时候养成好的习惯,insert 表名后要跟上列名,这样对于你这样的错误可以迅速知道错误在哪,很明显你的a对应的NewsRemark的第二个列类型是int,而你的a是string类型

热点排行