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-
<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
public bool identify(string filenm)
{
string filePath = Server.MapPath("~/upload/templates/book-xpress/");
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)
{
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.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();
}
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");
bool res = identify(lblProductName.Text + ".exe");
if (res == true)
{
clsDownload objUpdateCount = new clsDownload();
objUpdateCount.strOrderNo = lblOrderNo.Text;
objUpdateCount.strProductId = prodId;
clsDownload.UpdateDownloadCount(objUpdateCount);
Response.Headers.Clear();
Response.Redirect(Request.RawUrl);
}
}
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
{
imgBtnDownload.Visible = false;
lblDownloadCount.Visible = false;
}
}
}
the Load Grid function which is load the grid-
public void LoadGrid()
{
string orderNo = "DFSPL11289";
lblOrderNo.Text = orderNo;
clsDownload objProductDownloads = new clsDownload();
objProductDownloads.strOrderNo = orderNo;
DataTable dtProductDownloads = clsDownload.GetAllProductDownloads(objProductDownloads);
if (dtProductDownloads.Rows.Count > 0)
{
gdvOrderDetails.DataSource = dtProductDownloads;
gdvOrderDetails.DataBind();
}
}