尝试使用我的访问数据库更新我的GridView的总数有问题[重复]

问题描述 投票:-1回答:1

我想更新一个玩家得分的目标数,所以如果他打进一球,我想对他的进球数进行更新......我的代码中出现错误我不知道如何修复它。有人可以帮我解决吗,拜托?

My Asp.net Code:

    protected void SoccerTable_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    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 @id";
        OleDbCommand sqlCmd = new OleDbCommand(queryStr, sqlCon);
        sqlCmd.Parameters.AddWithValue("@id", Convert.ToInt32(SoccerTable.DataKeys[e.RowIndex].Value.ToString()));
        sqlCmd.Parameters.AddWithValue("@NumOfGoals", (SoccerTable.Rows[e.RowIndex].FindControl("AchNums") as DropDownList).Text.Trim());
        OleDbDataAdapter dataAdapt = new OleDbDataAdapter(sqlCmd);
        DataSet ds = new DataSet();
        dataAdapt.Fill(ds, "SoccerAchievement");
        DataRow row = ds.Tables["SoccerAchievement"].Rows[0];
        int a = int.Parse("@NumOfGoals");
        string query = "UPDATE SoccerAchievements SET Achievement= '" + a + "' WHERE @id";
        sqlCmd.ExecuteNonQuery();
    }
}

My GridView Code:

 <asp:GridView ID="SoccerTable" runat="server" AutoGenerateColumns="False" 
      ShowFooter="True" DataKeyNames="ID"
            ShowHeaderWhenEmpty="True"
            BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" 
            CellPadding="3" DataSourceID="AccessDataSource1"
            OnRowUpdating="SoccerTable_RowUpdating">
    <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="הישגים">
            <ItemTemplate>
                <asp:DropDownList ID="AchNums" runat="server"> 
            <asp:ListItem>1</asp:ListItem>
            <asp:ListItem>2</asp:ListItem>
            <asp:ListItem>3</asp:ListItem>
            <asp:ListItem>4</asp:ListItem>
            <asp:ListItem>5</asp:ListItem>
            <asp:ListItem>6</asp:ListItem>
            <asp:ListItem>7</asp:ListItem>
            <asp:ListItem>8</asp:ListItem>
            <asp:ListItem>9</asp:ListItem>
            <asp:ListItem>10</asp:ListItem>
             </asp:DropDownList>
            </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
            <ItemTemplate>
           <asp:ImageButton ID="ImageButton3" ImageUrl="Pictures/AdminToolsPics/save.png" runat="server" CommandName="Update" ToolTip="Update" Width="20px" Height="20px" />
            </ItemTemplate>
            </asp:TemplateField>
        </Columns>

我的错误:输入同一植物女儿的字符串。 enter image description here

请帮帮我们:)

c# asp.net database gridview sql-update
1个回答
1
投票

代码int a = int.Parse("@NumOfGoals");永远不会工作,因为函数int.Parse()尝试将字符串转换为整数。 Sinde字符串@NumOfGoals不是一个字符串,该函数将抛出异常。

© www.soinside.com 2019 - 2024. All rights reserved.