Replacing a column of DataGridView - c#

I have a datebase and a DataGridView that i bind by the request SELECT idUser, nameUser, idFunction FROM dbo.Users.
table_Users {idUser, nameUser, idFunction}
table_Functions {idFunction, nameFunction}
How can I replace the colum "idFunction" of the grid view by "nameFunction"?

You want to use a join linking the two tables based on idFunction
SELECT idUser, nameUser, f.nameFunction FROM dbo.Users u
Inner Join table_Functions f on f.idFunction = u.idFunction
More information on joins can be found here

Related

Is that possible to return a whole join table data to datagridview instead of specifying the item one by one?

I am trying to get all data from the join table to data review, but what I learn is just to specify the attributes one by one in the SELECT statement as following code:
var query = (from inv in db.tblinventories
join sup in db.tblsuppliers on inv.SupplierID equals sup.SupID into left
from sup in left.DefaultIfEmpty()
select new { inv.Invnum,inv.Model, sup.name }).ToList();
this.dgvPOS.DataSource = query;```
Is this the only way to fetch all data? Because in ORACLE SQL the way is quite simple by using "*" to represent all attributes like this:
SELECT *
FROM tblinventories
LEFT JOIN tblsuppliers
ON tblinventories.SupplierID = tblsuppliers.SupID;

C# SqlCommandBuilder , CommandUpdate - how to write correct update based on select with outer join tables

I want is to update 2 fields: p.FlagaWaznosci and p.Notatka
My select looks like:
Select DISTINCT p.id,p.Model_Number,p.Product_Name,p.Website_Link,p.Entry_date,p.LastUpdate_date,p.PrzydzialRozmiarow_ID,p.FlagaWaznosci,p.Notatka,pr.NazwaRozmiarowki,wd.LINK_StockX
from Products p with(nolock)
left outer join Widok_Model_Sklep_Stockx_Linki wd with(nolock) on wd.Product_ID = p.id
left outer join PrzydzialRozmiarow pr with(nolock) on pr.id = p.PrzydzialRozmiarow_ID
inner join Shops s with(nolock) on s.ID = p.Shop_ID
There is just outer joins to get correct data that I need to be displayed in gridview. And now when values p.FlagaWaznosci or p.Notatka is changed I want to save update in my database.
I try to use
//loads dataand fill to gridview
DataTable WszystkieProduktyDlaDanegoSklepu;
SqlDataAdapter sda555123 = new SqlDataAdapter("here is my select", conn123);
sda555123.Fill(WszystkieProduktyDlaDanegoSklepu);
//later update table Prooducts and save changed on p.Notatka and p.FlagaWaznosci
cmdbl = new SqlCommandBuilder(sda555123);
cmdbl.ConflictOption = ConflictOption.OverwriteChanges;
sda555123.Update(WszystkieProduktyDlaDanegoSklepu);
But this way I have error
So I searched a lot and found: I have to write own CommandUpdate.
So ... sda555123.UpdateCommand and I don't have idea how can I write own update for it in update command.
The update in SQL Server should looks like:
Update Products
set FlagaWaznosci = #Flagawaznosci from my sda555123,
Notatka = #Notatka from my sda555123
where id = # p.ID from my sda555123
How my command update should looks like here?
EDIT 1 :
i try added : WszystkieProduktyDlaDanegoSklepu.PrimaryKey = new DataColumn[] { WszystkieProduktyDlaDanegoSklepu.Columns["id"] }
but nothing . Still this error.
I would solve the problem by changing the approach instead of mutating the update command of the SqlDataAdapter.
Given that Products.id in your query is unique within the result set:
1- Create a temporary table (local or global), having its columns same as the result of the query with id as primary key.
2- Insert data into the temporary table using your select statement.
3- DataAdatper.selectQuery.commandText is set to "select * from TempTable"
4- The update command is now based on a simple select statement, consequently any change in the datagridview/datatable can be updated to the temptable using dataadapter.update(datatable)
5- As for the final database update, you could use the below statement
Update Prd
set Prd.FlagaWaznosci = TempTable.FlagaWaznosci ,Prd.Notatka = TempTable.Notatka etc.. all the fields that need to be updated
from my Products as Prd
Inner Join TempTable on TempTable.id = Prd.id
Note that the update in (5) will affect all rows, even unchanged ones.
To address this issue you can proceed as below
1- Save changed ids in a list.
List<string> lst = new List<string>();
foreach(DataRow dr in datatable.GetChanges(DataRowState.Modified))
{
lst.add(dr["id"].ToString());
}
2- Convert your list to a string value to be concatenated with the query in (5)
String strchange = String.Join(",",lst); //will give you id1,id2,...
//The update query becomes
Update Prd
set Prd.FlagaWaznosci = TempTable.FlagaWaznosci ,Prd.Notatka =
TempTable.Notatka etc.. all the fields that need to be updated
from my Products as Prd
Inner Join TempTable on TempTable.id = Prd.id
Where Prd.id In ( strchange )
Kindly update your tables separately because in join you just seen two or more than two tables into one table form . but you cant do any crud operation on

C# SELECT statement, SQL query

I am trying to get all the products from the Products table, and at the same time retrieve Company_Name from Company table. A common column in both my table is the Company_Id.
I am using this query:
SELECT
products.product_id,
products.product_name,
products.product_desc,
products.unit_price,
products.stock_level,
products.product_image,
products.gender,
products.type_of_acct,
products.product_cname,
products.product_cdesc,
products.company_id,
company.company_name
FROM
products
INNER JOIN
company ON products.company_id = company.company_id
However this only show all the products from a specific company.
I need to show all the products.
It seems you have an optional relationship here, so use LEFT JOIN:
....
FROM Products
LEFT JOIN Company
ON Products.Company_Id = Company.Company_Id
This retrieves all the products whether linked to a valid company or not.
I think you also need to go over your data and check if you have your foreign keys set up right and have the correct data.
You can use LEFT JOIN to get all details from Products table. LEFT JOIN is fetch all records from left table and also fetch matching records from right table.
SELECT Products.Product_ID, Products.Product_Name, Products.Product_Desc, Products.Unit_Price, Products.Stock_Level, Products.Product_Image, Products.Gender, Products.Type_Of_Acct, Products.Product_CName, Products.Product_CDesc, Products.Company_Id, Company.Company_Name
FROM Products
LEFT JOIN Company ON Products.Company_Id = Company.Company_Id
Left join is best solution for you.
Or you can make one user define function and from there you can retrieve company name like below
SELECT
Products.Product_ID, Products.Product_Name,
Products.Product_Desc, Products.Unit_Price,
Products.Stock_Level, Products.Product_Image,
Products.Gender, Products.Type_Of_Acct,
Products.Product_CName, Products.Product_CDesc,
Products.Company_Id,
dbo.GetCompanyNameFromCompanyID(Products.Company_Id) AS Company_Name
FROM
Products
Try this
SELECT
Products.Product_ID, Products.Product_Name, Products.Product_Desc,
Products.Unit_Price, Products.Stock_Level, Products.Product_Image,
Products.Gender, Products.Type_Of_Acct, Products.Product_CName,
Products.Product_CDesc, Products.Company_Id, Company.Company_Name
FROM
Products
LEFT JOIN
Company ON Products.Company_Id = Company.Company_Id
This will return you all the products, with its linked company if any, a NULL will be shown under Company.Company_Name otherwise

how to write delete query in multiple selection?

I have a gridview in my web project and I connect it with the SqlDataSource. The select command contain fields from 3 table, and I created commandField for gridview so that users can delete their Purchase Items.
here is my C# select code:
SqlDataSource1.SelectCommand =
"SELECT Production.Name, Production.Price ,PurchaseItem.Quantity,
Production.Info ,Purchase.OrderDate
FROM Production
INNER JOIN PurchaseItem on (Production.ID = PurchaseItem.ProductID)
INNER JOIN Purchase on(Purchase.ID = PurchaseItem.PurchaseID)
WHERE Purchase.UserID = '" + Convert.ToInt64(da.getuserid(Session["Mysys"].ToString()).FirstOrDefault()) + "'";
GridView1.DataSourceID = SqlDataSource1.ID;
GridView1.DataBind();
My problem is the DeleteCommand of the SqlDataSource.
is it Possible to do such thing?
Production, PurchaseItem, Purchase are the tables you want to delete the items by selecting on your gridview right? if so.
you just have to make a delete query like
DELETE FROM PRODUCTION WHERE ID = VALUE
DELETE FROM PURCHASEITEM WHERE ID = VALUE
DELETE FROM PURCHASE WHERE ID = VALUE
and if you want to delete running a single query only you may try INNER JOIN:
DELETE table1, table2 FROM table1 INNER JOIN table2
WHERE table1.id=table2.id and table1.id = 'VALUE'

SQL JOIN on three tables using a common table

I have to make a join on 3 three table, the scheme is following:
1. Request_Send table have: RequestID(FK),DonorID(FK),SendRequestID(PK)
2. Donor table have Donor,ID(PK),Name,etc...
3. Blood_Request table have RequestID(PK),etc...
Now I want to make join in which I could select some columns from Donor, some columns from Request. so how could I do this?
my present query is:
string show = "
SELECT Blood_Request.Date,Blood_Request.Time,Blood_Request.R_Name,R_Address,R_Phone
FROM Request_Send INNER JOIN
Blood_Request ON Blood_Request.RequestID=Request_Send.RequestID INNER JOIN
Donor ON Request_Send.DonorID=Donor.DonorID
Where D_Emial='" + Session["UserID"];
Please Help and thanks in advance.
Your problem is Donor.DonorID=Request_Send.DonorID
try this query your problem is solved.
string show ="
SELECT Blood_Request.Date,Blood_Request.Time,Blood_Request.R_Name,R_Address,R_Phone
FROM Request_Send INNER JOIN
Blood_Request ON Blood_Request.RequestID=Request_Send.RequestID INNER JOIN
Donor ON Donor.DonorID=Request_Send.DonorID
WHERE D_Emial='" + Session["UserID"] + "'";
You need to provide talbe name from where D_Emial will select data like
Where Request_Send.D_Emial ='something';
Donor.DonorID doesn't exist. Try Donor.ID in your join
string show = "select bld_req.Date,bld_req.Time,bld_req.R_Name,R_Address,R_Phone FROM Request_Send req_snd INNER JOIN Blood_Request bld_req ON bld_req.RequestID=req_snd.RequestID INNER JOIN Donor dnr ON req_snd.DonorID=dnr.ID Where D_Emial='" + Session["UserID"] + "'";
please make sure D.Emial column is there in the table and call it like : tablename.D_Emial.

Categories

Resources