解决异常“SqlParameterCollection 只接受非空的 SqlParameter 类型对象。”

  1. public void DataList_UpdateCommand(Object sender, DataListCommandEventArgs e)  
  2.  {  
  3.      string strName = ((Label)e.Item.FindControl("lblName")).Text;  
  4.      int intChinese = Int32.Parse(((TextBox)e.Item.FindControl("tbChinese")).Text);  
  5.      int intMath = Int32.Parse(((TextBox)e.Item.FindControl("tbMath")).Text);  
  6.      int intEnglish = Int32.Parse(((TextBox)e.Item.FindControl("tbEnglish")).Text);  
  7.      //更新数据库中的数据  
  8.      string strUpdate = "Update Score Set Chinese=@intChinese,Math=@intMath,English=@intEnglish Where Name=@strName";  
  9.      SqlParameter[] paras = new SqlParameter[10];  
  10.      paras[0] = new SqlParameter("@strName", SqlDbType.VarChar);  
  11.      paras[0].Value = strName;  
  12.   
  13.      paras[1] = new SqlParameter("@intChinese", SqlDbType.Int);  
  14.      paras[1].Value = intChinese;  
  15.   
  16.      paras[2] = new SqlParameter("@intMath", SqlDbType.Int);  
  17.      paras[2].Value = intMath;  
  18.   
  19.      paras[3] = new SqlParameter("@intEnglish", SqlDbType.Int);  
  20.      paras[3].Value = intEnglish;  
  21.   
  22.      bool temp = NewsDB.Getcmd(strUpdate, paras);  
  23.      score.EditItemIndex = -1;  
  24.      ListBind();  
  25.  }  

一直报错“SqlParameterCollection 只接受非空的 SqlParameter 类型对象”。

后来将

  1. SqlParameter[] paras = new SqlParameter[10];  

改成

  1. SqlParameter[] paras = new SqlParameter[4];  

以后解决错误。

  1. SqlParameter[] paras = new SqlParameter[10];  

是我从别的地方拷贝过来的,而不是自己写的。现在越来越发现拷贝代码虽然快,但是往往存在许多连自己都不易发现的错误。





本文转自xwdreamer博客园博客,原文链接:http://www.cnblogs.com/xwdreamer/archive/2010/03/18/2297154.html,如需转载请自行联系原作者




上一篇:git如何回滚远程仓库


下一篇:京东搜索排序在线学习的 Flink 优化实践