我目前有一个Gridview,通过从下拉列表中选择一个值来填充。选择值后,将填充gridview,并且我尝试允许用户使用“RowUpdating”事件编辑gridview中的值。但是,我试图根据下拉列表中的值“StockTakeIDNew”更新Gridivew中的值。
这是我的c#代码。
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
using (SqlConnection sqlCon = new SqlConnection(@"Data Source=(local)\;Initial Catalog=SmallBatch;Integrated Security=True;"))
{
sqlCon.Open();
string query = "UPDATE Stock_Take_Item SET BarQuantity=@BarQuantity, StorageQuantity=@StorageQuantity WHERE StockTakeIDNew =@id";
SqlCommand sqlCmd = new SqlCommand(query, sqlCon);
sqlCmd.Parameters.AddWithValue("@BarQuantity", (GridView1.Rows[e.RowIndex].FindControl("barQuantityTxt") as TextBox).Text.Trim());
sqlCmd.Parameters.AddWithValue("@StorageQuantity", (GridView1.Rows[e.RowIndex].FindControl("storageQuantityTxt") as TextBox).Text.Trim());
sqlCmd.Parameters.AddWithValue("@id", Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()));
sqlCmd.ExecuteNonQuery();
String query1 = "SELECT Stock_Take.Username, Item.ItemID, Item.ItemDesc, Stock_Take_Item.BarQuantity, Stock_Take_Item.StorageQuantity, Stock_Take.StockTakeIDNew FROM Item INNER JOIN Stock_Take_Item ON Item.ItemID = Stock_Take_Item.ItemID INNER JOIN Stock_Take ON Stock_Take_Item.StockTakeIDNew = Stock_Take.StockTakeIDNew where Stock_Take.StockTakeIDNew = @DATE";
SqlConnection con = new SqlConnection(@"Data Source=(local)\;Initial Catalog=SmallBatch;Integrated Security=True;");
con.Open();
SqlCommand cmd = new SqlCommand(query1, con);
DdlUser.SelectedValue.GetType();
cmd.Parameters.Add("@DATE", SqlDbType.Int).Value = DdlUser.SelectedValue;
SqlDataReader reader = cmd.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
}
}
我正在使用此代码。我基本上不将@ID值作为.DataKeys,而是来自DDL的SelectedIndexChanged的值。
代码运行,除了我收到错误: 指数超出范围。必须是非负数且小于集合的大小。参数名称:index