无法将内容从前端应用程序推送到数据库
本文关键字:应用程序 数据库 前端 | 更新日期: 2023-09-27 17:52:17
我在visual studio中开发了一个前端,用户可以使用它与数据库进行交互。唯一的问题是更新不能从gridview中工作。我已经尝试了多种方法来解决这个问题。我附上了有问题的表的图片和gridview的代码。有人能指出我的错误吗?
当点击更新按钮时出现错误:
索引超出范围。必须非负且小于集合的大小。参数名称:index
Database table for info read/write
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["ZS_CS_EVO_IntegrationConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT Id, Period_Id, Evo_StockLink, Evo_ItemCode, Evo_Description, UnitRate, MinRate, RateBeforeSevenDays, RateAfterSevenDays FROM rates"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
this.BindGrid();
}
protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
int Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
int Period_Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
int Evo_StockLink = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
string Evo_ItemCode = (row.Cells[3].Controls[0] as TextBox).Text;
string Evo_Description = (row.Cells[4].Controls[0] as TextBox).Text;
float UnitRate = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
float MinRate = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
float RateBeforeSevenDays = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
float RateAfterSevenDays = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
string constr = ConfigurationManager.ConnectionStrings["ZS_CS_EVO_IntegrationConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("UPDATE rates SET Period_Id = @Period_Id, Evo_StockLink = @Evo_StockLink, Evo_ItemCode = @Evo_ItemCode, Evo_Description = @Evo_Description, UnitRate = @UnitRate, MinRate = @MinRate, RateBeforeSevenDays = @RateBeforeSevenDays, RateAfterSevenDays = @RateAfterSevenDays WHERE Id = @Id"))
{
cmd.Parameters.AddWithValue("@Id", Id);
cmd.Parameters.AddWithValue("@Period_Id", Period_Id);
cmd.Parameters.AddWithValue("@Evo_StockLink", Evo_StockLink);
cmd.Parameters.AddWithValue("@Evo_ItemCode", Evo_ItemCode);
cmd.Parameters.AddWithValue("@Evo_Description", Evo_Description);
cmd.Parameters.AddWithValue("@UnitRate", UnitRate);
cmd.Parameters.AddWithValue("@MinRate", MinRate);
cmd.Parameters.AddWithValue("@RateBeforeSevenDays", RateBeforeSevenDays);
cmd.Parameters.AddWithValue("@RateAfterSevenDays", RateAfterSevenDays);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
GridView1.EditIndex = -1;
this.BindGrid();
}
ASPX:
<asp:GridView ID="GridView1" runat="server" OnRowEditing="OnRowEditing" OnRowCancelingEdit="OnRowCancelingEdit" OnRowUpdating="OnRowUpdating" AutoGenerateEditButton="True" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" PageSize="3">
<AlternatingRowStyle BackColor="#DCDCDC" />
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
<asp:SqlDataSource ID="RatesLink" runat="server"
ConnectionString="<%$ ConnectionStrings:ZS_CS_EVO_IntegrationConnectionString %>"
OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [rates]"
ConflictDetection="CompareAllValues" DeleteCommand="DELETE FROM [rates] WHERE [id] = @original_id AND (([Period_Id] = @original_Period_Id) OR ([Period_Id] IS NULL AND @original_Period_Id IS NULL)) AND [Evo_StockLink] = @original_Evo_StockLink AND [Evo_ItemCode] = @original_Evo_ItemCode AND (([Evo_Description] = @original_Evo_Description) OR ([Evo_Description] IS NULL AND @original_Evo_Description IS NULL)) AND [UnitRate] = @original_UnitRate AND [MinRate] = @original_MinRate AND [RateBeforeSevenDays] = @original_RateBeforeSevenDays AND [RateAfterSevenDays] = @original_RateAfterSevenDays" InsertCommand="INSERT INTO [rates] ([Period_Id], [Evo_StockLink], [Evo_ItemCode],
[Evo_Description], [UnitRate], [MinRate], [RateBeforeSevenDays], [RateAfterSevenDays]) VALUES (@Period_Id, @Evo_StockLink, @Evo_ItemCode, @Evo_Description, @UnitRate, @MinRate, @RateBeforeSevenDays, @RateAfterSevenDays)" UpdateCommand="UPDATE [rates] SET [Period_Id] = @Period_Id, [Evo_StockLink] = @Evo_StockLink,
[Evo_ItemCode] = @Evo_ItemCode, [Evo_Description] = @Evo_Description, [UnitRate] = @UnitRate, [MinRate] = @MinRate, [RateBeforeSevenDays] = @RateBeforeSevenDays, [RateAfterSevenDays] = @RateAfterSevenDays WHERE [id] = @original_id AND (([Period_Id] = @original_Period_Id) OR ([Period_Id] IS NULL AND @original_Period_Id IS NULL)) AND [Evo_StockLink] = @original_Evo_StockLink AND [Evo_ItemCode] = @original_Evo_ItemCode AND (([Evo_Description] = @original_Evo_Description) OR ([Evo_Description] IS NULL AND @original_Evo_Description IS NULL)) AND [UnitRate] = @original_UnitRate AND [MinRate] = @original_MinRate AND [RateBeforeSevenDays] = @original_RateBeforeSevenDays AND [RateAfterSevenDays] = @original_RateAfterSevenDays">
<DeleteParameters>
<asp:Parameter Name="original_id" Type="Int32" />
<asp:Parameter Name="original_Period_Id" Type="Int32" />
<asp:Parameter Name="original_Evo_StockLink" Type="Int32" />
<asp:Parameter Name="original_Evo_ItemCode" Type="String" />
<asp:Parameter Name="original_Evo_Description" Type="String" />
<asp:Parameter Name="original_UnitRate" Type="Double" />
<asp:Parameter Name="original_MinRate" Type="Double" />
<asp:Parameter Name="original_RateBeforeSevenDays" Type="Double" />
<asp:Parameter Name="original_RateAfterSevenDays" Type="Double" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Period_Id" Type="Int32" />
<asp:Parameter Name="Evo_StockLink" Type="Int32" />
<asp:Parameter Name="Evo_ItemCode" Type="String" />
<asp:Parameter Name="Evo_Description" Type="String" />
<asp:Parameter Name="UnitRate" Type="Double" />
<asp:Parameter Name="MinRate" Type="Double" />
<asp:Parameter Name="RateBeforeSevenDays" Type="Double" />
<asp:Parameter Name="RateAfterSevenDays" Type="Double" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Period_Id" Type="Int32" />
<asp:Parameter Name="Evo_StockLink" Type="Int32" />
<asp:Parameter Name="Evo_ItemCode" Type="String" />
<asp:Parameter Name="Evo_Description" Type="String" />
<asp:Parameter Name="UnitRate" Type="Double" />
<asp:Parameter Name="MinRate" Type="Double" />
<asp:Parameter Name="RateBeforeSevenDays" Type="Double" />
<asp:Parameter Name="RateAfterSevenDays" Type="Double" />
<asp:Parameter Name="original_id" Type="Int32" />
<asp:Parameter Name="original_Period_Id" Type="Int32" />
<asp:Parameter Name="original_Evo_StockLink" Type="Int32" />
<asp:Parameter Name="original_Evo_ItemCode" Type="String" />
<asp:Parameter Name="original_Evo_Description" Type="String" />
<asp:Parameter Name="original_UnitRate" Type="Double" />
<asp:Parameter Name="original_MinRate" Type="Double" />
<asp:Parameter Name="original_RateBeforeSevenDays" Type="Double" />
<asp:Parameter Name="original_RateAfterSevenDays" Type="Double" />
</UpdateParameters>
</asp:SqlDataSource>
您没有在aspx页面中提到DataKeyNames。然而,在RowUpdating中,您试图访问Datakey的值[0]。这就是为什么你会得到这个错误。
解决方案:在aspx页面中指定"DataKeyNames=ID"
请参阅此处了解DataKey概念