Delete query not working - c#

I am using linq to delete data from database. I googled, and made query. I am using following query.
When i put debugger over this, its not showing any error. Its going fine. But when I check my database , record does not gets delete.
At the same time doing operation for insert:
splitTradeDataContext db = new splitTradeDataContext();
tradeFile tlbTradeFile = new tradeFile();
tlbTradeFile.TradeNo = TradeNo + "_A";
tlbTradeFile.Status = Status;
tlbTradeFile.Scrip_Code = Scrip_Code;
tlbTradeFile.Inst_Type = Inst_Type;
tlbTradeFile.Expirydate = Expirydate;
tlbTradeFile.Strike_price = Strike_price;
tlbTradeFile.Option_type = Option_type;
tlbTradeFile.Sec_Name = Sec_Name;
tlbTradeFile.BookType = BookType;
tlbTradeFile.BookTypeName = BookTypeName;
tlbTradeFile.TerminalId = TerminalId;
tlbTradeFile.Branch_Id = Branch_Id;
tlbTradeFile.Buy_Sell = Buy_Sell;
tlbTradeFile.Trade_Qty = Trade_Qty;
tlbTradeFile.Market_Rate = Market_Rate;
tlbTradeFile.Pro_cli = Pro_cli;
tlbTradeFile.Party_Code = Party_Code + "_A";
tlbTradeFile.ParticipantCode = ParticipantCode;
tlbTradeFile.O_C_Flag = O_C_Flag;
tlbTradeFile.Sauda_Date = Sauda_Date;
tlbTradeFile.TradeModified = TradeModified;
tlbTradeFile.OrderNo = OrderNo;
tlbTradeFile.Opp_Broker_Id = Opp_Broker_Id;
tlbTradeFile.OrderTime = OrderTime;
tlbTradeFile.UnknowkNum = UnknowkNum;
var remove = from aremove in db.tradeFiles
where aremove.ID==int.Parse(gvTradeFile.SelectedRows[0].Cells[0].Value.ToString())
select aremove;
if(remove!=null)
db.tradeFiles.DeleteAllOnSubmit(remove);
What can be problem? Am I using wrong query? Or any thing still remained from me?

You are not calling SubmitChanges anywhere in your code you need to do:
db.SubmitChanges(); //if its LINQ to SQL

Related

I am getting ORA-01008: not all variables bound error while updating

Dont know what I am doing wrong. can anyone help. Not even sure for which parameter I am getting this error.
var sql = #"
UPDATE Test_Tem_Table SET
SOFTCLSD_IND = :SOFTCLSD_IND
, SOFTCLSD_DT = :SOFTCLSD_DT
, SOFTCLSD_BY = :SOFTCLSD_BY
, SOFTCLSD_COMMENT = :SOFTCLSD_COMMENT
, SOFTCLSD_LASTUPDT_BY = :SOFTCLSD_LASTUPDT_BY
, SOFTCLSD_LASTUPDT_DT = :SOFTCLSD_LASTUPDT_DT
WHERE PREFIX_CB_ELIG_ID = :PREFIX_CB_ELIG_ID";
var param = AdoTemplate.CreateDbParameters();
param.Add("SOFTCLSD_IND", System.Data.OracleClient.OracleType.Char).Value = DBUtils.ConvertBoolToYN(currentSoftClose.IsSoftClosed);
param.Add("SOFTCLSD_DT", System.Data.OracleClient.OracleType.DateTime).Value = ((DateTime)currentSoftClose.SoftClosedDate).Date;
param.Add("SOFTCLSD_BY", System.Data.OracleClient.OracleType.VarChar).Value = currentSoftClose.SoftClosedBy;
param.Add("SOFTCLSD_COMMENT", System.Data.OracleClient.OracleType.VarChar).Value = currentSoftClose.Comments;
param.Add("SOFTCLSD_LASTUPDT_BY", System.Data.OracleClient.OracleType.VarChar).Value = currentSoftClose.SoftClosedLastUpdatedBy;
param.Add("SOFTCLSD_LASTUPDT_DT", System.Data.OracleClient.OracleType.DateTime).Value = currentSoftClose.SoftClosedLastUpdatedDate == null ? (DateTime?)null : ((DateTime)currentSoftClose.SoftClosedLastUpdatedDate).Date;
param.Add("PREFIX_CB_ELIG_ID", System.Data.OracleClient.OracleType.Number).Value = currentSoftClose.Id;
return AdoTemplate.ExecuteNonQuery(CommandType.Text, sql, param);

How to insert update delete using entity-framework model in asp.net C# (Not MVC)

I need help on how to INSERT, UPDATE and DELETE data using entity-framework. But it is not working.
Give me an example?
my code Edit:
var maso = Request.Cookies["NguoiDung"].Value;
using (CalibraMainEntities dv = new CalibraMainEntities())
{
Device dev = new Device();
dev.KMH = txtKMH.Text;
dev.Name_TB = txtNameTB.Text;
dev.Model = txtModel.Text;
dev.Serial = txtSerial.Text;
dev.Date_Entry = DateTime.Parse(txtNgayVe.Text);
dev.Main_Place = txtMainPlace.Text;
dev.Calibra_Place = txtCalibraPlace.Text;
dev.Location = txtLocation.Text;
dev.Staff_ID = maso;
dev.Workshop = int.Parse(drWorkshop.SelectedValue);
dev.Status = int.Parse(drStatus.SelectedValue);
dev.Ware_ID = int.Parse(drWareHouse.SelectedValue);
dv.Devices.Attach(dev);
dv.Entry(dev).State = EntityState.Modified;
dv.SaveChanges();
}

