无法从前端应用程序将内容推送到数据库

时间:2016-06-27 06:08:29

标签: c# asp.net gridview

我在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>

1 个答案:

答案 0 :(得分:2)

您没有在aspx页面中提到过DataKeyNames。但是在RowUpdating中,您尝试访问Datakey的值[0]。这就是您收到此错误的原因。

解决方案: 指定&#34; DataKeyNames = ID&#34;在你的aspx页面中。

请参阅here以了解DataKey概念