Friday, November 20, 2009

Download File with ProgressBar ASP.NET

Create a Click_Event for the button by double clicking it.

Add this using statement to the top of the form.

using System.Net;




private void btnStartDownload_Click(object sender, EventArgs e)
{
WebClient client = new WebClient();
client
.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client
.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);

// Starts the download
client
.DownloadFileAsync(new Uri("SomeURLToFile"), "SomePlaceOnLocalHardDrive");

btnStartDownload
.Text = "Download In Process";
btnStartDownload
.Enabled = false;
}




In the previous code, we are creating two event handlers to handle when
the Progress of the file download changes, and when the download
completes.

We also use the DownloadFileAsync method so that the download(which could be lengthy) does not freeze the main GUI.

Next, we need to do the code to calculate the percentage of the download complete so that we can assign it to the ProgressBar.


void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
double bytesIn = double.Parse(e.BytesReceived.ToString());
double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
double percentage = bytesIn / totalBytes * 100;

progressBar1
.Value = int.Parse(Math.Truncate(percentage).ToString());
}




In this code, we use the BytesReceived and TotalBytesToReceive properties of the DownloadProgressChangedEventArgs object.

Next, we need to do the code for the event when the download completes.




void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
MessageBox.Show("Download Completed");

btnStartDownload
.Text = "Start Download";
btnStartDownload
.Enabled = true;
}

Dynamically Creat Menu C#

Creating Menu Item at Run Time C# .net Code


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;


namespace Reports
{
public partial class Reports : Form
{
OdbcDataReader od;
Connect c = new Connect();
string[] menuitem=new string[20];
MenuStrip menu = new MenuStrip();
int id = 0;

public Reports()
{
InitializeComponent();
}

private void Reports_Load(object sender, EventArgs e)
{
c.cmd().CommandText = "select distinct r.undermenu,r.undername from rptlist as r ,user_auth as u where r.menuname=u.repotname and u.uid='02' and u.prm='1' ";
c.cmd().Connection = c.cn();
od = c.cmd().ExecuteReader();

while (od.Read())
{

string s = od["undermenu"].ToString();
// menuStrip1.Items.Add(s);
//menuStrip1.Items.Add(s);
menuitem.SetValue(s, id);
id++;

}
od.Close();
for (int i =0; i< id ; i++)
{
ToolStripMenuItem item = new ToolStripMenuItem(menuitem[i].ToString());

// string menuname = menuStrip1.Items[i].Text.ToString();
c.cmd().CommandText = "select r.menuname from rptlist as r ,user_auth as u where r.menuname=u.repotname and u.uid='02' and r.undermenu like '" + menuitem[i].ToString() + "'and u.prm='1' order by r.undermenu,r.menuname";
c.cmd().Connection = c.cn();
od = c.cmd().ExecuteReader();
while (od.Read())
{

string sa = od["menuname"].ToString();
// menuStrip1.Items.Add(s);
item.DropDownItems.Add(sa.ToString(),null,new System.EventHandler(menuitem_click));
menu.Items.Add(item);
// item.Click += new EventHandler (this.menuitem_click);
}
od.Close();
}
this.Controls.Add(menu);


}

Connection Class C# .Net

Make Connection Class in C# .Net


using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Odbc;

namespace Reports
{
class Connect
{
OdbcConnection con = new OdbcConnection("dsn=local");
OdbcCommand cm = new OdbcCommand();
OdbcDataReader od;
OdbcDataAdapter odz = new OdbcDataAdapter();

public OdbcConnection cn()
{
if (con.State.ToString() == "Open")
{
}
else if (con.State.ToString() == "Closed" || con.State.ToString() == "Broken")
{
con.Open();

}
return con;
}
public OdbcCommand cmd()
{

return cm;

}
public OdbcDataReader odr()
{


return od;
}
public OdbcDataAdapter oda()
{


return odz;
}
}
}

Pakistan vs NewZealand Live Cricke Match Streaming

Pakistan vs NewZealand Live Streaming1


Pakistan vs NewZealand Live Streaming2


Pakistan vs NewZealand Live Streaming3



Pakistan vs NewZealand Live Streaming4



Pakistan vs NewZealand Live Streaming5



Pakistan vs NewZealand Live Streaming6