I have given the user the option to download images that are being uploaded by a provider. When the download button is clicked the image downloads. But I also want to store how many times that download button is clicked. For that I typed this code, but it isn't working.
I tried several ways like declaring an int variable and increment it. Didn't work. Then I tried this. Still not working. What am I doing wrong?
protected void btn_download_Click(object sender, EventArgs e)
{
var res = (byte[])Session["Image"];
Response.Clear();
MemoryStream ms = new MemoryStream(res);
Response.ContentType = "image/jpeg";
Response.AddHeader("content-disposition", "attachment;filename=Image.jpg");
Response.Buffer = true;
ms.WriteTo(Response.OutputStream);
Response.End();
try
{
SqlCommand command = new SqlCommand("UPDATE [ImageWithTags] SET DownloadCount = DownloadCount + 1 WHERE ImageID='" + DropDownList1.SelectedItem.Text + "'", con);
con.Open();
command.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
You can create a session variable for that as well and do the calculations like:
protected void btn_download_Click(object sender, EventArgs e)
{
int counter=0;
if(Session["counter"]!=null)
{
Session["counter"] = ++Convert.ToInt32(Session["counter"]);
counter = Convert.ToInt32Session["counter"];
}
else
Session["counter"] = 0;
// do your calculation here with variable counter
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.