Displaying foreign key on datagridview - c#

Lets say I have two tables in database, Table1 and Table2. Relationship is one to many from table2 to table1. I would like to show all data from Table1 and not all columns from Table2 on GUI. Foreign key from table2 is column in Table1:
Table2 table2;
I am trying to display data from Table1 and some columns from Table2+foreign key. My problem is how to display foreign key, here is part of code:
List<Table1> list = new List<Table1>();
Table t2 = new Table2();
t2.prop1 = done using OledBReader;
t2.prop2 = ...;
t2.prop3 = ...;
Table1 t1 = new Table1();
t1.prop1 = ...;
t1.prop2 = ...;
*t1.Table2 = t2;*
list.Add(t1);
Two properties of Table2 are name and surname so I am overriding toString(), so that one column will be Name Surname. Foreign key is ID, but I dont know how to display It as a column (I dont want to include it in override method). My problem is that instead of that ID which is foreign key, I have property which is entire object of class Table2, and in Table2 I have to override name and surname, so I dont know where to include this ID, so I will have ID column at the end?
I hope this explanation is helpful.

Related

Replacing foreign key with values related to the key

I have 4 columns that act as foreign keys and all are connected to one table which is a detail table for it. I have tried everything and can't get it to work. Instead of saying 1 it should say stefa. Those are names which all have their id's in the other table. I am doing it all in Windows forms the code and query builder, after I run it
SELECT
RadniDan.ID, RadniDan.Datum, RadniDan.PrvaSmena, RadniDan.DrugaSmena,
RadniDan.IspomocPrva, RadniDan.IspomocDruga
FROM
RadniDan
INNER JOIN
Radnik ON RadniDan.PrvaSmena = Radnik.ID
AND RadniDan.DrugaSmena = Radnik.ID
AND RadniDan.IspomocPrva = Radnik.ID
AND RadniDan.IspomocDruga = Radnik.ID)
WHERE
(RadniDan.Datum = ?)

Joining multiple tables with one to many relationship

I have 3 tables:
maintable(id, serialno, col3, col4, col5, ..., col10)
table1(t1_id, serialno, t1_type, t1_color)
table2(t2_id, serialno, t2_base, t2_price)
maintable's Primary Key is id and serialno is UNIQUE.
Table1's Primary Key is t1_id and table2's is t2_id.
Table1 and Table2 serialno are Foreign Keys that reference MainTable's serialno.
maintable has a one to many relationship with both table1 and table2.
What I want to do is join these 3 tables in a DataTable.
I first thought that it would be simple and I tried: "SELECT * FROM maintable INNER JOIN table1 ON maintable.serialno = table1.serialno INNER JOIN table2 ON maintable.serialno= table2.serialno WHERE maintable.id = 200";
The problem with the result is that if table1 has 3 rows and table2 has 4 rows then my DataTable becomes 12 rows(3x4). What I want to do in this instance is just get 4 rows.
table1 and table2 columns don't have anything to do with each other and they only have to match maintable's serialno.
In case that I'm not being understood, I want to select the rows of table1 and table2 that match maintable's serialno and add them to the right of maintable without them getting duplicated.
Edit: Sorry, I had written accountno instead of serialno in some cases.
SELECT * FROM
maintable m
INNER JOIN (
SELECT t1.serialno, t1.t1_type, t1.t1_color, null as t2_base, null as t2_price
FROM table1 t1
UNION
SELECT t2.serialno, null as t1_type, null as t1_color, t2.t2_base, t2.t2_price
FROM table2 t2
) t ON m.serialno = t.serialno
ORDER BY m.serialno
This will do what you're asking for: return number of rows in t1 + number of rows in t2, rather than rows in t1 x number of rows in t2. Fiddle. This may not perform so hot if you have a large amount of data.
Now that you know how it's done, don't do it.
The real question is why is this a requirement? What are you really trying to accomplish here? This is not a meaningful way to combine the data from the two child tables, given their relationships. T1 and t2 are different tables and not keyed to each other for a reason: they aren't meant to combine their data like this.
The only new data I can imagine extracting from this kind of query is the total count of rows in both t1 and t2 for a given serial number. But there are much better ways to get this information than selecting the rows like this. If you need both t1 and t2 data and duplicates are throwing you off, odds are good that you should be making two separate SELECT statements instead of trying to combine everything.
SELECT
maintable.accountno
FROM
maintable
INNER JOIN table1 ON
maintable.accountno = table1.accountno
INNER JOIN table2 ON
maintable.accountno = table2.accountno
WHERE
maintable.id = 200
GROUP BY
maintable.accountno

