how to display the items in the combobox in any given manner? - c#

I have a dropdown getting populated by fetching a query on the database.
Say it fetches items given as follows:
Teacher,
Student,
Pricipal,
Sweeper,
FinanceManager.
While showing it in the dropdown, I wish to show it in the following order:
Principal,
FinanceManager,
Teacher,
Student,
Sweeper.
This isn't any specific order (ascending or descending), but just a order that has some relevance according to the personalities.
How do I acheive ?

Since you fetching the data from database,the easy solution to me is adding a extra column to the same table which hold order.
eg. Your Table
Column1 Column2 .... DisplayOrder(int)
Principal 1
FinanceManager 2
etc...
So you can order by the DisplayOrder when you do the selection

you could give them an extra "role-flag" in your db
ID NAME FLAG
1 Principal 5
3 FinanceM 4
33 Teacher 3
22 Student 2
99 Sweeper 1
and you could do the query by "order by FLAG"

Related

How can I display a set of data from 2 database tables? Winforms

I created a Winforms app and a database with 2 tables in Visual Studio:
Member - holds details like first name
Membership - holds a membership type basic, VIP.
I want to display membership type from the Membership table and the FirstName from the Member table.
I have added the foreign key for the membership table to Member. Now I try to create the view as below. While this works, when I show results of the view , it creates all the requested data 3 times:
MemberID FirstName Type
-------------------------------
1 Tommy Basic
2 Sammy VIP
3 Alley Basic
1 Tommy Basic
2 Sammy VIP
3 Alley Basic
1 Tommy Basic
2 Sammy VIP
3 Alley Basic
The code for the view:
CREATE VIEW [dbo].[Memberdetails]
AS
SELECT Member.MemberId, Member.FirstName, Membership.Type
FROM [Member], [Membership]
Not sure how to fix it just to display it once.
You need add field MembershipId to Member table.
You can use where clause
CREATE VIEW [dbo].[Memberdetails]
AS
SELECT Member.MemberId, Member.FirstName, Membership.Type
FROM [Member], [Membership]
WHERE [MemberShip].[Id] = [Member].[MembershipId]
Or use join clause
CREATE VIEW [dbo].[Memberdetails]
AS
SELECT Member.MemberId, Member.FirstName, Membership.Type
FROM [Member]
JOIN [Membership] ON [Membership].[Id] = [Member].[MembershipId]

Updating Checked ListBox values in Database

