C#

Spring 2011

 

Hash password with MD5 - Kenneth

 

using System.Security.Cryptography 

 

// Declare variables to be passed and returned from encryption routine

           string stgOriginal = txtPassword.Text;

           string stgEncrypted;

 

// Perform hash routine and return encrypted password to be stored in stgEncrypted

            stgEncrypted = EncodePassword(stgOriginal);

 

public string EncodePassword(string originalPassword)

        {

            //Declarations

            Byte[] originalBytes;

            Byte[] encodedBytes;

            MD5 md5;

 

//Instantiate MD5CryptoServiceProvider, get bytes for original password and compute hash (encoded password)

            md5 = new MD5CryptoServiceProvider();

            originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);

            encodedBytes = md5.ComputeHash(originalBytes);

 

            //Convert encoded bytes back to a 'readable' string

            return BitConverter.ToString(encodedBytes);

        }

Spring 2010

 

Report video tutorial-Garrett

 

This website contains a video on how to create a report using the report viewer.

http://www.youtube.com/watch?v=ociTNFwKii0

 

How to pull created reports up into a window form.- Jared

 

For the example, just consider the report you made is called "CrystalReport1" and that it was a summary report.

 

1. Create a new windows form and name it Summary.  Next, add the Crystal Report Viewer to the form.  Finally, double click the title bar of the form and in the FormLoad place the following code:

 

        CrystalReport1 summaryReport = new CrystalReport1();

        crystalReportViewer1.ReportSource = summaryReport;

 

2. Then just place the following code behind your Summary Report button on your Report Menu:

 

        Summary aSummary = new Summary();

 

        aSummary.Show();

 

Two separate opening menus - Jared

 

