我想更新一个玩家得分的目标数,所以如果他打进一球,我想对他的进球数进行更新......我的代码中出现错误我不知道如何修复它。有人可以帮我解决吗,拜托?
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();
}
}
<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>
请帮帮我们:)
代码int a = int.Parse("@NumOfGoals");
永远不会工作,因为函数int.Parse()
尝试将字符串转换为整数。 Sinde字符串@NumOfGoals
不是一个字符串,该函数将抛出异常。