异常详细信息: System.Data.SqlClient.SqlException: 在将 varchar 值 'zhangsan' 转换成数据类型 int 时失败。源错误:
行 92: string sql = "insert NewsRe 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='ReList.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 NewsRe values(" + b + ",'" + a + "','" + this.txContent.Value.Trim() + "','" + DateTime.Now + "')";
SqlCommand com = new SqlCommand(sql, con);
com.ExecuteNonQuery();
Response.Write("<script>alert('发布成功!!');window.location.href='ReList.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 NewsRe values(" + b + ",'" + a + "','" + this.txContent.Value.Trim() + "','" + DateTime.Now + "')";
SqlCommand com = new SqlCommand(sql, con);
com.ExecuteNonQuery();
Response.Write("<script>alert('发布成功!!');window.location.href='ReList.aspx?NewsID=" + b + "';</script>");
con.Close();
}
}
行 92: string sql = "insert NewsRe 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='ReList.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 NewsRe values(" + b + ",'" + a + "','" + this.txContent.Value.Trim() + "','" + DateTime.Now + "')";
SqlCommand com = new SqlCommand(sql, con);
com.ExecuteNonQuery();
Response.Write("<script>alert('发布成功!!');window.location.href='ReList.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 NewsRe values(" + b + ",'" + a + "','" + this.txContent.Value.Trim() + "','" + DateTime.Now + "')";
SqlCommand com = new SqlCommand(sql, con);
com.ExecuteNonQuery();
Response.Write("<script>alert('发布成功!!');window.location.href='ReList.aspx?NewsID=" + b + "';</script>");
con.Close();
}
}
你看下你的表有几列this.txContent.Value.Trim()
一般就是这里有问题sql数据类型转换的话用cast(nvarchar as int)
你把你SQL语句要插入的字段在前面一一列出来
要插入的值 跟表字段值不一致 你插入的是STRING 而里头是INT
insert into NewRe(field1,field2,...)values()
这一句,你看一下NewsRe是否有多一个int类型的。结果将字符a插入到int类型中了。导致出现上述错误仔细检查一下表结构应该就能解决