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

c#页面程序中,向SQL SERVER 2008 安插记录总是产生Exception消息

2012-11-17 
c#页面程序中,向SQL SERVER 2008 插入记录总是产生Exception消息我的插入一句如下:insert into InModelDet

c#页面程序中,向SQL SERVER 2008 插入记录总是产生Exception消息
我的插入一句如下:
insert into InModelDetail(id,name,gender,passtype,passexpire,prepay,payair,chinaaddress,phone) values(3707,'','Male','1','333443',1000,123,'sdsds','57886')
该语句用SQL SERVER 2008 Management Studio 执行是成功的,但在.aspx.cs代码中总是报错:

列名或所提供值的数目与表定义不匹配。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.Data.SqlClient.SqlException: 列名或所提供值的数目与表定义不匹配。

源错误: 


行 107: if (pars != null)
行 108: cmd.Parameters.AddRange(pars);
行 109: return cmd.ExecuteNonQuery();
行 110: }
行 111: }
 

源文件: D:\Glenn\ERPs -FRAME4\ERP.DAL\DBHelper.cs 行: 109 

堆栈跟踪: 


[SqlException (0x80131904): 列名或所提供值的数目与表定义不匹配。]
  System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +212
  System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +245
  System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1099
  System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) +2858575
  System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +470
  System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +182
  ERP.DAL.DBHelper.GetExcuteNonQuery(String cmdTxt, SqlParameter[] pars) in D:\Glenn\ERPs -FRAME4\ERP.DAL\DBHelper.cs:109
  ERP.DAL.DBHelper.GetExcuteNonQuery(String cmdTxt) in D:\Glenn\ERPs -FRAME4\ERP.DAL\DBHelper.cs:95
  ERP.DAL.InModelService.InsertInModelDetail(InModelDetail inde) in D:\Glenn\ERPs -FRAME4\ERP.DAL\InModelService.cs:38
  ERP.BLL.InDeptModelsManager.InsertInModelDetail(InModelDetail inmodeldetail) in D:\Glenn\ERPs -FRAME4\ERP.BLL\InDeptModelsManager.cs:15
  Master_Default.btnClickOK_Click(Object sender, EventArgs e) in d:\Glenn\ERPs -FRAME4\WEB\InDepot.aspx.cs:121
  System.Web.UI.WebControls.Button.OnClick(EventArgs e) +115
  System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +140
  System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +29
  System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2981




--------------------------------------------
版本信息: Microsoft .NET Framework 版本:2.0.50727.5456; ASP.NET 版本:2.0.50727.5456 



源代码如下:
protected void btnClickOK_Click(object sender, EventArgs e)
  {
  int id = Convert.ToInt32(this.txtStoreID.Text.Trim());
  string StoreDate = this.txtStoreDate.Text;
  string StoreType = this.ddlStoreType.SelectedItem.Text.Trim();
  string StorePlace = this.ddlStorePlace.SelectedItem.Text;
  string Users = this.ddlUsers.SelectedItem.Text;
  string BuyDate = this.txtBuyDate.Text.Trim();
  string BuyDepartment = this.ddlDepartment.SelectedItem.Text;
  string BuyMan = this.txtBuyMan.Text.Trim();
  string Ratify = this.txtRatify.Text.Trim();
  string Remark = this.txtRemark.Text.Trim();
  string StoreName = this.txtStoreName.Text.Trim();//glenn
  dp.Id = id;
  dp.StoreDate = Convert.ToDateTime(StoreDate);
  dp.StoreType = StoreType;
  dp.StorePlace = StorePlace;
  dp.GoodsSign = Users;
  dp.BuyDate = Convert.ToDateTime(BuyDate);
  dp.Department = BuyDepartment;


  dp.BuyMan = BuyMan;
  dp.Ratify = Ratify;
  dp.Remark = Remark;
  dp.StoreName = StoreName;
  dm.InsertInDepot(dp);
  //Model----
  //int id = Convert.ToInt32(this.txtStoreID.Text.Trim());
  string name = this.txtStoreName.Text.Trim();
  string passtype = this.passtype.Text.Trim();
  string passexpire = this.passexpire.Text.Trim();
  string visastate = this.visastate.Text.Trim();
  int prepay = Convert.ToInt32(this.txtPay.Text.Trim());
  int payair = Convert.ToInt32(this.txtPayAir.Text.Trim());
  string chinaaddress = this.txtAddress.Text.Trim();
  string phone = this.txtPhone.Text.Trim();
  string expectdate = this.expectDate.Text.Trim();
  string actualdate = this.actualDate.Text.Trim();
  string arrivalstatus = this.arrivalStatus.Text.Trim();
  //InModelDetail dd;
  dd.Id = id;
  dd.Name = name;
  //dd.Gender = gender;
  dd.Passtype = passtype;
  dd.Passexpire = passexpire;
  dd.Visastate = visastate;
  dd.Prepay = prepay;
  dd.Payair = payair;
  dd.Chinaaddress = chinaaddress;
  dd.Phone = phone;
  dd.Expectdate = expectdate;
  dd.Actualdate = actualdate;
  dd.Arrivalstatus = arrivalstatus;
   
  //debug
  StringBuilder sb = new StringBuilder();
  sb.Append("insert into InModelDetail(id,name,gender,passtype,passexpire,prepay,payair,chinaaddress,phone) values(");
  sb.Append(dd.Id);
  //sb.Append(");");
  sb.Append(",'");
  sb.Append(dd.Name);
  sb.Append("','Male','");
  //sb.Append("','");
  sb.Append(dd.Passtype);
  sb.Append("','");
  sb.Append(dd.Passexpire);
  //sb.Append("','");
  //sb.Append(dd.Visastate);
  sb.Append("',");//'");
  sb.Append(dd.Prepay);
  sb.Append(",");
  sb.Append(dd.Payair);
  sb.Append(",'");
  sb.Append(dd.Chinaaddress);
  sb.Append("','");
  sb.Append(dd.Phone);
  sb.Append("')");
  // this.Response.Redirect("error.html?m=" + sb.ToString());
  //debug end
  InDeptModelsManager dmm = new InDeptModelsManager();
dmm.InsertInModelDetail(dd);

-----------------
以上代码,在执行dm.InsertInDepot(dp);时完全正确,但执行dmm.InsertInModelDetail(dd);
时报错,错误信息如上所示。

[解决办法]
错误应该是在
InsertInModelDetail方法里面,

dd对象的字段跟
InModelDetail(id,name,gender,passtype,passexpire,prepay,payair,chinaaddress,phone)

字段数量不一样啊
[解决办法]
说实话,这样拼接字符串,我看着头都晕
报错的意思很简单,插入数据的字段前后不一致。
[解决办法]
打开你的Sql2008》工具》SQL Server Profiler》选择好事件运行,然后运行你的程序页面,看看程序执行的SQL语句,看是哪错了,然后回头去程序里修改你的错误。
[解决办法]
sql语句出错了嘛,仔细调试下就行啦
[解决办法]
把SQL 语句拿出来 去 查询分析器 看下。学会调试.

热点排行