Delete and update a row in gridview having value of multiple tables - asp.net

I want to delete and update a row in Gridview but that grid have values of two tables
I'm using following query for selecting-
SELECT JobMaster.Country, JobMaster.City, JobMaster.State, JobMaster.EmploymentType,
JobMaster.JobTitle, JobMaster.SpecializedArea, JobMaster.Specializedskill, JobMaster.ID,
JobMaster.[Post Assign], RAssign.RecruiterID AS Post_Assign FROM JobMaster
LEFT OUTER JOIN RAssign
ON JobMaster.ID = RAssign.JobID
WHERE (RAssign.RecruiterID = #RecruiterID) OR (#RecruiterID IS NULL)
What statement should I use for Update,Insert and for delete?
(Actually I'm new in coding and I just know to delete from single table but in above statement we are selecting to tables data one is "Jobmaster" and second is "RAssign".
Please help me if possible)

Related

Insert Into Select Case Statement

I am using an Insert Into Select to move a new table into a master table. I need to have either one of two columns [customMessage1] or [customMessage2] in the new table be entered into the master table. The good data in the new table starts 'SO' in either column. [customMessage2] only has a good value if [customMessage1] does not in the imported data.
INSERT INTO [Freight].[dbo].[MASTER]
Select
[transactionType]
,[amount]
,[transactionId]
,CASE
WHEN [customMessage1] LIKE 'SO%'
THEN [customMessage1]
ELSE
[customMessage2]
END
FROM [Freight].[dbo].[NEW]

Remove duplicate records from mysql table

I am trying to delete some duplicate records from mysql table but it was not working.
I am taking help of https://www.javatpoint.com/mysql-delete-duplicate-records
If I will try with example database which is given example it was working fine.
But in my table it was not working.
DELETE S1 FROM employee_attendance AS S1 INNER JOIN employee_attendance AS S2 WHERE S1.DbKey < S2.DbKey AND S1.DivisionDbKey = S2.DivisionDbKey AND S1.Date = S2.Date AND S1.Month = S2.Month AND S1.FinancialYearDbKey = S2.FinancialYearDbKey AND S1.EmployeeDbKey = S2.EmployeeDbKey AND S1.Attendance = S2.Attendance AND S1.InTime = S2.InTime AND S1.OutTime = S2.OutTime AND S1.EmployeeDbKey = 6798 AND S1.Month = '05' AND S1.FinancialYearDbKey = 5;
I am getting error
#1205 - Lock wait timeout exceeded; try restarting transaction
I have tried with another example https://www.geeksforgeeks.org/sql-query-to-delete-duplicate-rows/
DELETE FROM employee_attendance WHERE DbKey NOT IN (SELECT MAX(DbKey) FROM employee_attendance WHERE EmployeeDbKey = 6798 AND Month = '05' AND FinancialYearDbKey = '5' GROUP BY DivisionDbKey,Date,Month,FinancialYearDbKey,EmployeeDbKey,Attendance,InTime,OutTime)
I am getting same error.
#1205 - Lock wait timeout exceeded; try restarting transaction
Any suggestion will be appriciated. Thank you.
I personally think this is a bad practice. You should instead make a (empty) duplicate of the table employee_attendance then define a UNIQUE KEY on that new table that will prevent duplicate entries.
Consider these steps:
Create a duplicate table:
CREATE TABLE employee_attendance_new LIKE employee_attendance;
Add UNIQUE INDEX - now, this is just a simple example. You can add or reduce columns to the unique index but make sure that you drop the existing unique index first then only you re-create:
ALTER TABLE employee_attendance_new
ADD UNIQUE INDEX unq_idx(EmployeeDbKey, date, InTime, OutTime);
Insert the data into the new table using INSERT IGNORE..:
INSERT IGNORE INTO employee_attendance_new
SELECT * FROM employee_attendance;
Check and compare both table. If you're satisfied with the result, rename those tables:
RENAME TABLE employee_attendance TO employee_attendance_old;
RENAME TABLE employee_attendance_new TO employee_attendance;
Now you have the new table with no duplicates and the old table for reference or in case there are some data you need from it.
Fiddle example

Is there a way to find / update a row based on column values instead of the Row ID?

I have a C# app that sends data to Smartsheet. At the moment, it's just inserting rows, based on the following:
Row rowA = new Row.AddRowBuilder(null, true, null, null, null).SetCells(cellsA).Build();
smartsheet.SheetResources.RowResources.AddRows(_SheetID, new Row[] { rowA });
There are two columns that are essentially the keys for the data, and I'd like to be able to update a row if it exists (WHERE field1 = 'key1' and field2 = 'key2'), and if no row exists with those two field values, do a row insert.
Is it possible to do an update / insert like this? Keep in mind, I'm very new to the Smartsheet API - still finding my way...
Thanks much.
You would need to do this manually today. There isn't a currenty way to ask the API to do this for you.
Irwin

Updating data from GridView

I have a Gridview where I join two tables, bringing in the data to be displayed
table 1 sid, schedule, stime, splace, stourid
table 2 tourid, tourname
Basically they are joined stourid to tourid. That is so I can show the tourname in the grid.
Now I want to edit this GridView, what do I need to do, for instance, if the user edits the tourname, that it'll be saved to the database? I assume an Update statement is need, but how does it link to the GridView?
Whats the best and neatest method?
Regards,
Create Store Procedure to Update both tables and call it in your Update Code...
Create Proc UpdateData
#sid int,
#schedule,
#stime,
#splace,
#tourname
as
Begin
declare #tourid int,
select distinct #tourid=tourid from table1 where sid=#sid
begin try
// update tabel1
update tabel2 set tourname=#tourname where tourid =#tourid
end Try
begin Catch
end Catch
End
Using LinqToSQL you can do something like (if there is a FK relation between the tables)
DatabaseDataContext data = new DatabaseDataContext();
Table1 row = data.Table1s.Where(t =>t.ID == selectedID);
row.Table2.tourname = newName;
data.SubmitChanges();
You would then want to rebind the grid to show the new data.

Edit the code of a GridView column asp.net

I've been looking for a way to control the text that appears in a particular column of a GridView.
For example consider a database with two tables Student and Class.
I want to add a column to the GridView which print out all the students in the Database, the column will show the student's class name, how can do it? (I can normally print the ClassId since its a FK in the student table)
I want to add a column to the GridView which print all the classes, the column will count the number of students in each class, how can I do it?
1- You can do that simply with inner join or a stored procedure to get the class name beside all the data you need in your query.
2- More than one way to do what you want:
For example:
you can add a column to your data table (empty column ), and fill it later through using Sum() aggregate function in a query.
DataTable result_dt = DAL_Helper.Return_DataTable(sqlSelect);//your original query
result_dt.Columns.Add("NumberOfStudent");
result_dt.Columns["NumberOfStudent"].DataType = typeof(string);
result_dt.Columns["NumberOfStudent"].MaxLength = 255;
if (result_dt.Rows.Count > 0)
{
for (int i = 0; i < result_dt.Rows.Count; i++)
{
//Here u can fill your new empty column.
}
}
result_dt.AcceptChanges();
After you return your customized data table(as a data source), you can bind it to the grid view.
Another solution : add an empty column to the grid view and in the RowDataBound event of the grid view you can fill this column through some loop and you can use LINQ to help you in getting the summation.

Resources