Click here to Skip to main content
15,917,473 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
In my project the facility to download some file to the user but there is only the 3 times any user download same file, for that i m using one counter. So when user download file the counter is decremented by 1 & when counter reaches to zero the download of these particular file is not allowed to the user.for these I use gridview
all are well performed counter is decremented,but the problem is that the decremented counter is not shown in grid.so, how to show it.
My Code is-
XML
<asp:GridView ID="gdvOrderDetails" runat="server" AutoGenerateColumns="False"
        Width="977px" onrowdatabound="gdvOrderDetails_RowDataBound">
        <Columns>
            <asp:TemplateField HeaderText="Serial No">
                <ItemTemplate>
                    <asp:Label ID="gdvlblserialno" runat="server"></asp:Label>
                    <asp:Label ID="lblProductType" Visible="false" Text='<%# Eval("ProductType") %>' runat="server"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Date">
                <ItemTemplate>
                    <asp:Label ID="gdvlbldate" runat="server"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Themes">
                <ItemTemplate>
                    <asp:ImageButton ID="gdvimgbtndownload" runat="server"
                        ImageUrl="../_img/icons/download-now-short.png"
                        CommandArgument='<%# Eval("ProductId") %>' onclick="gdvimgbtndownload_Click"/>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Downloads Remaining">
                <ItemTemplate>

                    <asp:Label ID="gdvlbldownloadcount" runat="server"></asp:Label>

                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Downloads Valid Till Date">
                <ItemTemplate>
                    <asp:Label ID="gdvlbldwnvalidtildt" runat="server"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

& the code for download
C#
 public bool identify(string filenm)
        {
            //string filePath = "D:\\Rakesh\\DLLNEW\\testDll\\dllManaged";
            //string filePath = "C:\\Webroot\\product.dgflick.com\\updates";
            string filePath = Server.MapPath("~/upload/templates/book-xpress/");
            //string str2 = "C:\\FileZilla2\\data";
            //string _DownloadableProductFileName = "Bx-Pro-Update-3.0.exe";
            string _DownloadableProductFileName = filenm;
            System.IO.FileInfo FileName = new System.IO.FileInfo(filePath + "\\" + _DownloadableProductFileName);
            FileStream myFile = new FileStream(filePath + "\\" + _DownloadableProductFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            BinaryReader _BinaryReader = new BinaryReader(myFile);
            if (FileName.Exists)
            {
                //try
                //{
                long startBytes = 0;
                string lastUpdateTiemStamp = File.GetLastWriteTimeUtc(filePath).ToString("r");
                string _EncodedData = HttpUtility.UrlEncode(_DownloadableProductFileName, Encoding.UTF8) + lastUpdateTiemStamp;
                Response.Clear();
                Response.Buffer = false;
                Response.AddHeader("Accept-Ranges", "bytes");
                Response.AppendHeader("ETag", "\"" + _EncodedData + "\"");
                Response.AppendHeader("Last-Modified", lastUpdateTiemStamp);
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName.Name);
                Response.AddHeader("Content-Length", (FileName.Length - startBytes).ToString());
                Response.AddHeader("Connection", "Keep-Alive");
                // Response.End();
                
                Response.ContentEncoding = Encoding.UTF8;
                _BinaryReader.BaseStream.Seek(startBytes, SeekOrigin.Begin);
                maxCount = (int)Math.Ceiling((FileName.Length - startBytes + 0.0) / 1024);
                for (i = 0; i < maxCount && Response.IsClientConnected; i++)
                {
                    Response.BinaryWrite(_BinaryReader.ReadBytes(1024));
                    Response.Flush();
                }
                if (i < maxCount)
                {
                    return false;
                }
                if (i == maxCount)
                {
                    if (Response.IsClientConnected == true)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                _BinaryReader.Close();
                myFile.Close();
                //if blocks transfered not equals total number of blocks
                //}
                //catch
                //{
                //    return false;
                //}
                //finally
                //{

                //    //Response.End();
                //    _BinaryReader.Close();
                //    myFile.Close();
                //}
            }
            return false;
        }

& the download is perform on these button which is in gridview-
protected void imgBtnDownload_Click(object sender, ImageClickEventArgs e)
        {
            
            string prodId = ((ImageButton)sender).CommandArgument.ToString();
            GridViewRow row = (GridViewRow)((Control)sender).Parent.Parent;
            int index = row.DataItemIndex;
            Label lblDownloadCount = (Label)gdvOrderDetails.Rows[index].FindControl("lblDownloadCount");
            Label lblExpiryDate = (Label)gdvOrderDetails.Rows[index].FindControl("lblExpiryDate");
            Label lblExDate = (Label)gdvOrderDetails.Rows[index].FindControl("lblExDate");
            Label lblProductName = (Label)gdvOrderDetails.Rows[index].FindControl("lblProductName");
            Label lblImgUrl = (Label)gdvOrderDetails.Rows[index].FindControl("lblImgUrl");
            string imgUrl = lblImgUrl.Text;
            string prodName = lblProductName.Text;
            string todaysDate = DateTime.Now.Date.ToShortDateString();
            DateTime today = Convert.ToDateTime(todaysDate);
            DateTime exDate = Convert.ToDateTime(lblExDate.Text);
            if (DateTime.Now.Date <= exDate.Date)
            {
                int downloadCount = Convert.ToInt32(lblDownloadCount.Text);
                if (downloadCount > 0)
                {
                    int lastFolderIndex = imgUrl.LastIndexOf("/");
                    string folders = imgUrl.Substring(0, lastFolderIndex + 1);
                    folders = folders.Replace("galleria/book-xpress/thumbnail", "templates/book-xpress");
                    //Response.Redirect(folders + lblProductName.Text + ".exe");
                    bool res = identify(lblProductName.Text + ".exe");
                    if (res == true)
                    {
                    clsDownload objUpdateCount = new clsDownload();
                    objUpdateCount.strOrderNo = lblOrderNo.Text;
                    objUpdateCount.strProductId = prodId;
                    clsDownload.UpdateDownloadCount(objUpdateCount);
                    //lblDownloadCount.Text = Convert.ToString(downloadCount - 1); 
                    //Response.AddHeader("Refresh","0;URL=Index.aspx"); 
                    //Response.AddHeader "Refresh", "0;URL=default.asp"
                    Response.Headers.Clear();
                        Response.Redirect(Request.RawUrl);
                      
                    //Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>windows.location.reload();</script>");
                    //LoadGrid();
                    }
                
               }
                else
                {
                    clsJava.MessageBox("Sorry, Your download count reached to 0. You can not download.");
                }
                }
                else
                {
                    clsJava.MessageBox("Sorry, Your downloads are expired. You can not download.");
                }
         }

the rowdatabound event of gridview-
protected void gdvOrderDetails_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string ordno = DataBinder.Eval(e.Row.DataItem, "OrderNo").ToString();
                string exDate = DataBinder.Eval(e.Row.DataItem, "ExDate").ToString();
                string dwn = DataBinder.Eval(e.Row.DataItem, "DownloadCount").ToString();
                string productType = DataBinder.Eval(e.Row.DataItem, "ProductType1").ToString();
                ImageButton imgBtnDownload = (ImageButton)e.Row.FindControl("imgBtnDownload");
                string todaysDate = DateTime.Now.Date.ToShortDateString();
                DateTime today = Convert.ToDateTime(todaysDate);
                DateTime expDate = Convert.ToDateTime(exDate);
                Label lblDownloadCount = (Label)e.Row.FindControl("lblDownloadCount");
                lblDownloadCount.Text = dwn; 
                if (DateTime.Now.Date > expDate.Date)
                {
                    lblDownloadCount.Text = "0";
                }
                if (productType == "2")
                {
                    imgBtnDownload.Visible = true;
                    lblDownloadCount.Visible = true;
                }
                else
                {
                    //TemplateControl tempContol =(TemplateControl)gdvOrderDetails.TemplateControl.FindControl("Downloads Remaining");
                    //tempContol.Visible = false;
                    imgBtnDownload.Visible = false;
                    lblDownloadCount.Visible = false;
                }
            }
        }