Lets say I have a DataBase table that associates an Id with multiple items.
Say ID with favorite food.
So it looks like
Id FoodId
----------
1 1
1 54
1 543
2 42
2 4234
etc
I can select/ unselect favorite food values using my checked list box
When I update the db after I edit the items, do I go back and delete all previous entries in the table with Id 1 and then enter the whole new set or is there a better way of doing this?
The easiest way would be your approach. But it is also the slowest and generates redundant database operations. The correct way would be to have the old and the new list in memory creating delete, insert statements accordingly.
Compare the two lists and remove all identical values from both lists.
Create DELETEstatements for all remaining values in the 'old' list.
Create INSERTstatements for all remaining values in the 'new' list.
DELETE
FROM favfood
WHERE id=#Id and foodid not in(new_list_of_food_ids)
// serialized new_list_of_food_ids
INSERT INTO favfood(id,foodid)
(SELECT #Id, [int]
FROM OPENXML (#idoc, '/ArrayOfInt/int',1)
WITH ([int] int '.')
where [int] not in (select foodid from favfood where id=#Id))

insert data into two table depending on the id from another table

I have table called customers contain custID,custName, another table called products contain proID,proName,price and third table Cust_PRo contain id,custID,proID.
i filled the products with data like this:
proID proName price
1 potato 100
2 cotton 600
3 rice 200
and in another form i have combobox i filled it with products names and textbox to write the customer name and gridview to appear data and there are two buttons one to add the values into grid and second to save it into database.
when i write the customer name and choose the products the data appear in the grid like this:
custName ProName
john potato
john cotton
john rice
as you see one customer can take many products the problem is to add the values into database i want when the user click the save button the data insert into the database like this:
first customers table:
custID custName
1 john
second Cust_PRo table:
id custID ProID
1 1 1
2 1 2
3 1 3
thank you
i'm not sure i got you 100% but i think you need some algorithm or something.. anyway
first, you need to generate a CustID for your new customer, to do that you need to get the maximum CustID from the table Customers and add it to 1 to avoid Primary key violation.
select max(CustID) from Customers
then, you'll be good to go. You have (CustID and ProdID from comboBox1.SelectedValue;)
I added the values to datatable and then used the SqlBulkCopy class to insert these values into database

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.

How to use in my application a SELECT statement that must return more than one record to show multiple values for a certain field (m:m relation)

I don't know if you got what I mean, but I'll try to explain it with an example.
Users Table
UsedId UserName
-------- ----------
1 Mike
2 Raul
HasPrivileges Table
UsedId PrivilegeId
-------- --------------
1 1
1 2
1 3
2 2
2 3
Privileges Table
PrivilegeId Privilege
------------- ------------
1 Create
2 Edit
3 Delete
now this is two tables users and privileges that has a many-to-many relation between them, so when I select all the users associated with the privileges they have, I get the in this examples 3 records or rows in result for Mike each one contains a privilege he has.
Now I need in my application to display a list of all the users with their privileges but INDEED i don't want my users to see a user three times to show all of his privileges or anything else instead I want it to display
User Id : 1
Name : Mike
Privileges : Create, Edit, Delete
or something close to this! ANY IDEAS GUYS !??
Ikashef, as Tomalak said, suppressing the repeating name from each of the name/permissions rows is a "presentation-layer" issue, i.e. how you display data to your users.
What you want to do is look at ADO.NET DataTable to get these rows back:
Joe 1
Joe 7
Joe 8
Tom 3
Tom 7
Tom 8
The DataTable has a Rows property, which contains a collection of rows. You can iterate over (i.e. visit in turn) each DataRow in the Rows collection. So read up on ADO.NET DataTable object and on collections classes and on the "for each" syntax.
Ok there are 3 points I can identify with this current problem (I've got a similar thing in my own project).
Bitwise
You can virtually eliminate one of your tables by using a bitfield as opposed to a join table. For example, rather than storing the HasPrivilages along with a privileges table.... You can do this:
UsedId PrivilegeId
-------- --------------
1 1
1 2
1 3
2 2
2 3
Could equate to:
UsedId PrivilegeId
-------- --------------
1 7 (equivalent of Create, Edit and Delete)
2 6 (equivalent of Create and Delete)
This is because Create = 1, Edit = 2 and Delete = 4. Combined, they form a single integer number. This can be differentiated using Bitwise operations, like & and | to produce combinations of permissions.
You'd declare your set of permissions, with the Flags attribute like
[Flags()]
public enum Permissions {
Create = 1,
Edit = 2,
Delete = 4
}
When you read the value back, the enum will calculate the actual permissions for you, and you can work it out in your application by doing an operation such as:
bool canEdit = ((myUser.Permissions & Permissions.Edit) == Permissions.Edit);
If you have the appropriate Permissions enum, doing a .ToString() on that given instance will actually give you the permissions data you require. It is however preferable to give the enum an custom attribute so you can give each value a better name, or even make it language independent from a resource.
Formatting for presentation
You can of course stick with what you've got, and use the example Tim has given. Iterate over the rows and essentially precalculate the text.
Do it in SQL
Sometimes it's just easier to get SQL to do the work. I've done this a lot. If you're just getting DataTables back as opposed to reading them manually or using LINQ, this is a quick fix.
If you're using SQL Server 2005 or above, you can use code similar to:
SELECT u.UserId,
u.UserName AS [Name],
(
SELECT DISTINCT Privilege + ', '
FROM Privileges p2
INNER JOIN HasPrivileges j ON j.PrivilegeId = p2.PrivilegeId
WHERE j.UserId = u.UserId
FOR XML PATH ('')
) AS [Privileges]
FROM Users u
INNER JOIN HasPrivileges h ON h.UserId = u.UserId
GROUP BY u.UserId, UserName
This outputs:
UserId Name Privileges
------- ----- -----------
1 Mike Create, Delete, Edit,
2 Paul Delete, Edit,
This still isn't perfect. You'd have to load this into a temp table and strip the final "," char off the end of each Privileges column, or do it within your C# code.
Anyway just thought I'd offer some alternatives, Tom.
You have ASP.NET and C# in your tags. Considering that what you intend to do is a presentational issue, do it in the presentation layer (i.e. with C#) and not in the data layer (i.e. with SQL). That's a lot easier, too.
For example, like shown here: Use LINQ to concatenate multiple rows into single row (CSV property)

Categories