fix data property name in gridview

I have 2 table :
table1: name, family, cityID
table2: cityID, cityName
I have a gridview that it's datasource is:
select * from table1, table2 where table1.cityID = table2.cityID
I disable autoCreatColumns
I add 3 columns to my datagrid.
in first column I fix the data property name to name
in second column I fix the data property name to family
but in third colum when I fix the data propery name to table2.cityName It dosent work...
I want to show the city name in third column.
Its an example but in fact, I have cityName in anouther table that I used, and if I use just cityName, makes error
can any body help me?
Thanks...
try to change
select * from table1, table2 where table1.cityID = table2.cityID
To
select table1.name as Name,table1.family as Family,table2.cityName as City from table1, table2 where table1.cityID = table2.cityID
SELECT name,family,cityName from table1 INNER JOIN table2 ON table1.cityID = table2.cityID
As you are doing inner joins with city ids on both tables..U will get matched Table IDs with name,citynames
You don't need to write table2.cityName, just write cityName and it should work.

How do you include all fields from first table in join, and some from remaining?

Given the following:
var item = (from table1 in Entity.table1
join table2 in Entity.table2
on table1.ID equals table2.fkId
where table1.ID == TheID
select table1
)
How can I return all the fields from table1, and then add a few from table2 without having to explicitly define all the columns again like this:
where table1.ID == TheID
select new
{
table1.field,
table1.field2,
etc, etc,etc,etc,
table2.field1
}
How can I return all the fields from table1, and then add a few from
table2 without having to explicitly define all the columns
You can't. All properties of anonymous object should be specified. But you can return whole instance from table1 and some fields from table2:
select new
{
table1
table2.field1
}
Then all properties from first table will be available via x.table1.field2 and you would not list them all manually.
You can keep both table1, and table2 instead
where table1.ID == TheID
select new
{
table1,
table2
}
and access to the fields of this anonymous class by by myItem.table1.field
If however you want to access by first-level properties, you can create dynamic class, that returns proper value by reflection from one or another table, or implement ICustomTypeDescriptor interface if you want to show table on UI(both Winform and WPF grids recognize this interface)

Delete row from two sql tables that join together

There are two tables:
Table1 : UserID Name Job
Table2 : BookID Book Car UserID
I load these two tables in one wpf datagrid:
da.SelectCommand = new SqlCommand("select Table1.UserID, Table1.Name, Table1.Job, Table2.Book, Table2.Car from Table1 inner join Table2 on Table1.UserID = Table2.UserID");
I want to delete one row from Table2 by DataGrid:
SqlCommand com = new SqlCommand("delete from Table2 where BookID=#BookID)",con);
but not work,
How can I do it?
You say "It must delete a book from a certain user not all books.".
You have to know user id for which the books must be delete.
If you want to delete users books, do this:
delete from Table2 where userid in (user_id1,user_id1, etc .....);
But you are large rows to delete, use bulk delete mechanism.
If you don't have user ids and have book ids, you have to do this:
Delete From Table2 where bookid in (book_id1,book_id2, etc ..);
Or Delete From Table2 where bookid =? //according your development language, you set "?" parameter. I don't know C# syntaxe.
You have to use the below way to delete the rows
DELETE FROM table2
WHERE userid = (SELECT userid
FROM table1);
Are you expecting something like this?
DELETE FROM B WHERE BOOKID IN (SELECT BOOKID FROM B,A WHERE B.USERID=A.USERID AND B.BOOK='ABCD');

Categories