Cascade Dropdown and set Detail value properly - c#

I have the following cascade relationships between Parent and Child categories and keep the ChildCategoryId in the Personel table. When creating a new Personel record there are 2 cascade Dropdownlist and child records are listed according to their parents. Only ChildCategoryId is saved to the Personel table e.g. when select Education and Sales, the ChildCategoryId is 3. However, when selecting a parent record that has no child i.e. Family or Culture, I have no idea what value should be saved to the Personel table.
Of course I can add two "N/A" records to the Child table for Family or Culture records and save their Ids to the Personel table, but I am not sure if it is a good idea. Because when I list the Child category name, "N/A" will be displayed on the details page.
Is there a best practices for this kind of scenarios?
Personel:
Id | Name | ChildCategoryId |
----------------------------------
1001 | John | 1 |
1002 | Mary | 2 |
1003 | Bill | 3 |
1004 | Jose | 4 |
1005 | Anna | 5 |
ChildCategory:
Id | Name | ParentCategoryId |
-----------------------------------
1 | IT | 101 |
2 | Finance | 102 |
3 | Sales | 102 |
4 | PR | 103 |
5 | HR | 103 |
ParentCategory:
Id | Name |
------------------
101 | Health |
102 | Education |
103 | Sport |
104 | Family |
105 | Culture |

Related

Check child records of a record before making status of it as false in SQL Server

I have a application with some relational tables in my SQL Server database.
In the application, whenever user deletes and records I never delete it from my database, instead I make 'status' (table column) as 'False'.
Eg: User will delete a record from tblAccounts, the records has a column 'Status' which can be either true/false. On delete action record is set to false.
Now the problem is this account may be referred in other transactions. If it is used in other tables then it should not allow user to delete (make it false).
If I allow user to delete the record physically from table, it will throw foreign key error but in this scenario (making it false) how can I check the child rows without deleting and prompt the User.
I can do it by a select query on each table but that will be slow down my application.
Is there any other way/idea to achieve it?
I can do it by a select query on each table but that will be slow down
my application.
Is there any other way/idea to achieve it?
No there is not. You can do the automation with either a CHECK constraint that calls a function or with a TRIGGER, but in that code a SELECT statement against the other tables will have to be performed. There is no way around it.
You could do it using a foreign key, the trick here is to make both columns (AccountID and Status) Primary in tblAccounts. Then in the transaction table, you create a foreign key to both (AccountID and Status) with cascade on UPDATE/Delete. This means, if you ever change/delete an account id or its status from tblAccounts, the changes will be applied on all foreign keys as well.
Here is an example :
CREATE TABLE tblAccounts(
ID INT,
AccountID INT NOT NULL,
[Status] BIT NOT NULL
)
ALTER TABLE tblAccounts
ADD PRIMARY KEY (AccountID, [Status])
CREATE TABLE tblTransactions(
[ID] INT,
[TransID] INT NOT NULL PRIMARY KEY,
[AcctID] INT NOT NULL,
[Status] BIT NOT NULL
)
ALTER TABLE tblTransactions
ADD FOREIGN KEY (AcctID,[Status]) REFERENCES tblAccounts(AccountID, [Status])
ON UPDATE CASCADE
ON DELETE CASCADE
INSERT INTO tblAccounts (ID, AccountID, [Status])
VALUES
(1,1000,1),
(2,1100,1),
(3,1200,1),
(4,1300,1)
INSERT INTO tblTransactions(ID, TransID, AcctID,[Status])
VALUES
(1,5000,1000,1),
(2,3258,1300,1),
(3,5852,1000,1),
(4,9631,1100,1),
(5,1870,1200,1)
tblAccounts
| ID | AccountID | Status |
|----|-----------|--------|
| 1 | 1000 | true |
| 2 | 1100 | true |
| 3 | 1200 | true |
| 4 | 1300 | true |
tblTransactions
| ID | TransID | AcctID | Status |
|----|---------|--------|--------|
| 1 | 5000 | 1000 | true |
| 2 | 3258 | 1300 | true |
| 3 | 5852 | 1000 | true |
| 4 | 9631 | 1100 | true |
| 5 | 1870 | 1200 | true |
Let's change the status of AccountID 1100 to false
UPDATE tblAccounts
SET
[Status] = 0
WHERE
AccountID = 1100
Check tblAccount
| ID | AccountID | Status |
|----|-----------|--------|
| 1 | 1000 | true |
| 2 | 1100 | false|
| 3 | 1200 | true |
| 4 | 1300 | true |
Check tblTransactions
| ID | TransID | AcctID | Status |
|----|---------|--------|--------|
| 1 | 5000 | 1000 | true |
| 2 | 3258 | 1300 | true |
| 3 | 5852 | 1000 | true |
| 4 | 9631 | 1100 | false|
| 5 | 1870 | 1200 | true |

MSSQL Query omitting specific row value?