How to search Work Order records from Netsuite using api?

I have to fetch Work Order record from NetSuite based on work order number, I can do that using a saved search but would like to do it without it, so as i do not have a to create another saved search record in production. My code is not returning any result, however the search is successful.
here is the code i am using:
TransactionSearch ts = new TransactionSearch();
TransactionSearchBasic tsb = new TransactionSearchBasic();
// work order number
SearchStringField sfTranId = new SearchStringField();
sfTranId.#operator = SearchStringFieldOperator.#is;
sfTranId.searchValue = workorder;
sfTranId.operatorSpecified = true;
// type
SearchEnumMultiSelectField sfType = new SearchEnumMultiSelectField();
sfType.#operator = SearchEnumMultiSelectFieldOperator.anyOf;
sfType.operatorSpecified = true;
sfType.searchValue = new String[] {"_workOrder"};
tsb.tranId = sfTranId;
tsb.type = sfType;
ts.basic = tsb;
tsa3.criteria = ts;
// perform the search
SearchResult res = _service.search(ts);
res.pageSizeSpecified = true;
if (res.status.isSuccess)
{
SearchRow[] searchRows = res.searchRowList;
if (searchRows != null && searchRows.Length >= 1)
{
TransactionSearchRow tranRow = (TransactionSearchRow)searchRows[0];
if (tranRow.basic.internalId != null && tranRow.basic.internalId.Length > 0)
{
woResult = tranRow.basic.internalId[0].searchValue.internalId;
}
}
}
I realized my mistake, basic search returns RecordList and not SearchRowList.

Convert Function Based on TSQL with Function Based with Entity Framework

