I have two tables, a query, and a form and I can't get it to behave like I want. There is a relationship between Table 1 and Table 2 formed between Employee ID (Table 1) and Employee (Table 2)
Employees (Table 1)
Employee ID (Key)
Employee Name
Status
Attendance (Table 2)
Attendance ID (Key)
Employee
Date
Note
Employee Query
Returns Employee ID and Employee Name for any employee with Status="Active"
Attendance Form (This form is used to enter records into Table 2)
Attendance ID (Table 2)
Employee (Drop down list from Employee Query)
Date
Note
Essentially what I want is for the form to work like so:
Dropdown to display Employee names that are "Active"
Save Employee ID back to Table 2
Display Employee name rather than ID in the form
The issue that I'm having is that the form wants to display the Employee ID rather than the name. This isn't practical for data entry but I still want the form to send the Employee ID to the table so we can run queries and reports.
Advise not to use spaces in naming convention.
Set combobox properties like:
ControlSource: Employee
ColumnCount: 2
ColumnWidths: 0";1"
BoundColumn: 1
RowSource: SELECT EmployeeID, EmployeeName AS FullName FROM Employees ORDER BY EmployeeName WHERE Status = 'Active';
This will display name but save id.
Employee name parts really should be in separate fields: FirstName, MiddleName, LastName. Then RowSource like:
SELECT EmployeeID, LastName & ", " & FirstName & " " & MiddleName AS FullName FROM Employees WHERE Status='Active';
Related
Combobox has 3 columns. Employee name, ID, Phone.. Textbox2 should show the id and textbox3 shows phone. issue is my id number in the table and query is 220005 but the ID Textbox, "textbox2", shows "5" ... Combobox does show Id as 220005. Any suggestions?
Private Sub EMP_Change()
Me.ID = Me.EMP.Column(1)
Me.Phone = Me.EMP.Column(2)
End Sub`
I would like to ask about inserting new record into table through entity framework. I have composited primary key (id and department_id). Department_Id is constant because there are departments and each department can create new record. Each record is replicated to all departments. Id is identity by department. But it cannot be identity on column because when all departments can create record with Id=1 but department_id will be different.
Now I solve it that I create new record in dummy table which has identity column and I get this value into Id column and save record. So for each table I have specialized table with identity column to generated Id value.
Table Customer (Id int, Department_Id int, …) Id, Department_Id is primary key.
Table CustomerIdentity(Id int identity(1,1), Dummy int null).
I have stored procedure to save new record:
declare #id int
declare #department_id int = 10 -- constant for department 10
insert into CustomerIdentity(Dummy) values (null)
set #id = scope_identity()
insert into Customer(Id, Department_Id, …) values (#id, #department_id, …)
I refactor this code to extract logic from stored procedure to C# where I have Customer entity. Id cannot be identity because there could be records as:
Id | Department_Id
1 | 1 -- record from department 1.
1 | 2 -- record from department 2.
1 | N -- record from department N.
So there is Identity table which holds identity value for each department. Each record from department 1..N is replicated to all departments.
How to set EF to give value for Id column from another table?
To do something with EF I would look at something like:
using (var context = new MyDbContext())
{
var customerIdentity = new CustomerIdentity();
context.CustomerIdentities.Add(customerIdentity);
context.SaveChanges();
var newCustomer = new Customer
{
DepartmentId = _DepartmentId, // I.e. "10"
CustomerId = customerIdentity.CustomerId
// ...
}
// Optionally you could delete the CustomerIdentity record.
context.CustomerIdentities.Remove(customerIdentity);
context.SaveChanges();
}
This assumes that each department will have its own database which would manage its own identity tables for the ids, but would be synchronized with other databases or a central database where data cross departments would be queried. The CustomerIdentity provides the sequence (autoincrement) for the IDs, so once that is retrieved (first SaveChanges call) and earmarked into the Customer record, the CustomerIdentity record can be deleted. The table would remain empty but the Identity would continue to increment. If you decide to keep the CustomerIdentity row you would want to avoid associating the Customer and CustomerIdentity entities in a relationship in EF even though for a department they would form a 1 to 1 relationship. Other departments identity counters could be higher and such a relationship would result in errors if the target database had identity up to say 50, and a record wants to get added with an ID of 65. Identity 65 doesn't yet exist so EF would throw a FK violation if you try to bring that customer record in.
I have to insert data to db in form fields that have the same path and i want to save it different id's but rather it concatenated it with ",", how could i possibly do it?
I tried to make some alias in SQL but it saves into same db field name with concatenated with ","
i expected in db when i insert that
EX.
db field name = description
input 1 value = "john";
input 2 value = "doe";
id description
1 john
2 doe
above is my expected result
but in my case when i insert it shows these
id description
1 john,doe
can someone help me to achieve that result ? THANKYOU!
Let me present a similar situation. You have a database of people and you are concerned that each person might have multiple phone numbers.
CREATE TABLE Persons (
person_id INT UNSIGNED AUTO_INCREMENT,
...
PRIMARY KEY(person_id) );
CREATE TABLE PhoneNumbers (
person_id INT UNSIGNED,
phone VARCHAR(20) CHARACTER SET ascii,
type ENUM('unknown', 'cell', 'home', 'work'),
PRIMARY KEY(person_id, phone) );
The table PhoneNumbers has a "many-to-1" relationship between phone numbers and persons. (It does not care if two persons share the same number.)
SELECT ...
GROUP CONCAT(pn.phone) AS phone_numbers,
...
FROM Persons AS p
LEFT JOIN PhoneNumbers AS pn USING(person_id)
...;
will deliver a commalist of phone numbers (eg: 123-456-7890,333-444-5555) for each person being selected. Because of the LEFT, it will deliver NULL in case a person has no associated phones.
To address your other question: It is not practical to split a commalist into the components.
Hi I have M:M relationship between two tables, Contacts and Tags and their M:M table is called Contacts_Tags:
Contacts
------------
ID
Name
Tags
-----------
ID
Name
Contacts_Tags
--------------
Contact_ID
Tag_ID
I have entities for Contacts called Contact and for Tags called Tag but not for Contacts_Tags table.
I want to left join in query builder
$queryBuilder = $this->entityManager->getRepository(Contact::class)->createQueryBuilder("o")->select("o");
$queryBuilder->leftJoin(//here, "et", "WITH", "et.Contact_ID = o.ID")
->leftJoin(Tag::class, "t", "WITH", "t.ID = et.Tag_ID")
;
But I cannot figure out how to add it. I tried documentations but it says to add Entity when I add entity of ContactTag it throws error that entity should have primary key.
Any idea?
To do left join :
$queryBuilder->join(table, condition, columns, $queryBuilder::JOIN_LEFT);
with :
table is the name of a table or another Select or array [alias => table]
condition is a string (the same as in Sql language)
columns is an array like [alias => column_name, ...] or [column_name, ...], may be empty
$queryBuilder is a Select, can be replaced by \Zend\Sql\Select::JOIN_LEFT
I have a scenario where I need to fetch list of string values from a table.
I need to query the Subject table and get list of subjects:
EmployeeDetails emp = new EmployeeDetails();
emp.subjects = from x in EmpDB.subjects
join y in EmpDB.employeeInfo
on x.subjectCode equals y.subjectCode select new
{
x.subjectTitle,
}.toList();
I have an employee ID which I need to query the EmployeeDetails table and get SubjectCodes of that particular Employee ID, and using those subject codes, I need to query the Subject table and get the subject values.
I assume that in EmployeeDetails class you have property Subjects with List.
EmployeeDetails emp = new EmployeeDetails();
emp.subjects = (from x in EmpDB.subjects
join y in EmpDB.employeeInfo on x.subjectCode equals y.subjectCode
where y.EmployeeId = <<your ID>> // Employee ID you searching
select x.subjectTitle).ToList();