Textboxes using BindingSource with more tables - c#

On my form there is a listbox containing movie titles from table1 and if you select one from the listbox you can see all the movie datas in textboxes bindingsourced from table1.
However i have another table containing + data about the selected movie but i don"t know how can i set the textbox to that because if i simply select the bindingsource from the table2 it won't recognise the movie somehow i need to connect them ...
Can you help me with a solution?
this is how i added the table1 so when user type in textbox it can filter listbox and select the movie
var h = from s in db.Filmek where s.Filmcim.StartsWith(textBox1.Text) select s;
filmekBindingSource1.DataSource = h;
But i need the connected data from table2 to show in the textbox too

Have you tried a join between the two tables?
First create a class for the result of the two tables joined, something like:
public class MovieResult
{
public int MovieId {get;set;}
public string MovieTitle {get;set;}
//Other properties of table one
//Properties of table two
}
Then modify your query to look something like:
var result = from movie in db.Filmek
where movie.Filmcim.StartsWith(textBox1.Text)
join anotherTable in db.OtherTable on movie.FilmId equals anotherTable.FilmId
selecte new MovieResult
{
MovieId = movie.Id,
MovieTitle = movie.Title,
//Fill the properties of table one
//Fill the properties of table two, ex. if table two contains a field named Country
Country = anotherTable.CountryName //etc
}
Finally bind it to the result:
filmekBindingSource1.DataSource = result;

Related

Join two tables with key in common and show on a datagrid c# linq

I need to join the two tables of a DataGrid, but I only want the values with the same id (idviagem is pk in table idviagem and is fk in table idpassageiro)
I don't know how to do the query, in that moment I only take the table tbpassageiro on the grid, and I want to join them on DataGrid when the keys are equals
using (checkinEntities1 db = new checkinEntities1())
{
var qcheckin = (from c in db.tbpassageiro
join g in db.tbviagem on c.idviagem equals g.idviagem
where c.idviagem == g.idviagem
select c).ToList();
gridpass.ItemsSource = qcheckin;
}
The binding I know 100% is correct (some values from table passageiro and the other Biding values from table tbviagem)
This what I want to do:
If you want columns from both tables, then you have to create a view model for the columns which you want from both tables.
using (checkinEntities1 db = new checkinEntities1())
{
var qcheckin = (from c in db.tbpassageiro
join g in db.tbviagem on c.idviagem equals g.idviagem
where c.idviagem == g.idviagem
select new viewModelName()
{
//get the column values here like
Hora = c.Hora,
Partida = g.Partida
}).ToList();
gridpass.ItemsSource = qcheckin;
}
Hope this will give you the answer you are looking for.

Displaying foreign key on datagridview

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.

Creating a gridview or listview from different sql tables in asp.net