Two separate opening menus without having to use a database for the customer or owner name / password. It also includes the coding for clearing out the name and password and resetting the focus with appropriate error messages if the entries are wrong.

  

            if (txtUser.Text == "owner" & txtPassword.Text == "1234")

            {

                this.Dispose(false);

 

                OwnerMenu aOwnerMenu = new OwnerMenu();

 

                aOwnerMenu.Show();

            }

            else if (txtUser.Text == "customer" & txtPassword.Text == "5678")

            {

                this.Dispose(false);

 

                CustomerMenu aCustomerMenu = new CustomerMenu();

 

                aCustomerMenu.Show();

            }

            else

            {

                MessageBox.Show("Sorry the user name or password was incorrect.", "Incorrect",

                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                txtPassword.Clear();

                txtUser.Clear();

                txtUser.Focus();

            }

Moving the contents of a text box to the next form being opened.- Jared

 

  The text boxes on both forms must be named the same for this coding to work.

 

Under your click event for opening your second form the coding should read as follows:

 

{

      this.Dispose(false);

 

      SecondForm aSecondForm = new SecondForm(txtValue.Text);

 

      aSecondForm.Show();

}

 

In the code SecondForm is the name of the form to be opened and txtValue is the text box name.

  

On your second form the following code should be added under your variable declerations.

 

      txtPrice.Text = val;

Spring 2009

How to make it so you don’t have to change a path from computer to computer - Michael

Most of the time, when you move from one computer to another you have to change the path of where your jump drive is in your program. There is a couple lines of code that you can use to make that problem go away.  In my program I have everything in one folder and it is good to do this. Here is the path of my program F:\Senior Project\The Mulligan\ then my Website is under F:\Senior Project\.

      string Es = Path.GetDirectoryName(Application.ExecutablePath);

     string[] Column = Es.Split('\\');

     string Root = Column[0];

     string Sub = Column[1];

     string path = Root + @"\" + Sub + @"\" + @"\Website.htm";

 F: would be in the string Root and Senior Project would be in Sub.

You then just string it all together in the string Path. This code is only for if it is in one sub folder you would have to add more to it to make it if it is in another.

Multi user Log in form code from a SQL Database in C# - Michael

Code put in behind your login screen to pull the user name and password from a SQL database.

SqlConnection lgnConn = new SqlConnection(@"Data Source=uam-cis1\sqlexpress;Initial Catalog=Senior_Project_Howard;Integrated Security=SSPI");

            lgnConn.Open();

            SqlCommand cmd = new SqlCommand("SELECT pass_Password FROM Password WHERE pass_Emp_ID = " + txtUsername.Text ,lgnConn);

            SqlDataReader dr;

            try

            {

                dr = cmd.ExecuteReader();

                while (dr.Read())

                {

                    if (dr["pass_Password"].ToString() == txtPassword.Text)

                    {

                        Global.ClockinoutForm();

                        this.Close();

                    }

                    else

                    {

                        errorProvider1.SetError(txtPassword, "Password invalid");

                    }

                }

            }

            catch

            {

                errorProvider1.SetError(txtUsername, "Please Enter Username");

            }

Create a Global Function in C# - Michael

If you want to make it so that you don’t have to retype a certain set of code that you use on every form you can make a Global Function. To do this you have to first Click the Project Menu item at the top of VB, then you Click Add New Windows Form. It will bring up a new window Click Class and make sure that you name it Global. After you do that it will bring it up Type:

        public static void HomeForm()

        {

            // This brings up the home form and close the form that you where on.

 

              frmHome anHomeForm = new frmHome();

             anHomeForm.Show();

        }     

Make one of these for everything that you are repeating. To Use this statement you have to use this Line of code:

            // Shows the About Box

           

            Global.AboutForm();

            this.Close()

This can be done for more then just opening different forms you just have to change what goes into the public static void.

Fall 2007

 

C#- Where to code error messages - Sarah

 

Error messages can be coded behind the save button on the binding navigator. You can code a try/catch statement or error message right before the this.Validate(); code. This way when you hit save it checks the data that has been entered.

 

C# Advice - Sarah

 

After you have completed programming and run your program, every time after that run the program is an executable. This is in your debug folder within the bin folder of your project. The changes that you make from running it as an executable will update your database. However, if you debug your program again you will lose any changes you made and the database will return to the way it was the last time you debugged.

 

C# - To properly exit the program and stop debugging - Sarah

 

Use Application.Exit(); instead of this.Close();

 

Examples of C# code to go from one form to another - Sarah

{

 

//Goes to the employee form, and hides the current one.

 

frmEmployee frmEmployee = new frmEmployee();

frmEmployee.Show();

this.Hide();

 

}

 

{

 

//Goes to the main menu form.

 

frmMainMenu frmMainMenu = new frmMainMenu();

frmMainMenu.Show();

this.Hide();

 

}

 

C# Web Sites- Sarah

Website for C# with some how to videos.

http://msdn2.microsoft.com/en-us/vcsharp/default.aspx

 

Webiste for C# Frequently Asked Questions

http://blogs.msdn.com/csharpfaq/

 

Website for C# Help, including build errors and sample code.

http://msdn2.microsoft.com/en-us/library/aa287558.aspx

 

How to do anything website.

http://www.howtodothings.com/

Creating Crystal Reports in C# - Sarah

 

Click the Projects tab on the Menu Bar

Choose Add Windows Form

Type in the Name you have selected for your form

Make sure that Windows Form is selected before you click Add

Open the Toolbox

Locate the Crystal Reports section and expand it

Drag and Drop the Crystal Report Viewer to the form you just created

The viewer will be placed on the new form

Click on the Smart Tag in the upper right hand corner

The Crystal Reports Viewer Task Menu will appear

Choose to create a New Crystal Report

Enter the Name you have chosen for the report and click OK

Accept the License Agreement and click OK

Choose the option Using the Report Wizard and Standard

Then click OK

Expand the New Connection Folder

Choose which ever option fits your needs

Then you will have to choose the location of your database

Finish the wizard by selecting the appropriate tables and fields you want on the report

When you are finished your report will appear on the form where you placed your viewer

You will be in either Main Report View or Main Report Preview

Fall 2007

 

Using Crystal Reports - Miranda

Always run the system on the same computer you built the reports. If the drive changes you are likely to encounter a load failure. You must go in and correct the file path if you change computers.

 

Websites for free counters to use on your webpages - Miranda

http://www.freestatscounter.com/

http://rawhits.com/http://rawhits.com/

 

Database load code for C# - Miranda

private void frmDatabase_Load(object sender, EventArgs e)

 {// TODO: This line of code loads data into the'mMABooks03DataSet.Customers'

   table. You can move, or remove it, as needed.

    this.customersTableAdapter.Fill(this.mMABooks03DataSet.Customers);

  }