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'
Related
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
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.
I have a grid view that shows to user all baskets that he or she buy in my web. My grid has check box in each row that user can select several basket to print them(Print its items in it.) When user check the check box and click the button to print them , my code check all rows in grid view and save the basket ID in a list and save it in session as list and in print page I can use it. But my problem is here : In print Page I define a DataTable with one column and fill it with basket ID. But I don't know how to join my select query with this data table
this is my query:
sqlQuery="SELECT sale.basketid as id ,sale.discount,ss.LBL as RowNo,sale.ColumnNo,Sale.ID as serial,Sale.Qty "
+ " FROM Sale INNER JOIN SansDate ON Sale.SID = SansDate.SID AND Sale.SansNumber = SansDate.SansNumber"
+ " INNER JOIN Sect ON Sale.SID = Sect.SID INNER JOIN Saloon ON Sect.SaloonID = Saloon.SaloonID left outer join saloonstructure ss on ss.saloonid=saloon.saloonid and ss.rownum=sale.rowno "
+ "where sale.basketid=" + hash.decode(Request.QueryString["param"].ToString()));
and this is my DataTable :
List<string> arraybasket = (List<string>)Session["basketArray"];
DataTable dt = new DataTable();
dt.Columns.Add("bid");
DataRow r = dt.NewRow();
for (int i = 0; i < arraybasket.Count; i++)
{
r["bid"] = arraybasket[i];
dt.Rows.Add(r);
}
in where cluse i just want to show the items that have these Basket ID in my data table (dt)
in where cluse i just want to show the items that have these Basket ID in my data table (dt)
You could use IN operator of SQL.
No need to create a data table to get strings from List<string>. Directly get basketid from List<string> using string.Join;
List<string> arraybasket = (List<string>)Session["basketArray"];
sqlQuery=#"SELECT
sale.basketid as id ,
sale.discount,
ss.LBL as RowNo,
sale.ColumnNo,
Sale.ID as serial,
Sale.Qty
FROM Sale
INNER JOIN SansDate ON Sale.SID = SansDate.SID AND Sale.SansNumber = SansDate.SansNumber
INNER JOIN Sect ON Sale.SID = Sect.SID
INNER JOIN Saloon ON Sect.SaloonID = Saloon.SaloonID
LEFT OUTER JOIN saloonstructure ss on ss.saloonid=saloon.saloonid AND ss.rownum=sale.rowno
WHERE sale.basketid in (" + string.Join(",", arraybasket) + ");";
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
string sql1 = "select jname, jcode
from heardt,judge,main
where heardt.jud1 = judge.jcode and main.fil_no=heardt.fil_no and ..
main.fil_no= ";
I have a form in which user enters a reg_no on entering reg_no only name and address is displayed, in the same table there is fil_no which i want to be used in my above query to link to another table.
how can i specify it in my above query,in the third AND condition? please guide me.
You are talking about JOINing tables I believe? Joins are very simple and are written as thus :-
SELECT t1.column1,t1,column2,t2.column1,t2.column2
FROM table1 t1
JOIN table2 t2
ON t1.key1 = t2.key1
WHERE .....
You can join as many tables as you require and have multiple JOIN types.
See more here :-
http://msdn.microsoft.com/en-us/library/ms191472.aspx