April 2009

April 2, 2009

C# FTP Demo

How to use the FTP functions built-in to C#

This is a more intermediate video.

What you will learn:
1) How to create a reuseable FTPFunction class library (with three static methods: Upload, Download, GetFileList.
2) How to call the function library from both a Windows form and a Console program.

First, I found some sample FTP code from this site:

http://aspalliance.com/1187_Building_a_Simple_FTP_Application_Using_C_20

I wrote the console program, because I wanted to schedule a nightly download from an FTP site in order to back-up an SQL server database backup (from my dedicated server to my laptop).

I decided to package the three routines in a class library, so it could be a shareable and reuseable DLL.
I then am able to call it from various other programs (and in the video, I do this from a Windows form, and a Console Command-Line program).

Once the code was packaged in the .DLL, it was easy to call it like this:

Code for you to copy:


    FTPFunctions.FTPFunctions.Download(FTPLocalPath,
                       FTPFilename,
                       FTPServer,
                       FTPUserid,
                       FTPPassword);

I created Download, Upload, and GetFileList as static methods, so they can be called without instantiating a class.

Two videos coming shortly…

Filed under FTP by Administrator

Permalink Print Comment

C# Intro to Try/Catch

Using “Try/Catch” in C#

A Try/Catch block is the error handling mechanism for .NET.

What you will learn here:
1) How to wrap your code in a “Try” statement
2) How to catch and display errors in the “Catch” statement
3) The three major properties of the Exception class (Message, Source, StackTrace) and the ToString() method.

Code for you to copy:


        private void btnAddTwoNumbers_Click(object sender, EventArgs e)
        {
            try
            {
                decimal decimalResult = AddTwoNumbers(txtNumber1.Text,
                                                      txtNumber2.Text);
                txtResult.Text = Convert.ToString(decimalResult);
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    "You did not enter two valid numbers, please try again.\r\n"
                    + " Message=" +  ex.Message + "\r\n"
                    + " Source=" +  ex.Source+ "\r\n"
                    + " StackTrace=" +  ex.StackTrace + "\r\n"
                ); 

            }

        }

        // This routine was created to demonstrate the StackTrace
        private static decimal AddTwoNumbers(string A, string B)
        {
            decimal decimalA = decimal.Parse(A);
            decimal decimalB = decimal.Parse(B);
            decimal decimalResult = decimalA + decimalB; 

            return decimalResult;
        }
    }

Video Demonstration:

Part 1 of 2



Part 2 of 2

Filed under C# Try/Catch by Administrator

Permalink Print Comment

C# MessageBox

Using “MessageBox” in C#

A MessageBox is basically a pop-up box that shows a message to the user. It may or may not provide buttons to get feedback from the user.

What you will learn here:
1) How to use MessageBox.Show with various parameters
2) How to find out which button the user clicked-on in the MessageBox
3) How to change the caption of the MessageBox

Code for you to copy:


        private void buttonOkay_Click(object sender, EventArgs e)
        {
            DialogResult dr =
                 MessageBox.Show("Hello World",
                "My Caption",
                MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                txtResults.Text = "You chose the 'Yes' button";
            }
            if (dr == DialogResult.No)
            {
                txtResults.Text = "You chose the 'No' button";
            }
        }

Video Demonstration:

Filed under C# MessageBox by Administrator

Permalink Print Comment

C# Lists

Use of Lists in C#

What you will learn here:
1) Syntax is “List” where type can be a built-in type such as int, decimal, bool, string, or can be a class (either a .NET built-in class or one of your own).
2) How to add items to a List
3) How to use a “foreach” statement to loop through a list
4) Our specific example is a subroutine (method) that receives a List of numbers, and returns only the odd numbers in that List.

This video is a solution to a “quiz” I was given in an interview. A developer gave me this method signature, and asked me to scratch out the code to accomplish returning all the odd numbers in the list (at first I thought he said all the odd numbered items in the list).


   public List GetOddNumbers(List numList)
    {
      ...
    }

I had a “brain cloud” when doing this demo. I was thinking I could divide by 2 to find out if the number was even. I totally forgot about “modulo” division, which is what my subconscious was thinking. If you use a backslash instead of a forward slash, then the remainder will be returned. If you do modulo division dividing by 2, and the remainder is zero, then you have an even number, otherwise an odd number. That’s a more elegant way to do the odd/even test than what I did in the video below.

Code for you to copy:


namespace CSharpList
{
    class Program
    {
        static void Main(string[] args)
        {
            List numList = new List();
            numList.Add(5);
            numList.Add(6);
            numList.Add(7);
            numList.Add(20);
            numList.Add(21);
            numList.Add(22);
            numList.Add(23);
            List oddNumberList = GetOddNumbers(numList); 

            Console.WriteLine("Press enter to end:");
            Console.ReadLine(); 

        }

        // Extract odd numbers from a list of numbers
        public static List GetOddNumbers(List numList)
        {
            List returnOddNumberList = new List();
            foreach (int i in numList)
            {
                int x = i / 2;
                decimal y = (decimal) i / 2M;
                if (x != y)
                {
                    // we must have an odd number
                    returnOddNumberList.Add(i);
                    Console.WriteLine(
                       "Returning odd number = " + i);
                }

            }  // end foreach 

            return returnOddNumberList;
        }

    }
}

Video Demonstration:

Filed under C# Lists by Administrator

Permalink Print Comment