В следующем коде у меня есть форма Pincode, которая проверяет таблицу сотрудников на действительный Pincode. Он также проверяет, есть ли у записи Isadmin для доступа к _targetform, если _targetform Is Administration < /p>
Как добавить второй запрос в следующем коде, который также проверяет таблицу часов времени, если у employeeid есть запись, помеченная iSactive, перед загрузкой _targetform. Если он не запрашивает, если пользователь захочет включить в форму Timeclock, а затем загрузить _targetform после того, как запустить или не включать, и открыть _targetform. private void Submitbutton_Click(object sender, EventArgs e)
{
// Handle the Submit button click event
// You can add your logic here to process the entered PIN code
string pinCode = PINCODE.Text; // Assuming your textbox is named 'pincodeTextBox'
string EmployeeID = string.Empty;
string FirstName = string.Empty;
string LastName = string.Empty;
// Replace with your actual connection string
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["PublicConnectionString"].ConnectionString;
using SqlConnection conn = new(connectionString);
string query = "SELECT IsAdmin, EmployeeID, FirstName,LastName FROM Employees WHERE Pin = @PinCode";
using SqlCommand cmd = new(query, conn);
// Use parameters to prevent SQL injection attacks.
cmd.Parameters.AddWithValue("@PinCode", pinCode);
conn.Open();
object result = cmd.ExecuteScalar();
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read()) // Checks if there is a row to read
{
// Retrieve the values from the reader
bool isAdmin = reader.GetBoolean(reader.GetOrdinal("IsAdmin"));
int employeeID = reader.GetInt32(reader.GetOrdinal("EmployeeID"));
string firstName = reader.GetString(reader.GetOrdinal("FirstName"));
string lastName = reader.GetString(reader.GetOrdinal("LastName"));
EmployeeID = employeeID.ToString();
FirstName = firstName;
LastName = lastName;
}
{
// Check if a record was found.
if (result != null)
{
// Get the type of your specific administration form.
Type administrationFormType = typeof(Administration); // Replace with your actual admin form type
// Check if the target form is the administration form.
// You can also use other forms as needed here.
if (_targetFormType == administrationFormType)
{
// Check if the user is an admin.
bool IsAdmin = Convert.ToBoolean(result);
if (IsAdmin)
{
// A variable can hold the proper form type to be opened.
Form newForm = (Form)Activator.CreateInstance(_targetFormType, new object[] { EmployeeID, FirstName, LastName });
newForm.Show();
this.Hide(); // Optionally hide the login form.
}
else
{
MessageBox.Show("Access Denied: You do not have administrator privileges.");
}
}
else
{
// A variable can hold the proper form type to be opened.
Form newForm = (Form)Activator.CreateInstance(_targetFormType, new object[] { EmployeeID, FirstName, LastName });
newForm.Show();
this.Hide(); // Optionally hide the login form.
}
}
else
{
MessageBox.Show("Invalid PIN code. Please try again.");
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... y-are-open