在DataGrid1中更新数据时出错:(NID在表中是主键,还是标识种子,int类型,长度为4)Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.UpdateCommand
SqlUpdateCommand1.CommandText = "UPDATE table SET AAA= @AAA, BBB= @BBB WHERE NID = @NID" SqlUpdateCommand1.Parameters("@NId").Value = DataGrid1.DataKeys(CInt(e.Item.ItemIndex))
SqlUpdateCommand1.Parameters("@AAA").Value = CType(e.Item.Cells(2).Controls(0), TextBox).Text
SqlUpdateCommand1.Parameters("@BBB").Value = CType(e.Item.Cells(2).Controls(0), TextBox).Text SqlConnection1.Open()
SqlUpdateCommand1.ExecuteNonQuery()
SqlConnection1.Close()end sub*******运行时报错如下:::“/apptest”应用程序中的服务器错误。
--------------------------------------------------------------------------------此 SqlParameterCollection 中未包含带有 ParameterName“@NID”的 SqlParameter。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.IndexOutOfRangeException: 此 SqlParameterCollection 中未包含带有 ParameterName“@NID”的 SqlParameter。源错误:
......
行 216: SqlDeleteCommand1.Parameters("@NID").Value = DataGrid1.DataKeys(CInt(e.Item.ItemIndex))
.......
new SqlParameter("@NID", SqlDbType.BigInt,8),
new SqlParameter("@AAA", SqlDbType.VarChar,1000),
new SqlParameter("@BBB", SqlDbType.VarChar,1000)}; parameters[0].Value= DataGrid1.DataKeys(CInt(e.Item.ItemIndex))
parameters[0].Value = CType(e.Item.Cells(2).Controls(0), TextBox).Text
parameters[0].Value = CType(e.Item.Cells(2).Controls(0), TextBox).Text foreach (SqlParameter parm in SqlParameter)
{
SqlUpdateCommand1.Parameters.Add(parm);
}
SqlConnection1.Open()
SqlUpdateCommand1.ExecuteNonQuery()
SqlConnection1.Close()
//实例化
SqlParameter param = new SqlParameter("@NID", SqlDbType.BigInt,8); //赋值
param.Value = DataGrid1.DataKeys(CInt(e.Item.ItemIndex)); //加到集合中
SqlUpdateCommand1.Parameters.Add(param);
那我应该把实例化语句放在DataGrid1_UpdateCommand事件中就可以嘛??我查过相关的资料,,好像也放过你说的这个实例化的语句,,好像没成功...
SqlUpdateCommand1.Parameters.Add(Param) SqlUpdateCommand1.Parameters("@NID").Value = DataGrid1.DataKeys(CInt(e.Item.ItemIndex))'''''到这还是报错呀...索引超出范围。必须为非负值并小于集合大小。参数名: index END SUB
应该是SqlUpdateCommand1.Parameters("BBB").Value吧
应该是这句有错,它是null,取不到值
要么你先改为 SqlUpdateCommand1.Parameters("@NId").Value = DataGrid1.DataKeys(CInt(e.Item.ItemIndex))
SqlUpdateCommand1.Parameters("@AAA").Value = DataGrid1.DataKeys(CInt(e.Item.ItemIndex))
SqlUpdateCommand1.Parameters("@BBB").Value = DataGrid1.DataKeys(CInt(e.Item.ItemIndex))调试一下看看,是不是CType(e.Item.Cells(2).Controls(0), TextBox).Text这里的问题。