当前位置: 代码迷 >> ASP.NET >> 求教在将 varchar 值转换成数据类型 int 时失败,该怎么处理
  详细解决方案

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

热度:3157   发布时间:2013-02-25 00:00:00.0
求教在将 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();
  相关解决方案