C# 使用DataGrid编辑记录时ArgumentOutOfRange异常
你好。我正在使用asp.net C#进行编码,我很难使用gridview编辑保存在sql server数据库中的记录。这是我的密码C# 使用DataGrid编辑记录时ArgumentOutOfRange异常,c#,asp.net,sql-server-2008,gridview,C#,Asp.net,Sql Server 2008,Gridview,你好。我正在使用asp.net C#进行编码,我很难使用gridview编辑保存在sql server数据库中的记录。这是我的密码 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { camConnection.ConnectionString = SqlDataSource1.ConnectionString; camSqlDataAdapter.SelectComma
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
camConnection.ConnectionString = SqlDataSource1.ConnectionString;
camSqlDataAdapter.SelectCommand = new SqlCommand(SqlDataSource1.SelectCommand, camConnection);
camSqlDataAdapter.Fill(camDataSet);
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
TextBox pid1 = (TextBox)row.Cells[1].Controls[0];
TextBox pname1 = (TextBox)row.Cells[2].Controls[0];
TextBox pdesc1 = (TextBox)row.Cells[3].Controls[0];
TextBox pquan1 = (TextBox)row.Cells[4].Controls[0];
TextBox pprice1 = (TextBox)row.Cells[5].Controls[0];
GridView1.EditIndex = -1;
camConnection.Open();
SqlCommand cmd = new System.Data.SqlClient.SqlCommand("update productInfo set ProductName='" + Convert.ToString(pname1.Text) + "', Description='" + Convert.ToString(pdesc1.Text) + "', Quantity='" + Convert.ToString(pquan1.Text) + "', Price='" + Convert.ToString(pprice1.Text) + "' where ID='" + pid1.Text + "' ", camConnection);
cmd.ExecuteNonQuery();
camConnection.Close();
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
}
当我在gridview中执行代码并编辑记录,然后单击更新链接时,出现了一个错误,显示:
指定的参数超出了有效值的范围。
参数名称:索引
在Gridview中,可能有些字段为空。 所以当你写作时:
TextBox pid1 = (TextBox)row.Cells[1].Controls[0];
代码正在寻找一些值。如果row.Cells[1].Controls[0]
为空,则将显示超出范围的异常。
希望这有帮助。在Gridview中,可能有些字段为空。所以当你写作时:
TextBox pid1 = (TextBox)row.Cells[1].Controls[0];
TextBox pid1=(TextBox)行。单元格[1]。控件[0];
代码正在寻找一些值。如果row.Cells[1].Controls[0]为空,则将显示超出范围的异常。希望这能有所帮助。在哪一行发生错误,可能是
行中的一行。单元格[n]。控件[n]代码>?放置断点,单步执行代码,在web上搜索异常消息。