I have done some updates with linq without problem. but the way my code is, I'm having trouble. What's the rule:
1) I have a screen where I make some report requests. Soon I can have n requests in progress.
2) These requests, I fill in some fields, except two fields: DT_GERACAO
and BL_RELATORIO
. BL_RELATORIO should contain a binary file (LOB-Oracle field), which is the report itself (A byte array generated in my application).
3) Then when I call the report, it should fetch the first record it finds and populate those two null
fields, where DT_GERACAO
receives DateTime.Now()
and BL_RELATORIO
receives the generated binary file, however only one at a time, that is, each time I run the report, it searches the table in question and sees if the two fields are null
and then population. The application that generates the report, is an application console and I have no context in it, just dataset. See the code below the report generator.
public static void Emitir()
{
//Relatório com DataSource = ORACLE
dsPlanoMedico.PLANO_MEDICODataTable dtPlanoMedico = new dsPlanoMedico.PLANO_MEDICODataTable();
dsPlanoMedico.POC_SOLIC_RELATORIODataTable dtSolicRel = new dsPlanoMedico.POC_SOLIC_RELATORIODataTable();
dsPlanoMedicoTableAdapters.PLANO_MEDICOTableAdapter adapt = new dsPlanoMedicoTableAdapters.PLANO_MEDICOTableAdapter();
dsPlanoMedicoTableAdapters.POC_SOLIC_RELATORIOTableAdapter solic_adapt = new dsPlanoMedicoTableAdapters.POC_SOLIC_RELATORIOTableAdapter();
adapt.Fill(dtPlanoMedico);
solic_adapt.Fill(dtSolicRel);
dtSolicRel.Where(p => p.DT_GERACAO == null && p.BL_RELATORIO == null).First();
//dtPlanoMedico.Where(i => i.IND_REGULAMENTADO == "S");
var dv = new System.Data.DataView(dtPlanoMedico);
dv.RowFilter = "IND_REGULAMENTADO LIKE 'N' and TIPO_REGISTRO_ANS LIKE 'D'";
//dv.RowFilter = "TIPO_REGISTRO_ANS LIKE 'D'";
ReportDataSource rds = new ReportDataSource("dsDados", dv);
ReportViewer viewer = new ReportViewer();
viewer.ProcessingMode = ProcessingMode.Local;
viewer.LocalReport.ReportPath = "ReportBD.rdlc";
//viewer.LocalReport.SetParameters(new ReportParameter("Regulamentado", "S"));
viewer.LocalReport.DataSources.Add(rds);
Warning[] warnings;
string[] streamIds;
string mimeType = string.Empty;
string encoding = string.Empty;
string extension = string.Empty;
byte[] bytesPDF = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
FileStream fsPDF = new FileStream("c:\Relatemp\report.pdf", FileMode.Create);
fsPDF.Write(bytesPDF, 0, bytesPDF.Length);
fsPDF.Close();
fsPDF.Dispose();
byte[] bytesExcel = viewer.LocalReport.Render("Excel", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
FileStream fsExcel = new FileStream("c:\Relatemp\report.xls", FileMode.Create);
fsExcel.Write(bytesExcel, 0, bytesExcel.Length);
fsExcel.Close();
fsExcel.Dispose();
byte[] bytesWord = viewer.LocalReport.Render("Word", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
FileStream fsWord = new FileStream("c:\Relatemp\report.doc", FileMode.Create);
fsWord.Write(bytesWord, 0, bytesWord.Length);
fsWord.Close();
fsWord.Dispose();
if(dtSolicRel.Count > 0)
{
var query = (from qr in dtSolicRel
select qr).ToList();
}
}
I tried to make these lines in my update, but it is not working. I can not bring the fields in the var query to update:
if(dtSolicRel.Count > 0)
{
var query = (from qr in dtSolicRel
select qr).ToList();
}
bytesPDF is the binary that should be written to the table. The table is named: POC_SOLIC_RELATORIO
. The dataset is named: dsPlanoMedico
and it already contains all the necessary tables (4). We use ReportViewer.
This is the condition for the update:
dtSolicRel.Where(p => p.DT_GERACAO == null && p.BL_RELATORIO == null).First();
I have not tested it yet. I've done this, but I'm wondering how to write to the DB, as I have no context and I'm wondering how to do it with DataSet.
dtSolicRel.Where(p => p.DT_GERACAO == null && p.BL_RELATORIO == null).First();
if (dtSolicRel.Count > 0 && bytesPDF.Length > 0)
{
var query = dtSolicRel.AsEnumerable().Where(x => x.DT_GERACAO == null && x.BL_RELATORIO == null).First();
query.BL_RELATORIO = bytesPDF;
query.DT_GERACAO = DateTime.Now;
}
EDIT BELOW
I'm doing this and I'm going to test it. What I found strange is that it did not accept the DateTime.Now (), but like this: DateTime.Now.
var query = dtSolicRel.AsEnumerable()
.Where(x => x.DT_GERACAO == null && x.BL_RELATORIO == null).First();
query.BL_RELATORIO = bytesPDF;
query.DT_GERACAO = DateTime.Now;
solic_adapt.Update(query);
I tried to do this, but it is giving error due to test null, but I see no other way.
var query = (from i in dtSolicRel
//where i.DT_GERACAO == null && i.BL_RELATORIO == null
select i).ToList().First();
if(query.BL_RELATORIO == null && query.DT_GERACAO == null)
{
query.BL_RELATORIO = bytesPDF;
query.DT_GERACAO = DateTime.Now;
solic_adapt.Update(query);
}