I have the following query to retrieve the customer sales report based on customer ID. It works, but how can I omit the customer name being duplicated?
select Cu.CustomerName, Cu.City, pd.pname,
Cs.qty, Cs.totalAmount, Cs.payed, Cs.credit, Cs.CreditEndDate
from Customer Cu
inner join CreditSales Cs on Cu.ID=Cs.CustomerID
left join Product pd on pd.pid=Cs.pid
where Cu.ID=6
order by Cu.CustomerName
The result is:
+--------+-------+---------------+----+----------+----------+----------+------------+
| Halima | Jimma | Mouse | 1 | 345.00 | 345.00 | 0.00 | 2015-08-29 |
| Halima | Jimma | Mobile | 10 | 92000.00 | 40000.00 | 52000.00 | 2015-08-23 |
| Halima | Jimma | Iphone | 2 | 13800.00 | 6500.00 | 7300.00 | 2015-08-28 |
| Halima | Jimma | Tape Recorder | 10 | 5175.00 | 4000.00 | 1175.00 | 2015-10-30 |
+--------+-------+---------------+----+----------+----------+----------+------------+
But I need like this:
+--------+-------+---------------+----+----------+----------+----------+------------+
| Halima | Jimma | Mouse | 1 | 345.00 | 345.00 | 0.00 | 2015-08-29 |
| | Jimma | Mobile | 10 | 92000.00 | 40000.00 | 52000.00 | 2015-08-23 |
| | Jimma | Iphone | 2 | 13800.00 | 6500.00 | 7300.00 | 2015-08-28 |
| | Jimma | Tape Recorder | 10 | 5175.00 | 4000.00 | 1175.00 | 2015-10-30 |
+--------+-------+---------------+----+----------+----------+----------+------------+
That means the customer name should displayed only one instance.
Any help? Thanks in Advance.
Based on Milen Pavlov response, you can try this:
SELECT CASE WHEN ROW_NUMBER() over (partition by [CustomerName] order by [CustomerName]) = 1 THEN [CustomerName] ELSE '' END
FROM Customer
ORDER BY [CustomerName]
This could do the job (I need you have to add an ordering in the over() clause):
select case when a.rn > 1 then '' else a.CustomerName end as CustomerName, a.City, a.pname, a.qty, a.totalAmount, a.payed, a.credit, a.CreditEndDate
from
(
select Cu.CustomerName, Cu.City, pd.pname,
Cs.qty, Cs.totalAmount, Cs.payed, Cs.credit, Cs.CreditEndDate, ROW_NUMBER() over (partition by Cu.CustomerName order by Cs.CreditEndDate) as rn
from Customer Cu
inner join CreditSales Cs on Cu.ID=Cs.CustomerID
left join Product pd on pd.pid=Cs.pid
where Cu.ID=6
) a
order by a.CustomerName
don't think it's a job for SQL but if you insists you can create two tables the first one with all the rows and columns and than delete the first row, and the other table with the first row and just make a left join and make the join on all the fields.

how to add data into list view from datatable

I have one confusion with SQL Query.
I have three tables, student, books, book_taken_by_student.
In the last table we have all the details about books taken by students.
In list view I bind the student name as row vies and different books name as column vies from student and books table.
The list view look like
| Book1 | Book2 | Book 3 | Book 4|
A |
B |
C |
D |
E |
F |
Now with help of book_taken_by_student table I want to bind Yes or No in front of student name related with book name taken by them
How I can solve this question.
Finally I want to show my list view like this
| Book1 | Book2 | Book 3 | Book 4|
A | Yes | No | Yes | Yes
B | No | Yes | Yes | No
C | No | Yes | Yes | No
D | Yes | No | Yes | Yes
E | Yes | No | Yes | Yes
F | No | Yes | Yes | Yes

Divide Crystal Report Vertically in Two Parts

I have to SP, from which i want to generate Report. I need output in two different Columns on one Detail Sections. I have tried Format with Multiple Columns, but it do not full fill my needs..
-- My Data
-- Student Record
+------+------------+
| Name | FatherName |
+------+------------+
| A | B |
| C | D |
| E | F |
+------+------------+
--Teacher Record
+------+------------+
|Name | FatherName |
+------+------------+
| W | X |
| Y | Z |
+------+------------+
My required out will look like below ...
Students Record Teachers Record
+------+------------+ +------+------------+
| Name | FatherName | | Name | FatherName |
+------+------------+ +------+------------+
| A | B | | W | X |
| C | D | | Y | Z |
| E | F | +------+------------+
+------+------------+
Is there any way to get this output in one Detail Section. Student and Teacher have different number of records, one have 10 record 2nd have 5 record..
Any Help...
You can't do this in one detail section in Crystal Reports.
However, you can develop a report for each of these tables separately, then add the two reports side-by-side as sub-reports into a new report - this should achieve your objective.
I think you can do it by right-clicking the details section then Insert->subreport, one for Teachers record and one for the Students record, then editing these subreports with their respective data source.

Gridview and Select query

I am working with gridview and tables, I am using c# and asp.net. Assuming I have 2 tables, lets just name it Profiles and Info.
In table profiles let's say I have fields with p_Id, FirstName, LastName and in Transaction I have I_Id, childsID, fathersID, mothersID.
This is what my tables looks like:
Profile
| p_Id | FirstName | LastName |
| 1 | Jack | Cole |
| 2 | Cynthia | Cole |
| 3 | Robert | Cole |
Info
| I_Id | childsID | fathersID | mothersID |
| 1 | 1 | 3 | 2 |
Now in my gridview I have to display FirstName, LastName, Father and Mother. But I need to display their names not their ID's.
For now my gridview looks like this:
First Name | Last Name | Father| Mother |
Jack | Cole | 3 | 2 |
what I want is like this:
First Name | Last Name | Father | Mother |
Jack | Cole | Robert Cole | Cynthia Cole |
try this
select p.firstName, p.LastName
, (select sp.FirstName+' '+sp.Lastname FROM profile sp WHERE sp.p_id=i.fathersid) as father
, (select sp.FirstName+' '+sp.Lastname FROM profile sp WHERE sp.p_id=i.mothersid) as mother
from info i
inner join profile p ON (p.p_id=i.childsID)

Categories

Resources