insert data into two table depending on the id from another table - c#

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

Related

Insert to a table all records from a column from another table with a standard value

I have two tables, shops and products. Table shop with columns shopId (PK) and shopName. Another table Products has columns productId (PK) and productName. Because I have a relationship many-many between two tables I create another table shopProduct with two columns shopId and productId.
For example I have shop1 with id=1 and I have shop2 with id=2, product1 with id=1 and product2 with id=2. and the records of the table shopProduct is 1-1 ,2-2.
But now I want to have a product3 with id=3 that apply to all shops.
I consider in the real problem that i have about 250 shops.
How can implement this without a stored procedure?
I don't want to do of course 250
insert into shopProduct (shopId, productId)
values (#shopId,#productId).
Can I do for loop for example to feed the the shopId value every time?The other value for product id is the same every time.
From my understanding of this question, try this... Seems too simple, but...
Insert into ShipProduct (ProductID, ShopID)
Select 3, ShopID
From Shops

Common record fetch from table in two dropdownlist

i have problem when i want fetch data from sql table(common for both dropdownlist) & i want this way...
our common record is
id city
1 Pune
2 Mumbai
3 Delhi
4 Kolkata
Sourse-ddl1- if we select "Pune"
Destination-ddl2-Here should be come all cities except pune.
both record comes from common table id ,city but no one record should be match in both table.
In the first dropdownlist
select * from City where CityCode!=ddl2.selectedValue
whereas in the second dropdown
select * from City where CityCode!=ddl1.selectedValue

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))

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 display the items in the combobox in any given manner?

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"

Categories