Sql Join query for treeview - c#

Hi, I have 3 Tables in my database:
I want show the select Query from this table in treeView. I use RadTreeView, to show in treeview. I need to query by these Fields: DisplyMember | ParentMember | ChildMember.
How do I join theses tables for this query?

Your datasource would be your datagrid or list of whatever you are using to display the table. The display member would be whatever you wanted so say group name for this one. The parent member would be Groups. Value member would be groupid. I'm not real familiar with radtree view but I think this may help. Check this out too. It may help you http://www.telerik.com/help/winforms/treeview-data-binding-binding-to-object-relational-data.html

Related

SELECT DISTINCT isn't querying me DISTINCT values

I'm using this query in order to get some records from a MSSQL database and fill a combobox.
SELECT DISTINCT
ta.Marca,
ta.IDTipAutocar
FROM TipAutocar ta
INNER JOIN Autocare a
ON ta.idtipautocar = a.idtipautocar
In table Marca from the database, I have multiple names (eg. Mercedes - appears several times) and when I open my form and dropdown the combo list, I see all the values from database, including duplicates. Any ideas?
You are going to get all DISTINCT combinations of Marca and IDTipAutocar, so if you have multiple tips for Mercedes you will see it multiple times.
If you show an example data set and the desired result we can suggest how best to achieve.

How to Query 'In clause' upon collection of Collection in Linq

I have a list of Ids and I want to fire a 'In Clause' query on nested collection.How would i do that.
In more detail I have 2 tables
Table : A
Table : B
These both tables have their respective primary keys,they are using another "table C" to store their mapping details.
Table : C
Table C contains two columns both are the primary keys of respective table A and Table B.
Now I have Ids from Table A, and get all the records from B which are associated with A using Table C. I want to fire an 'In Query' just like Sql in Linq but not be able to fire on collections of Table C.
I know in linq we have WhereIn clause as well. but its not showing in the list of it.
My query is something like that.
Context.B.Where(item=>item.C.WhereIn(item1=>Item1.AID==aid));
I want something like that query but with 'C' its not showing the wherein clause as well what should i do any suggestion.
Try this one:
// Select the ids of the table B that are associated with ids in aIds.
var bIds = Context.C.Where(x=>aIds.Contains(x.aId)).Select(x=>x.bId);
// Get the record of table B, which their ids are in bIds
var result = Context.B.Where(x=>bIds.Contains(x.Id));

How to get values from 'distinct' column?

This might be simple one and also this has been answered before but I don't know how to look for the exact method.
-> Am having two list box in my form. One is for product name and other one is for Company name.
-> On page load, Listbox1 will retrieve values from database(Product name). Once item has been selected from listbox1, the respective company names should be fetched in listbox2.
For eg : Database name is Motor
Here is my table named as "Register" and it contains two columns they are,
Productname Companyname
Car Bmw
Bike Bmw
Car Honda
Bike Honda
My Question is
I retrieved product name details into listbox1, here the thing is i don't want to repeat the same items, so I used Distinct like this,
Select Distinct Productname from Register
Now if i select car from listbox 1 then the respective company names should be display in listbox2. But what am getting is only Honda on my listbox2 and am not getting BMW.
I can guess these things are happening only because of 'Distinct'. But i don't know exactly how to do this. Hope am not confusing you.So any help would be more helpful to me and thanks in advance.
Update
select Companyname from Register where Productname='"+listbox1.selecteditem+"'
This is the query which is used to retrieve values into listbox2. I used datareader to read and get the values.
Few things to my knowledge:
i) May be you are using Datareader(1) instead of start with datareader (0).
ii) try using datatable and check if you are getting all the values or not in the datatable object.
iii) Below code did work for me i tried placing both the columns(Product and company) in same table and with the below query..
"Select Distinct(Companyname) Company from DatabaseTable where Productname = '"+Listbox1.selectedItem.text+"'";
ive got the output as you needed..
Assuming that you have a table called database! with the structure of (Productname, Companyname)
Query to fetch Product names (on page load) is
Select Distinct Productname from database
When Selected Item changed on Listbox1 you need to run query bellow to fetch Company names and populate
Listbox2:
Select Distinct Companyname from database where UPPER(Productname) = UPPER(Listbox1.selectedItem)
You need to investigate what is Listbox1.selectedItem if you are getting some wired result.

Dynamic query and dynamic select

I need to design set of classes that will receive
Input params and will build dynamic where condition and select . Entity framework will allow to build dynamic where condition but have a problem to dynamically select fields .
Please advice other frameworks that will allow requirement above and will return querable objects by Linq .
For example I have several tables
First table name table1
Fied Column1
Second table. Table2
Field. Column2
And so on . I would like to have view defined that will join all tables . I would like to query view
And dynamically choose columns .
You would want to use Entity SQL. This allows you to build queries as a string.

ASP.NET filter results with checkboxes

I have a list of about 20 products, each with up to 30 possible attributes. I’m trying to figure out the best way to use checkboxes (representing the 30 possible attributes) on a form to filter the products, so that only products with the matching attributes would be shown. I can use SQL Server 2005, but it seems like that might be overkill. Any suggestions?
(Additional) Edit: Ok, given the data structure below, how would you query the database to return products that have ALL of the matching features? Say Product #1 has features 1, 2 and 3. Product # 2 has features 2, 3 and 4. A query for features 1 and 3 should return Product #1, but not Product #2.
Products table
productID int
productname nvarchar(50)
Features table
featureID int
featurename nvarchar(50)
FeatureMap table
featuremapID int
productID_fk int
featureID_fk int
First you would gather the properties that the user selected from your UI
List<string> filter = this.GetAttributeFilterFromUI();
string featureNamesParam = "(" + string.Join(",", filter) + ")";
Then you should be able to use a parameterized query, whose SQL would be something like:
SELECT ProductID FROM Products P
JOIN FeatureMap FM ON FM.ProductID = P.ProductID
WHERE FM.featureID_fk IN
(SELECT FeatureID FROM Features WHERE featurename IN #FeatureNames)
The database would work fine for this. If the attributes for your products is fixed, you can create a single table with your product Id/Name and each attribute would be in a column with boolean/bit types named "IsGreen", "IsBlue", "HasWarrant", etc. If you think atrributes may be added or removed over time, you'll want to setup an "Attribute", "Product" and "ProductAttribute" tables to match up the products and attributes accordingly.
You can then setup your ASP.Net web page to use a CheckBoxList control that has all of the options available. You can then go through the list of check box items in this list to determine which values have been selected and setup your database query from there.
If you want a "dynamic" approach to your search results, set the AutoPostBack property of the CheckBoxList to "true" and then every time a check box is selected or deselected, the SelectedIndexChanged will be triggered and you can update your search results accordingly. This way the user doesn't have to press a "search" button every time.
Hi SQL Server is not overkilling, you can use it very easily to create the Product table and insert the data. After you tell us the schema you have created for your table we could see how to handle this.

Categories