我的数据库项目的gridview和更新仍然存在问题

时间:2019-03-09 06:48:05

标签: c# asp.net database gridview sql-update

我想更新一个球员得分的进球数,因此,如果他为某个进球设定了进球数,我想对他的进球数进行更新... 我的代码中有一个错误,我不知道如何解决。有人可以帮我修复它吗?

我的代码:

        string connectionStr = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=|DataDirectory|\SoccerDataBase.mdb";
    using (OleDbConnection sqlCon = new OleDbConnection(connectionStr))
    {
        sqlCon.Open();
        string queryStr = "SELECT Achievement FROM SoccerAchievements WHERE UserID=@AchNums";
        OleDbCommand sqlCmd = new OleDbCommand(queryStr, sqlCon);
        sqlCmd.Parameters.AddWithValue("@AchNums", (SoccerTable.FooterRow.FindControl("AchNums") as TextBox).Text.Trim());
        OleDbDataAdapter dataAdapt = new OleDbDataAdapter(queryStr, sqlCon);
        DataSet ds = new DataSet();
        dataAdapt.Fill(ds, "SoccerAchievement");
        DataRow row = ds.Tables["SoccerAchievement"].Rows[0];
        int a = int.Parse(row[3].ToString());
        a = a + int.Parse("@AchNums");
        string query = "UPDATE SoccerAchievements SET Achievement= '" + a + "' WHERE UserID= @AchNums";
        sqlCmd.ExecuteNonQuery();
    }

我的GridView HTML代码:

<asp:GridView ID="SoccerTable" runat="server" AutoGenerateColumns="False" 
        BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" 
        CellPadding="3" DataSourceID="AccessDataSource1">
        <Columns>
            <asp:BoundField DataField="ID" HeaderText="קוד שחקן" InsertVisible="False" 
                SortExpression="ID" />
            <asp:BoundField DataField="Team" HeaderText="קבוצות" SortExpression="Team" />
            <asp:BoundField DataField="Players" HeaderText="שחקנים" 
                SortExpression="Players" />
                <asp:TemplateField HeaderText="הישגים">
                <FooterTemplate>
                <asp:TextBox ID="AchNums" runat="server"></asp:TextBox>
                </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                <ItemTemplate>
                 <asp:Button class = "AddButton" ID="AddButton" runat="server" onclick="AddButton_Click" Text="עדכן" />
                </ItemTemplate>
                </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="White" ForeColor="#000066" />
        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
        <RowStyle ForeColor="#000066" />
        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#007DBB" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#00547E" />
    </asp:GridView>

我的错误:位置0没有行。 图片:

enter image description here

我的数据足球成就

图片: enter image description here

请帮助我:)

1 个答案:

答案 0 :(得分:1)

替换此 OleDbDataAdapter dataAdapt = new OleDbDataAdapter(queryStr, sqlCon);OleDbDataAdapter dataAdapt = new OleDbDataAdapter(sqlCmd);

不过,您的int.parse代码可能会再次遇到错误。请更正该错误。