private void btnEnter_Click(object sender, EventArgs e) { string userPass = ""; //Password Validation if (!usersCmd.Parameters.Contains("@username")) { usersCmd.Parameters.Add(new global::System.Data.SqlClient.SqlParameter("@username", global::System.Data.SqlDbType.NChar)); } usersCmd.Parameters["@username"].Value = cmbUserType.SelectedItem.ToString(); usersCmd.CommandText = "SELECT password, role FROM users WHERE username = @username"; con.Open(); dr = usersCmd.ExecuteReader(); if (dr.Read()) { userPass = dr.GetString(0); } userPass = userPass.Replace(" ", String.Empty);//remove whitespaces if (txtPassword.Text == userPass) {//Password is OK //string securePass=MD5Hash(userPass); SingletoneUser.UserName = cmbUserType.SelectedItem.ToString(); SingletoneUser.UserPass = userPass; SingletoneUser.Role = (dr.GetString(1) == "Admin" ? 0 : 1); con.Close(); //close the connection LinqExample.Forms.Menu menu = new LinqExample.Forms.Menu(); menu.FormClosed += new FormClosedEventHandler(child_FormClosed); //add handler to catch when child form is closed menu.Show(); //show child this.Hide(); //hide parent x = new RTDataGenerator(); x.Start();//Start to generate the values } else { MessageBox.Show("Wrong Password", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error); txtPassword.Text = ""; txtPassword.Focus(); //Set the focus to the password field for second chance. con.Close(); //close the connection } }
//Generat the values based on contract database private void Generator(object arg) { RTDataGenerator me = (RTDataGenerator)arg; Random randGenerator = new Random(); while (shouldContinue) { foreach (DeviceData currDevice in devicesData) { DeviceData theChosenDevice = currDevice; if (thresholdForDeviceType.ContainsKey(theChosenDevice.type)) { ContractData currContract = thresholdForDeviceType[theChosenDevice.type]; foreach (KeyValuePair <int, float> thresholdWithContractValue in currContract.listThresholdIds) { int thresholdId = thresholdWithContractValue.Key; ThresholdData theChosenRange = thresholds[thresholdId]; //device_id insertSimulatedMeasurementCmd.Parameters[0].Value = theChosenDevice.id; //threshold_id insertSimulatedMeasurementCmd.Parameters[1].Value = thresholdId; //value insertSimulatedMeasurementCmd.Parameters[2].Value = GetValueForDeviceThresholdPair(theChosenDevice, thresholdId, currContract, (irregularPeak % 67) == 0); //timestamp insertSimulatedMeasurementCmd.Parameters[3].Value = DateTime.Now; //Here for debugging - need to know the generate values are correct Debug.WriteLine("device: " + theChosenDevice.id + " threshold_id: " + thresholdId + " value: " + insertSimulatedMeasurementCmd.Parameters[2].Value + " time: " + insertSimulatedMeasurementCmd.Parameters[3].Value); //ExecuteNonQuery - use when insert data to database insertSimulatedMeasurementCmd.ExecuteNonQuery(); //Incrase the irregularPeak to insert diff values after some time ++irregularPeak; } } } //Generate values each time based on the interval System.Threading.Thread.Sleep(interval); } }