How to get values from 'distinct' column? - c#

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.

Related

How do you put multiple data items with own unique key on another unique key?

Im making an ordering system where there is a product,supplier and order table. What I'm trying to do is when you order, it can have multiple product and one supplier. Example us OrderID 001 it can have 3 products from product table and 1 supplier from supplier table. How can I do this?
Sorry for asking too much but I don't have a code yet for this part of the system as I don't know where to begin. Thank you.
Create an Orders Table.
Add all ordered products to the order Table.
In the table the three products would all have the same order_id, but a different or (in case someone bought two of the same) same product. You will also need to track the amount they purchase with each row, in case the amount changes.
Select Sum(purchase_amount) from orders where order_id = "YOUR_ORDER_ID"
Select * from orders where order_id = "YOUR_ORDER_ID"
...3 rows show up
You may want to have an order_summary table as well that contains the total amount, etc.

How to get distinct values from a table for a particular column?

I had a problem in retrieving the Distinct values from my MySQL DB from a table from a particular column. I have a table in which i have repeated values, so while retrieving the distinct values am not getting distinct as am trying to retrieve the ID and Marks field.
Here is the table
So here am trying to get the distinct values from Total Marks into a dropdownlist box using Asp.net and i need to get student id also for further process.
I am trying with a query as below:
Select Distinct TotalMarks,Student_id from studenttable;
Is this correct query?
I need to get the output in dropdownlist as distinct values of TotalMarks and ID.
write this way:
Select Distinct TotalMarks,Student_id from studenttable group by Stuident_id;
The question is, if student 1 and student 2 have total marks 80. And you want to fetch only one of them (distinct total marks), which one is it?
Anyway the query would look like this:
SELECT TotalMarks, MIN(Student_id) AS Student_id FROM studenttable GROUP BY TotalMarks;
This query would fetch 1st student with such Total marks from each group. If you want the last one just change the aggregate function to MAX
Try this query
SELECT MAX(Student_id),TotalMarks
FROM studenttable
GROUP BY TotalMarks

Sql Join query for treeview

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

How to add column of SUM (in data grid view) to sum data from releation of other data table?

Ok so I have two tables.
First one is "Service" which contains information about Car, date, employee working on car, and so on.
2nd table is "service journal". It contains this:
ID ID_Service Text Price
1 1 blabla 50
2 1 gfdgfd 75
Both tables have they own data grid view.
I want to add column "Sum" at first datagrid, which shows sum of all prices. For this example it would be 75.
How can I do that?
If "Service journal" means "price list", try to write this query:
SELECT ..., SUM([Price]) FROM xyz WHERE ID_Service=x OR ID_Service=y
And than use Eval in column settings and than you'll have generated new column whitch will be named as "Columnx" where x equals index of column in query.
You must use WHERE...OR... for selecting all things that have been done on car.
Or if "Service journal" means list of all repairs done on all cars whitch have ever been done in service, try to write this query:
SELECT ..., SUM([Price]) FROM Service_journal WHERE ID_Service=CarServiceNumber
This SQL query will get summary of prices of all services/repairs whitch have been done. This query is based on understanding of ID_Service in that it means id whitch is common for all repairs done on one car.
Bit unclear question. Hope this helps.

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