public void DataList_UpdateCommand(Object sender, DataListCommandEventArgs e) { string strName = ((Label)e.Item.FindControl("lblName")).Text; int intChinese = Int32.Parse(((TextBox)e.Item.FindControl("tbChinese")).Text); int intMath = Int32.Parse(((TextBox)e.Item.FindControl("tbMath")).Text); int intEnglish = Int32.Parse(((TextBox)e.Item.FindControl("tbEnglish")).Text); //更新数据库中的数据 string strUpdate = "Update Score Set Chinese=@intChinese,Math=@intMath,English=@intEnglish Where Name=@strName"; SqlParameter[] paras = new SqlParameter[10]; paras[0] = new SqlParameter("@strName", SqlDbType.VarChar); paras[0].Value = strName; paras[1] = new SqlParameter("@intChinese", SqlDbType.Int); paras[1].Value = intChinese; paras[2] = new SqlParameter("@intMath", SqlDbType.Int); paras[2].Value = intMath; paras[3] = new SqlParameter("@intEnglish", SqlDbType.Int); paras[3].Value = intEnglish; bool temp = NewsDB.Getcmd(strUpdate, paras); score.EditItemIndex = -1; ListBind(); }
一直报错“SqlParameterCollection 只接受非空的 SqlParameter 类型对象”。
后来将
SqlParameter[] paras = new SqlParameter[10];
改成
SqlParameter[] paras = new SqlParameter[4];
以后解决错误。
是我从别的地方拷贝过来的,而不是自己写的。现在越来越发现拷贝代码虽然快,但是往往存在许多连自己都不易发现的错误。