title: How to Update Data in Database while Downloading a File in Asp (For counting number of times Files downloaded)
body: <p>I Just want to count how many times a file has been downloaded from my website. For that, first, i have used this code:</p>
  
  <pre><code> protected void Button1_Click(object sender, EventArgs e)
  {
  
  
      int id = (int)Session["idu"];
  
      byte[] bytes;
      string fileName, contentType, btype, date, visited;
      string constr = ConfigurationManager.ConnectionStrings["mycon"].ConnectionString;
      using (SqlConnection con = new SqlConnection(constr))
      {
          con.Open();
  
          using (SqlCommand c2 = new SqlCommand())
          {
              c2.Connection = con;
              c2.CommandTimeout = 1500;
              c2.CommandText = "UPDATE Publication SET downloads= downloads+1 where id='" + Session["idu"] + "'";
              // c2.ExecuteNonQuery();
              SqlDataReader s = c2.ExecuteReader();
              s.Read();
  
  
  
  
  
          } con.Close();
  
  
  
          using (SqlCommand cmd = new SqlCommand())
          {    
              cmd.CommandText = "select fname, data, ftype,btype,date,visited,downloads from Publication where id=@Id";
              cmd.Parameters.AddWithValue("@id", id);
              con.Open();
  
              cmd.Connection = con;
  
  
              using (SqlDataReader sdr = cmd.ExecuteReader())
              {
                  sdr.Read();
                  bytes = (byte[])sdr["data"];
                  contentType = sdr["ftype"].ToString();
                  fileName = sdr["fname"].ToString();
                  btype = sdr["btype"].ToString();
                  date = sdr["date"].ToString();
                  visited = sdr["visited"].ToString();
                 downloads = (int)sdr["downloads"];
  
  
              }
  
  
  
          }
  
  
  
  
          con.Close();
  
      }
      Response.Clear();
      Response.Buffer = true;
      Response.Charset = "";
      Response.Cache.SetCacheability(HttpCacheability.NoCache);
      Response.ContentType = contentType;
      Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
      Response.BinaryWrite(bytes);
  
      Response.Flush();
  
  
  
  
  
      Response.End();
  
  }
  </code></pre>
  
  <p>here Session["idu"] is the id value which were fetched from the linkbutton in gridview from the previous page. All i want that, when i hit Button1, first i want to update the value of downloads, then download the file. For this, i used CommandTimeout 1500 seconds to get enough time execute updating. This code is Working for Small files like size of 186 KB . But for large file, like 1 MB, I got Error of Time Expiration.</p>
  
  <p><a href="http://i.stack.imgur.com/tDAvS.png" rel="nofollow">enter image description here</a></p>
  
  <p>Why does it appear? How to get rid of this problem?</p>