What i aim to achive with this is to have a grid or listview where the colums get data from different sql tabels.
Here are the different tabels
TradeItemIdentification
id GTIN
TradeItemDescriptionInformation
id brandName tradeItemFunctionalName
ClassificationCategory
id additionalClassificationCategoryCode
I don know if it is possible to show a grid with all of these attributes except the id's. What is the smartest way to do achive this, if possible?
They are all linked to this table. With these "many ot many" tabels.
TradeItemBasic
id
TradeItemIdentificationOnTradeItem
tradeItemId identificationId
TradeItemDescriptionInformationsOnTradeItem
tradeItemId descriptionId
And the same for the last table. So they have a connection.
Here is some joins of the tabels that i have made so far.
public List<string> GetAllProductsInfo()
{
var gtins = (from gtinss in _db.TradeItemIdentificationOnTradeItems
join gtin in _db.TradeItemIdentifications on gtinss.tradeItemIdentificationId equals gtin.id
select gtin.gtin);
var brandNames = (from descriptions in _db.TradeItemDescriptionInformationsOnTradeItems
join description in _db.TradeItemDescriptionInformations on descriptions.tradeItemDescriptionInformationId equals description.id
select description.brandName);
var article = (from articleNumbers in _db.ClassificationCategoryOnGDSNTradeItemClassifications
join articleNumber in _db.ClassificationCategories on articleNumbers.gDSNTradeItemClassificationId equals articleNumber.id
select articleNumber.additionalClassificationCategoryCode);
var allInfo = gtins.Concat(brandNames).Concat(article).ToList();
return allInfo;
}
This is what ive got so far, i am no able to get all of the items that i wanted. But when i do it like this the results is not divided in to sections, but instead it is all just gets put out as one long list.
Just join the different tables and select the properties you want to display.
There are two different use cases. If you want to display all lines where some data is missing then use left joins. Else use inner joins (just replace the left join). That reduce your resultset.
Here is an example how to join your tables:
SELECT tib.id, tii.GTIN, tidi.brandName, tidi.tradeItemFunctionalName
FROM TradeItemBasic AS tib
LEFT JOIN TradeItemIdentificationOnTradeItem AS tii2ti ON tib.id = tii2ti.tradeItemId
LEFT JOIN TradeItemIdentification AS tii ON tii.id = tii2ti.identificationId
LEFT JOIN TradeItemDescriptionInformationsOnTradeItem AS tidi2ti ON tib.id = tidi2ti.tradeItemId
LEFT JOIN TradeItemDescriptionInformation AS tidi ON tidi.id = tidi2ti.descriptionId
For the ClassificationCategory you don't post a mapping table, so i remove the selected property.
Hope that helps.
I solved it.
I Made a new C# class. Called ProductInfo that looks like this
public class ProductInfo
{
public ProductInfo()
{
}
public List<string> Gtin { get; set; }
public List<string> BrandName { get; set; }
public List<string> ArticleNr { get; set; }
}
this class takes three types of lists.
And here is the method for getting the specific information from the different classes.
public ProductInfo GetAllProductsInfo()
{
var gtins = (from gtinss in _db.TradeItemIdentificationOnTradeItems
join gtin in _db.TradeItemIdentifications on gtinss.tradeItemIdentificationId equals gtin.id
select gtin.gtin).ToList();
var brandNames = (from descriptions in _db.TradeItemDescriptionInformationsOnTradeItems
join description in _db.TradeItemDescriptionInformations on descriptions.tradeItemDescriptionInformationId equals description.id
select description.brandName).ToList();
var article = (from articleNumbers in _db.ClassificationCategoryOnGDSNTradeItemClassifications
join articleNumber in _db.ClassificationCategories on articleNumbers.gDSNTradeItemClassificationId equals articleNumber.id
select articleNumber.additionalClassificationCategoryCode).ToList();
ProductInfo pr = new ProductInfo { Gtin = gtins, BrandName = brandNames, ArticleNr = article };
return pr;
}
with this method i get three lists from the tabels that i wanted. And after i made the joins i just create a new ProductInfo and add the results to the right lists in the new class.

Retrieve Integer value from string in a Gridview

I have a gridview, when inserting data into the table dept(100, 1, 'IT') and when fetch the data from two tables emp and dept the records look like below grid:
EMpNo DeptNo DeptName
Harshal 1 IT
My problem is that, when I click on a grid view all records shows in respected text boxes
but issue is, I got all data except empno. Here I get as Name of employee instead of name I want to Empno.
you can try like this
select empno,deptno,deptname from emp inner join dept on emp.dept.id=dept.id

getting two column values from same table depending on the condition

I have two tables:
table product1 with columns:
product_id
prodcut_name
category_id
another table categories
category_id
category_name
category_description
I am populating product details using DataGridView and it's working fine.
I am trying to get the two column values from same table depending on the same condition that I have got the code below:
string desc = Convert.ToString(selectedRow.Cells["productdescr"].Value);
string productname = Convert.ToString(selectedRow.Cells["productnam"].Value);
string productprices = Convert.ToString(selectedRow.Cells["productprice"].Value);
int productids = Convert.ToInt32(selectedRow.Cells["productid"].Value);
condition 1:
int categoryids = (from cats in tsg.product1
where cats.product_Name.Equals(productname)
select cats.category_Id).SingleOrDefault();
condition 2:
var catogynames = (from categorytypes in tsg.categories
where categorytypes.category_Id.Equals(categoryids)
select categorytypes.category_Name
).SingleOrDefault();
condition 3:
var categoprydecription = (from categorytable in tsg.categories
where categorytable.category_Id.Equals(categoryids)
select categorytable.category_Description
).SingleOrDefault();
I want to get the categorytypes.category_description also along with this categorytypes.category_Name from the condition 2, is it possible to combine the two conditions? (condition 2 and condition 3)
I think you can do this in this fashion
(from categorytable in tsg.categories
where categorytable.category_Id == categoryids
select new {Name=categorytable.category_Name,
Description=categorytable.category_Description}).SingleOrDefault();
This will be an anonymous class holding both name and description of the categories you want.

Categories