我的UPDATE功能有问题,我需要你的帮助。
我创建了一个表Computers(COM_ID,Company,Price,Model,Description,CAT_ID,Image,Quantity)
我有一个带有此代码的网络服务(AdminCentral.asmx)
[WebMethod(Description = "Updates a computer in the computer table", EnableSession = false)]
public string updateItem(string comid, string company, double price, string model, string description, string image, int CAT_ID, int quantity)
{
try
{
dbConn = new DbConnection();
SqlConnection conn = dbConn.OpenConnection();
SqlCommand updateItem = new SqlCommand("UpdateComputer", conn);
updateItem.CommandType = CommandType.StoredProcedure;
SqlParameter updatecomid = updateItem.Parameters.Add("@COM_ID", SqlDbType.Char, 15);
updatecomid.Value = comid;
SqlParameter updateCompany = updateItem.Parameters.Add("@Company", SqlDbType.Char, 90);
updateCompany.Value = company;
SqlParameter updatePrice = updateItem.Parameters.Add("@Price", SqlDbType.Money, 8);
updatePrice.Value = price;
SqlParameter updateModel = updateItem.Parameters.Add("@Model", SqlDbType.Char, 150);
updateModel.Value = model;
SqlParameter updateDescription = updateItem.Parameters.Add("@Description", SqlDbType.Char, 255);
updateDescription.Value = description;
SqlParameter updateImage = updateItem.Parameters.Add("@Image", SqlDbType.Char, 50);
updateImage.Value = image;
SqlParameter updateCatId = updateItem.Parameters.Add("@CAT_ID", SqlDbType.Int, 2);
updateCatId.Value = CAT_ID;
SqlParameter updateQuantity = updateItem.Parameters.Add("@Quantity", SqlDbType.Int, 2);
updateQuantity.Value = quantity;
return this.ExecuteQuery(updateItem);
}
catch (Exception e)
{
return e.ToString();
}
}
在另一个应用程序中,我创建了Web引用,并且我有这个Datagrid
<asp:datagrid id="Computerchange" runat="server" AllowPaging="True" PageSize="2" AutoGenerateColumns="False" BorderColor="Gainsboro" Height="500px"
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" HeaderText="Admin Functions" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" HeaderText="Delete" CommandName="Delete"></asp:ButtonColumn>
<asp:BoundColumn DataField="com_id" ReadOnly="True" HeaderText="Computer Number"></asp:BoundColumn>
<asp:BoundColumn DataField="company" HeaderText="Company"></asp:BoundColumn>
<asp:BoundColumn DataField="price" HeaderText="Price"></asp:BoundColumn>
<asp:BoundColumn DataField="model" HeaderText="Model"></asp:BoundColumn>
<asp:BoundColumn DataField="description" HeaderText="Description"></asp:BoundColumn>
<asp:BoundColumn DataField="id" ReadOnly="True" HeaderText="Category"></asp:BoundColumn>
<asp:BoundColumn DataField="imgSrc" HeaderText="Image"></asp:BoundColumn>
<asp:BoundColumn DataField="quantity" ReadOnly="True" HeaderText="Quantity"></asp:BoundColumn>
</Columns>
</asp:datagrid>
我写了这个函数
private void Computerchange_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//Store updated column values in local variables:
string updateCOM_ID = e.Item.Cells[2].Text;
string updateCompany = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
double updatePrice = double.Parse(((TextBox)e.Item.Cells[4].Controls[0]).Text);
string updateModel = ((TextBox)e.Item.Cells[5].Controls[0]).Text;
string updateDescription = ((TextBox)e.Item.Cells[6].Controls[0]).Text;
int updateCategoryId = int.Parse(e.Item.Cells[7].Text);
string updateImage = ((TextBox)e.Item.Cells[8].Controls[0]).Text;
int updateQuantity = int.Parse(e.Item.Cells[9].Text);
newView.RowFilter = "com_id='" + updateCOM_ID + "'";
if (newView.Count > 0)
{
//Delete the row that is being updated
newView.Delete(0);
}
newView.RowFilter = "";
//Create a new DataRow and populate it with the new data.
DataRow Row = Table.NewRow();
Row["com_id"] = updateCOM_ID;
Row["company"] = updateCompany;
Row["price"] = updatePrice;
Row["model"] = updateModel;
Row["description"] = updateDescription;
Row["id"] = updateCategoryId;
Row["imgSrc"] = updateImage;
Row["quantity"] = updateQuantity;
//Insert the new DataRow:
Table.Rows.Add(Row);
Computerchange.EditItemIndex = -1;
Computerchange.DataSource = newView;
Computerchange.DataBind();
// Now update the database with the new data
adminCentral1.adminCentral newData = new adminCentral1.adminCentral();
string results;
results = newData.updateItem(updateCOM_ID, updateCompany, updatePrice, updateModel, updateDescription, updateImage, updateCategoryId, updateQuantity);
if (results == "Success")
{
errorLabel.Text = "Computer Updated to database!";
}
else
{
errorLabel.Text = results;
}
当我点击“更新”按钮时,我遇到的错误是
"**Specified argument was out of the range of valid values.
Parameter name: index**"
Stack Trace:
[ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index]
System.Web.UI.ControlCollection.get_Item(Int32 index) +8750274
AdminMainPage.Computerchange_UpdateCommand(Object source, DataGridCommandEventArgs e) +626
System.Web.UI.WebControls.DataGrid.OnUpdateCommand(DataGridCommandEventArgs e) +115
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e) +498
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e) +121
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +125
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +169
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +9
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +176
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
你可以帮我解决这个错误吗?我想提一下,首先,我没有数量列,代码正常。当我插入数量列时,我有这个错误。我认为我正在尝试访问未在集合中创建或不存在的控件。
答案 0 :(得分:2)
Specified argument was out of the range of valid values."Parameter name: index*
表示您尝试访问其范围之外的集合
堆栈跟踪的前两行是
System.Web.UI.ControlCollection.get_Item(Int32 index) +8750274
AdminMainPage.Computerchange_UpdateCommand(Object source, DataGridCommandEventArgs e) +
这意味着在方法Computerchange_UpdateCommand
中,您通过不存在的索引访问Control Collection。
不幸的是,你多次引用控件,以下任何一个都可能是问题
string updateCOM_ID = e.Item.Cells[2].Text;
string updateCompany = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
double updatePrice = double.Parse(((TextBox)e.Item.Cells[4].Controls[0]).Text);
string updateModel = ((TextBox)e.Item.Cells[5].Controls[0]).Text;
string updateDescription = ((TextBox)e.Item.Cells[6].Controls[0]).Text;
int updateCategoryId = int.Parse(e.Item.Cells[7].Text);
string updateImage = ((TextBox)e.Item.Cells[8].Controls[0]).Text;
int updateQuantity = int.Parse(e.Item.Cells[9].Text);
然而,这是最有可能的候选人。
int updateQuantity = int.Parse(e.Item.Cells[9].Text);
我建议你在第一个断点上加一个断点,然后逐步查看哪一个失败了。
答案 1 :(得分:0)
这是需要阅读的大量信息......您可以采取一些措施来缩小范围:
1)确保您的索引不在范围之外。通常,索引不能小于0。 2)通过调试逐步执行。在调试时,跟踪索引变量的值。 3)手动运行存储过程(通过SSMS)
答案 2 :(得分:0)
我看不到的一条有用的信息是哪一行代码实际上是在抛出异常。由于在添加数量后出现异常,我建议在这行代码上设置一个断点。
int updateQuantity = int.Parse(e.Item.Cells[9].Text);
当它到达断点时,检查这是否是引发异常的行。如果你知道你的Cell [9]实际上并不存在,你就清楚地知道需要修复什么。