В следующем коде у меня есть форма Pincode, которая проверяет таблицу сотрудников на действительный Pincode. It also checks if the record has IsAdmin for access to _TargetForm if _TargetForm is Administration.
In the TimeClock table I have:
имя столбца < /th>
тип данных < /th>
< /tr>
< /thead>
id < /td>
int < /td>
< /tr>
/> int < /td>
< /tr>
clockin < /td>
dateTime2 (7) < /td>
< /tr>
clockout < /td>
datettime 2 (7) < /td>
(7) < /td>
(7) < /td>
(7) < /td>
(7). />
TotalTime
int
IsActive
bit
Когда пользовательские часы в новой записи создаются с работником, часами (время) и Isactive для True. Когда пользователь выпускает ту же запись, обновляется с помощью Clockout (время). TotalTime рассчитывается, а iSactive переключается на false.
Как добавить второй запрос в следующем коде, который также проверяет таблицу TimeClock , если у employeeeid есть запись, помеченная iSactive перед загрузкой _TargetForm ? Если у него нет такой записи, запрашивайте, если пользователь захочет включить в форму timeclock , а затем загрузите _targetform после работы или не включать и открыть _targetform .
Вот текущий код, который работает без Timeclock werce:
.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 ... hey-are-op