the Load Grid function which is load the grid-
 public void LoadGrid()
        {
            string orderNo = "DFSPL11289"; // clsQueryString.OrderNo.ToString();
            lblOrderNo.Text = orderNo;
            //string orderNo = "DFSPL11278";
            clsDownload objProductDownloads = new clsDownload();
            objProductDownloads.strOrderNo = orderNo;
            DataTable dtProductDownloads = clsDownload.GetAllProductDownloads(objProductDownloads);
            if (dtProductDownloads.Rows.Count > 0)
            {
                gdvOrderDetails.DataSource = dtProductDownloads;
                gdvOrderDetails.DataBind();
            }
        }
Posted
Updated 31-May-11 4:03am
v2
Comments
R. Giskard Reventlov 31-May-11 11:11am    
Where/how do you store the counter? In the database? Session? Note: only show relevant code: no one wants to have to wade through all of that.
rakesh-s-shinde 2-Jun-11 8:34am    
in database
BobJanova 31-May-11 11:17am    
This would be a truly annoying 'feature'. Why do you want to do it?
ArtificerGM 8-Jun-11 17:21pm    
Because you're a member of my site paying the bare-minimum membership fee which limits you to only 3 downloads per-file. For an extra $50/month you can upgrade to the platinum account which gives you unlimited downloads. Kind of like MegaUpload limits your bandwidth for a day if you don't have an account.
Sunasara Imdadhusen 1-Jun-11 2:44am    
what is use of <asp:Label ID="gdvlbldownloadcount" runat="server">

1 solution

Why has no one caught this yet!!

Just change this:
Label lblDownloadCount = (Label)e.Row.FindControl("lblDownloadCount");
to this:
Label lblDownloadCount = (Label)e.Row.FindControl("gdvlbldownloadcount");


[EDIT] I stand corrected ...
Ryan Gamal - yesterday
In your gridview your label has the id "gdvlbldownloadcount" but in your databound event you are trying to assign to a label with ID "lblDownloadCount" [/EDIT]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900