Wednesday, February 27, 2013

How to download file using C#?


In some of the cases we need to download file from the Internet, In this article I am explaining-

How to download file using C#?

For this I have used WebResponse, WebRequest Classes so we need to include System.Net Namespace.To download the file from Internet we have to make request to particular object so that I have used webRequest class object. After getting the response from the server we need to handle that response so I have used webResponse class object.

For displaying downloading process I have used ProgressBar control.

Here is the code-

 private void btnDownLoadFile_Click(object sender, EventArgs e)
        {
           
            WebRequest requestObj ;
            WebResponse responseObj;
            Stream readerData ;
            Stream writerData ;
            byte[] data = new byte[1024];
            int count = 0;
            int total = 0;
            this.Text = "Downloading file .....";
           
            string DestinationFileName= "TargetLocation";
            string SourceLocation= "SourceLocation";

            Application.DoEvents();

            try
            {
                requestObj = WebRequest.Create(
SourceLocation);
                responseObj = requestObj.GetResponse();
                readerData = responseObj.GetResponseStream();
                progressBar1.Maximum = Convert.ToInt32(responseObj .ContentLength);
                progressBar1.Value = 0;
                total = 0;
                writerData = File.Create(DestinationFileName);
                while (true)
                {
                    count = readerData .Read(data, 0, 1024);
                    if (count <= 0)
                    {
                        break;
                    }
                    writerData.Write(data, 0, count);
                    total += 1024;
                    if (total < progressBar1.Maximum)
                        progressBar1.Value = total;
                    Application.DoEvents();
                }
                readerData .Close();
                writerData.Close();
                MessageBox.Show("Download completed...");
            }
            catch (Exception ex)
            {  
                MessageBox.Show("Error: " + ex.Message);
            }
        }



In above code, I am making request to download file using WebRequest Class object, for that I am passing source file url to download file.

 requestObj = WebRequest.Create(TargetLocation);

After making request we will get response, using this line of code-

  responseObj = requestObj.GetResponse();

Now using using Stream class object I am reading and writing data to the target file.

 count = readerData .Read(data, 0, 1024);

 writerData.Write(data, 0, count);


Meanwhile for displaying the download process, I am showing progressbar. Based on data read and write progressbar value will be increased which will show downloading process.


Output-


Downloading file



Thanks


Sunday, February 17, 2013

How to update record without primary key?


If we have any primary key in table then it is very easy to update the records  in the database table, but generally we face problem when we don't have primary in table because on that time we can not use any particular column to update records because that column can contain duplicate values.

But there is work around for this problem, we can use” ROW_NUMBER()” function to generate identity value for each record and then based on that “ROW_NUMBER” we can delete record from the table.

In this article, I am explaining - How to update nth record of the table?
Or
How to update record without primary key?



For this I am using “ROW_NUMBER()” function in query, Here is syntax -

Syntax-

WITH Record AS (SELECT *, ROW_NUMBER() OVER (ORDER BY [ColumnName]) AS rownum FROM TableName where condition) update Record  set  col1=value ,col2=value where Record.rownum = value

Example-

WITH Record AS (SELECT *, ROW_NUMBER() OVER (ORDER BY EmpName) AS rownum FROM tab_Employee ) update Record  set  Age=25 ,Salary=45000 where Record.rownum = 1


In this example,   “ROW_NUMBER()” is a function which will generate sequence number for each record, here ordering will be based on EmpName column, you can change it.

Here "rownum" will be additional column in the result set, this will be used to update the record.

Here “Record “ is result set.


Try this example.


Thanks