在這裏,我試圖更新下載計數的一個例如在數據庫中的dwncount列點擊按鈕btndownloadpost。上點擊按鈕它顯示輸入字符串的格式不正確使用命令參數在按鈕
「輸入字符串的不正確的格式」中斷線INT錯誤countid = Convert.ToInt32(e.CommandArgument.ToString());.
db中的countid和dwncount列是int數據類型。 以下是我使用的完整代碼。
按鈕,如下圖所示
<div>
<asp:Button ID="btndownloadpost" CommandName="download" CommandArgument='<%#Eval("countid") %>' CssClass="btn btn-info" runat="server" Text="Download " OnCommand="btndownloadpost_Command" />
</div>
代碼後面的按鈕
protected void btndownloadpost_Command(object sender, CommandEventArgs e)
{
if (e.CommandName== "download")
{
int countid = Convert.ToInt32(e.CommandArgument.ToString());
int i = c.updatedwncount(countid, Convert.ToInt32(ViewState["click"]));
if (i > 0)
{
if (ViewState["click"] != null)
{
count = (int)ViewState["click"] + 1;
}
ViewState["click"] = count;
Label18.Text = "Updated ";
}
}
}
方法使用
public int updatedwncount(int countid, int dwncount)
{
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["myconnection"].ConnectionString);
string sql = "update tbltry set dwncount=dwncount+1 where [email protected]";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@dwncount", dwncount);
cmd.Parameters.AddWithValue("@countid", countid);
try
{
con.Open();
return cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
是按鈕控件是在gridview內嗎? – navnit