Search code examples
c#ms-accessuser-input

C# program If statements issue


I am new to Stackoverflow.

At my job I have been assigned the task to create a script/program that will log people and staff that visit each of the 15+ schools that are in my district, into an access database so that administrative people can review it. I decided to use C# to make this program happen.

The issue I am running into with my code is that when a form loads, it will go through a series of IF statements that determine what school they are in based on 2 characters of the computer's hostname before it prompts users to enter their name and the reason they are visiting the school.

When I am debugging my code, I enter my name and reason then I click save which will input the information such as Person's name, person's visiting reasons, timestamp, and what school they are in, into an access database. But when I click save and look at the execution process via debugging, it only saves the name and reason but not the timestamp and the school. Only way I can get it to save the timestamp and school, is if I try to edit those string values (doesn't let me edit it, which is good) which is something I shouldn't have to do.

Here is my code below for the form which the user enters information:

    namespace district_logprogram
    {
        public partial class district_CheckIn : Form
        {
            public district_CheckIn()
            {
                InitializeComponent();
                txtTime.Text = DateTime.Now.ToString();
                txtLocation.Text = ToString();            
            }

        private void txtTime_TextChanged(object sender, EventArgs e)
        {
            //displays date and time on txtTime text box in Check In menu.
            //On pressing 'Save' it will input date/time into checkIn column in district_checkIn table
            var today = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss");
            txtTime.Text = today;
        }

        private void txtLocation_TextChanged(object sender, EventArgs e)
        {
            //displays which school you are located in
            //On pressing 'Save' it will input the school into location column in district_checkIn table
            System.Threading.Thread.Sleep(5);
            if (System.Environment.MachineName.Contains("01"))
            {
                txtLocation.Text = "School 1";
            }
            else if (System.Environment.MachineName.Contains("02"))
            {

                txtLocation.Text = "School 02";
            }
            else if (System.Environment.MachineName.Contains("03"))
            {

                txtLocation.Text = "School 03";
            }
            else if (System.Environment.MachineName.Contains("04"))
            {

                txtLocation.Text = "School 04";
            }
            else if (System.Environment.MachineName.Contains("05"))
            {

                txtLocation.Text = "School 05";
            }
            else if (SystS6.Environment.MachineName.Contains("06"))
            {

                txtLocation.Text = "School 06";
            }
            else if (System.Environment.MachineName.Contains("07"))
            {

                txtLocation.Text = "School 07";
            }
            else if (System.Environment.MachineName.Contains("08"))
            {

                txtLocation.Text = "School 08";
            }
            else if (System.Environment.MachineName.Contains("09"))
            {

                txtLocation.Text = "School 09";
            }
            else if (System.Environment.MachineName.Contains("10"))
            {

                txtLocation.Text = "School 10";
            }
            else if (System.Environment.MachineName.Contains("11"))
            {

                txtLocation.Text = "School 11";
            }
            else if (System.Environment.MachineName.Contains("12"))
            {

                txtLocation.Text = "School 12";
            }
            else if (System.Environment.MachineName.Contains("13"))
            {

                txtLocation.Text = "School 13";
            }
            else if (System.Environment.MachineName.Contains("14"))
            {

                txtLocation.Text = "School 14";
            }
            else if (System.Environment.MachineName.Contains("15"))
            {

                txtLocation.Text = "School 15";
            }
            else if (System.Environment.MachineName.Contains("16"))
            {

                txtLocation.Text = "School 16";
            }
            else if (System.Environment.MachineName.Contains("17"))
            {

                txtLocation.Text = "School 17";
            }
            else if (System.Environment.MachineName.Contains("18"))
            {

                txtLocation.Text = "School 18";
            }
            else if (System.Environment.MachineName.Contains("19"))
            {

                txtLocation.Text = "School 19";
            }
            else if (System.Environment.MachineName.Contains("20"))
            {

                txtLocation.Text = "School 20";
            }
            else if (System.Environment.MachineName.Contains("21"))
            {

                txtLocation.Text = "School 21";
            }
            else if (System.Environment.MachineName.Contains("22"))
            {

                txtLocation.Text = "School 22";
            }
            else if (System.Environment.MachineName.Contains("23"))
            {
                txtLocation.Text = "School 23";
            }
            else
            {
                //If computer hostname is not configured correctly, it will display message below
                btnSave.Enabled = false;
                txtLocation.Text = "Cannot determine school name! Check hostname!";
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try                   
            {
                districtcheckinBindingSource.EndEdit();
                district_checkinTableAdapter.Update(this.districtDB.district_checkin);
                Close(); //this closes the check in menu
                MessageBox.Show("You are checked in!");                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "An error occurred during check in!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                districtcheckinBindingSource.ResetBindings(false);
            }
        }

        private void district_CheckIn_Load(object sender, EventArgs e)
        {
            this.district_checkinTableAdapter.Fill(this.districtDB.district_checkin); //opens connection to district_checkin table
            districtcheckinBindingSource.DataSource = this.districtDB.district_checkin; //opens connection to district_checkin table
            txtName.Focus(); //focuses cursor on txtName text box field
            this.districtDB.district_checkin.Adddistrict_checkinRow(this.districtDB.district_checkin.Newdistrict_checkinRow()); //begins a new row for adding records
            districtcheckinBindingSource.MoveLast(); //moves new record to end of table - "shifts it down"            
        }        
    }
}

Here is what it looks like when I debug it after entering the information: only the name and reason are inputted but not time and school name And here is what it should look like after inputting information: what the input should look like before being inserted Any suggestions on how to resolve this issue would help greatly! :)


Solution

  • When comparing strings you should make sure you are comparing the cases as well i.e Upper and lower case.

    So either compare with UpperCase or LowerCase

    if (System.Environment.MachineName.ToUpperInvariant().Contains("SS"))
    

    Similarly do this for other conditions.