I have two functions that each return the same list of objects. But, the one that uses TSQL is much faster than the one using Entity Framework and I do not understand why one would be faster than the other. Is it possible to modify my EF function to work as fast as the TSQL one?
Any help will be appreciated. My code is below:
TSQL:
public static List<ChartHist> ListHistory_PureSQL()
{
List<DataRow> listDataRow = null;
string srtQry = #"Select LoginHistoryID,
LoginDuration as LoginDuration_Pass,
0 as LoginDuration_Fail,
LoginDateTime,
LoginLocationID,
LoginUserEmailID,
LoginApplicationID,
LoginEnvironmentID,
ScriptFrequency,
LoginStatus,
Reason
From LoginHistory
Where LoginStatus = 'Pass'
UNION
Select LoginHistoryID,
0 as LoginDuration_Pass,
LoginDuration as LoginDuration_Fail,
LoginDateTime,
LoginLocationID,
LoginUserEmailID,
LoginApplicationID,
LoginEnvironmentID,
ScriptFrequency,
LoginStatus,
Reason
From LoginHistory
Where LoginStatus = 'Fail'";
using (SqlConnection conn = new SqlConnection(Settings.ConnectionString))
{
using (SqlCommand objCommand = new SqlCommand(srtQry, conn))
{
objCommand.CommandType = CommandType.Text;
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter(objCommand);
conn.Open();
adp.Fill(dt);
if (dt != null)
{
listDataRow = dt.AsEnumerable().ToList();
}
}
}
var listChartHist = (from p in listDataRow
select new ChartHist
{
LoginHistoryID = p.Field<Int32>("LoginHistoryID"),
LoginDuration_Pass = p.Field<Int32>("LoginDuration_Pass"),
LoginDuration_Fail = p.Field<Int32>("LoginDuration_Fail"),
LoginDateTime = p.Field<DateTime>("LoginDateTime"),
LoginLocationID = p.Field<Int32>("LoginLocationID"),
LoginUserEmailID = p.Field<Int32>("LoginUserEmailID"),
LoginApplicationID = p.Field<Int32>("LoginApplicationID"),
LoginEnvironmentID = p.Field<Int32>("LoginEnvironmentID"),
ScriptFrequency = p.Field<Int32>("ScriptFrequency"),
LoginStatus = p.Field<String>("LoginStatus"),
Reason = p.Field<String>("Reason")
}).ToList();
return listChartHist;
}
EF:
public static List<ChartHist> ListHistory()
{
using (var db = new LatencyDBContext())
{
var loginHist = (from hist in db.LoginHistories
select new { LoginHistory = hist }).ToList();
//PUT LOGIN HISTORY RECORDS INTO A LOCAL LIST
var listHistory = new List<ChartHist>();
foreach (var item in loginHist)
{
var localHistData = new ChartHist();
localHistData.LoginHistoryID = item.LoginHistory.LoginHistoryID;
//split up the duration for pass and fail values
if (item.LoginHistory.LoginStatus.ToUpper() == "PASS")
{
localHistData.LoginDuration_Pass = Convert.ToDouble(item.LoginHistory.LoginDuration);
localHistData.LoginDuration_Fail = 0;
}
else if (item.LoginHistory.LoginStatus.ToUpper() == "FAIL")
{
localHistData.LoginDuration_Pass = 0;
localHistData.LoginDuration_Fail = Convert.ToDouble(item.LoginHistory.LoginDuration);
}
localHistData.LoginDateTime = item.LoginHistory.LoginDateTime;
localHistData.LoginLocationID = item.LoginHistory.LoginLocationID;
localHistData.LoginUserEmailID = item.LoginHistory.LoginUserEmailID;
localHistData.LoginApplicationID = item.LoginHistory.LoginApplicationID;
localHistData.LoginEnvironmentID = item.LoginHistory.LoginEnvironmentID;
localHistData.LoginStatus = item.LoginHistory.LoginStatus;
localHistData.Reason = item.LoginHistory.Reason;
localHistData.ScriptFrequency = item.LoginHistory.ScriptFrequency;
listHistory.Add(localHistData);
}
return listHistory;
}
}
Of course EF will take longer to execute than a plain old SQL query, and there's very little that you can do about it (except write the most optimal LINQ queries that you can).
There's a very simple reason why this is so. Running a direct SQL command will just send back the data, with no muss and no fuss attached to it, waiting for you to do the data manipulations to get it to the point where it fits nicely into whatever data structure you want it in. Running EF, on the other hand, means that not only does it run the SQL command, but it massages the data for you into objects that you can manipulate right away. That extra action of going through ADO.NET and converting the data into the objects automatically means that it will take longer than just doing the plain SQL query.
On the flip side of that coin, however, EF does provide a very nice and simple way to debug and solve whatever problems you might have from a specific query/function (like by any exceptions thrown).
I can't performance test this, but try this solution instead before you remove EF entirely:
var loginHist = db.LoginHistories.Where(item => item.LoginStatus.ToUpper() == "PASS" || item.LoginStatus.ToUpper() == "FAIL")
.Select(item => new ChartHist()
{
LoginHistoryID = item.LoginHistoryID,
LoginDuration_Pass = item.LoginStatus.ToUpper() == "PASS" ? Convert.ToDouble(item.LoginDuration) : 0,
LoginDuration_Fail = item.LoginStatus.ToUpper() == "FAIL" ? Convert.ToDouble(item.LoginDuration) : 0,
LoginDateTime = item.LoginDateTime,
LoginLocationID = item.LoginLocationID,
LoginUserEmailID = item.LoginUserEmailID,
LoginApplicationID = item.LoginApplicationID,
LoginEnvironmentID = item.LoginEnvironmentID,
LoginStatus = item.LoginStatus,
Reason = item.Reason,
ScriptFrequency = item.ScriptFrequency,
});
return loginHist.ToList();
This is the "correct" way to populate a new object from a select. It will only retrieve the data you care about, and will put it directly into the object, rather than converting it into an object and then converting it again, from one object to another.
Note: I prefer the functional calls to the from / select form, but it'd be correct either way.

LINQ to Entities does not recognize the method 'System.String Content(System.String)'

I read some questions here but they all can be defined as separate variables but I think mine is little bit different:
var sliderRecordList = this._sliderService.GetAllAsQueryable();
var sliderModelList = sliderRecordList.Select(record => new SliderModel()
{
Id = record.Id,
SlideName = record.SlideName,
SlideOrder = record.SlideOrder,
SlideUrl = record.SlideUrl,
SlideImageUrl = Url.Content("~/Content/AhsenSliderImages/" + record.Id + ".jpg"),
Enabled = record.Enabled
});
The problem relies on where Url.Content() method is called. Any suggestion? I tried to remove Url.Content() method and didn't work.
Materalize the query first, then setup the UI concerns.
var sliderRecordList = this._sliderService.GetAllAsQueryable().ToList();
var sliderModelList = sliderRecordList
.Select(record => new SliderModel
{
Id = record.Id,
SlideName = record.SlideName,
SlideOrder = record.SlideOrder,
SlideUrl = record.SlideUrl,
SlideImageUrl = Url.Content("~/Content/AhsenSliderImages/" + record.Id + ".jpg"),
Enabled = record.Enabled
});
.ToList() will execute the query first.
On a side note, consider using AutoMapper to turn the above code into just a couple of lines:
var sliderRecordList = this._sliderService.GetAllAsQueryable().ToList();
var sliderModelList = Mapper.Map<ICollection<SliderRecord>, ICollection<SliderModel>>(sliderRecordList);
URL generation can be done in the View.
This is because the function Url.Content can not translated to its SQL equivalent function. Use AsEnumerable function to the list and give it a try
var sliderModelList = sliderRecordList.AsEnumerable.Select(record => new SliderModel()
{
Id = record.Id,
SlideName = record.SlideName,
SlideOrder = record.SlideOrder,
SlideUrl = record.SlideUrl,
SlideImageUrl = Url.Content("~/Content/AhsenSliderImages/" + record.Id + ".jpg"),
Enabled = record.Enabled
});
Good luck.

Categories