C# Microsoft SQL - c#

I want to write that two strbldrSQL.Append in single strbldrSQL.
Because Im inserting records in 2 DataSets dsorderHeader and dsorderDetail respectively.
if (strbldrSQL != null) strbldrSQL.Clear();
else strbldrSQL = new StringBuilder();
strbldrSQL.Append(#" SELECT ORDHEDR01.* FROM ORDHEDR01 "
+ " WHERE ORDHEDR01.COMPANY = " + argintCompany + " "
+ " AND ORDHEDR01.WHSE = " + argintWarehouse + " "
+ " AND ORDHEDR01.ORDNUM = " + arglngOrderNumber + " "
);
dsOrderHeader = DAL.pfGetDataSet(ref argobjMnCnnCls, strbldrSQL.ToString(), true);
if (dsOrderHeader == null) return CNS.gcUpdateFailed + ";" + "Unable to get data";
strbldrSQL.Clear();
strbldrSQL.Append(#" SELECT ORDDETL02.* FROM ORDDETL02 "
+ " WHERE ORDDETL02.COMPANY = " + argintCompany + " "
+ " AND ORDDETL02.WHSE = " + argintWarehouse + " "
+ " AND ORDDETL02.ORDNUM = " + lngOrderNumber + " "
);
dsOrderDetail = DAL.pfGetDataSet(ref argobjMnCnnCls, strbldrSQL.ToString(), true);
if (dsOrderDetail == null) return CNS.gcUpdateFailed + ";" + "Unable to get data";

Related

Set every element of TextBlockResult to visible when the value is not null

i would like to make each element of textblockresult visible only when the value is not null.
Intent, ROKEntity, ProcedureName etc are strings that are defined as empty and then receive their value from a json :
string intent = "";
if (!string.IsNullOrEmpty(root.XPathSelectElement("//intent").Value))
{
intent = root.XPathSelectElement("//intent").Value;
}
resultToDisplay = "Action: " + intent
+ Environment.NewLine + "Rok Entity: " + ROKEntity
+ Environment.NewLine + "Rok Entity: " + ROKEntity2
+ Environment.NewLine + "Rok Entity: " + ROKEntity3
+ Environment.NewLine + "Procedure Name: " + ProcedureName
+ Environment.NewLine + "Folder Name: " + FolderName
+ Environment.NewLine + "Task Name: " + TaskName
+ Environment.NewLine + "Worker Name: " + WorkerName
+ Environment.NewLine + "Risk Name: " + RiskName
+ Environment.NewLine + "File Name: " + FileName
+ Environment.NewLine + "Workflow Name: " + WorkflowName
+ Environment.NewLine + "Model Name: " + ModelName
+ Environment.NewLine + "Position Name: " + PositionName
+ Environment.NewLine + "Created by: " + CreatedBy
+ Environment.NewLine + "Modified by: " + ModifiedBy
+ Environment.NewLine + "From: " + From
+ Environment.NewLine + "To: " + To
+ Environment.NewLine + "Display Mode: " + DisplayMode
+ Environment.NewLine + "Data Tracking: " + DataTracking
+ Environment.NewLine + "Details: " + Details
+ Environment.NewLine + "Export Format: " + ExportFormat
+ Environment.NewLine + "Ged Files: " + GedFiles
+ Environment.NewLine + "Ged Folders: " + GedFolders
+ Environment.NewLine + "Map: " + Map
+ Environment.NewLine + "Kpi: " + Kpi
+ Environment.NewLine + "Procedure Type: " + ProcedureType
TextBlockResult.Text = resultToDisplay;
ie i want "Action: " + intent to be visible only when the value of intent is not null nor empty, the same for "Rok Entity: " + ROKEntity and so forth ..
For the moment, i have the following xaml that only allows me to set the visibility of the whole lot to collapsed or hidden or visible:
<TextBlock x:Name="TextBlockResult" Visibility="Collapsed" TextWrapping="Wrap" TextAlignment="Center" FontFamily="Segoe UI" FontSize="16" Foreground="Black"><Run Text="Result"/><InlineUIContainer>
What are the different steps to achieve that please ?
if I understand correctly what you are trying to do, you can just skip the elements with empty or null value. So have a method like
private void AddToDisplay(StringBuilder sb, string title, string value)
{
if (!string.IsNullOrEmpty(value))
{
if (sb.Length > 0)
sb.Append(Environment.NewLine);
sb.Append(title);
sb.Append(value);
}
}
then build your result like
var sb = new StringBuilder();
AddToDisplay(sb, "Action: ", intent);
AddToDisplay(sb, "Rok Entity: ", ROKEntity);
.
.
resultToDisplay = sb.ToString();
that way your results show only non-null non-empty elements and optionally hide the entire TextBlock if resultToDisplay is empty after you are done building it

LINQ to Entities does not recognize the method in Aggregate [duplicate]

This question already has answers here:
LINQ to Entities does not recognize the method
(5 answers)
Closed 6 years ago.
I have this LINQ query, I have problem with its Aggregate part:
Adresses = m.RelatedMultipleWorks.Count == 0 ?
m.RelatedCity + " + " + m.RelatedCounty + " + " + m.RelatedDistrict + " + " +
m.RelatedNeighborhood + " + " + m.RelatedStreet + " + " + m.Road :
m.RelatedCity + " + " + m.RelatedCounty + " + " + m.RelatedDistrict + " + " +
m.RelatedNeighborhood + " + " + m.RelatedStreet + m.RelatedRoad + " | "
m.RelatedMultipleWorks.Select(z => z.RelatedCity + " + " + z.RelatedCounty + " + " +
z.RelatedDistrict + " + " + z.RelatedNeighborhood + " + " + z.RelatedStreet + " + " + z.RelatedRoad)
.Aggregate((current, next) => current + " | " + next),
And I get this exception.
{"LINQ to Entities does not recognize the method 'System.String
Aggregate[String](System.Collections.Generic.IEnumerable1[System.String],
System.Func3[System.String,System.String,System.String])' method, and
this method cannot be translated into a store expression."}
Why am I getting this exception? How can I get rid of it? Thanks.
Aggregate has not translation to SQL, so Enumerate the results to memory:
.AsEnumerable().Aggregate((current, next) => current + " | " + next);
OR:
.ToList().Aggregate((current, next) => current + " | " + next);
The problem is that the .Aggregate() call is part of the projection to some type, and adding a .ToList() call inside the projection won't work since that projection is translated to sql as a whole. You cannot tell EF to translate half of the projection to SQL, and the other half not. You have to split the projection in two parts, and tell EF to translate the first part, but not the second.
Ok, to solve this. At the moment you have something like this. I can't be specific, and it will be different from your query, since you do not show the full query.
var result = ctx.SomeTable
.Where(...whatever...)
.Select(x => new SomeEntity {
// lots of properties
Adresses = m.RelatedMultipleWorks.Count == 0 ?
m.RelatedCity + " + " + m.RelatedCounty + " + " + m.RelatedDistrict + " + " +
m.RelatedNeighborhood + " + " + m.RelatedStreet + " + " + m.Road :
m.RelatedCity + " + " + m.RelatedCounty + " + " + m.RelatedDistrict + " + " +
m.RelatedNeighborhood + " + " + m.RelatedStreet + m.RelatedRoad + " | "
m.RelatedMultipleWorks.Select(z => z.RelatedCity + " + " + z.RelatedCounty + " + " +
z.RelatedDistrict + " + " + z.RelatedNeighborhood + " + " + z.RelatedStreet + " + " + z.RelatedRoad)
.Aggregate((current, next) => current + " | " + next),
// other properties
})
.ToList();
To eliminate the .Aggregate() call in the projection, you need to leave the datasource of the Aggregate intact so L2E can take that data from the database. After that you can apply the .Aggregate() in-memory.
var result = ctx.SomeTable
.Where(...whatever...)
// Change: Do not project to an entity, but to an anonymous type
.Select(x => new {
// lots of properties
// Change: Removed the Aggregate-part
Adresses = m.RelatedMultipleWorks.Count == 0 ?
m.RelatedCity + " + " + m.RelatedCounty + " + " + m.RelatedDistrict + " + " +
m.RelatedNeighborhood + " + " + m.RelatedStreet + " + " + m.Road :
m.RelatedCity + " + " + m.RelatedCounty + " + " + m.RelatedDistrict + " + " +
m.RelatedNeighborhood + " + " + m.RelatedStreet + m.RelatedRoad + " | ",
// Added: New property for the aggregate-datasource
RelatedAdresses = m.RelatedMultipleWorks
.Select(z =>
z.RelatedCity + " + " + z.RelatedCounty + " + " + z.RelatedDistrict + " + " +
z.RelatedNeighborhood + " + " + z.RelatedStreet + " + " + z.RelatedRoad
)
// other properties
})
// Added: Call AsEnumerable to stop translating to SQL
.AsEnumerable()
// Added: Project again, fairly simple since all properties are already ok, but now call the Aggregate
.Select(x => new SomeEntity {
// lots of properties
Adresses = Adresses + RelatedAdresses.Aggregate((current, next) => current + " | " + next)
// other properties
})
.ToList();

append column data as header/footers to same textfile using winforms application

I am newbie to c# and I want to send a text file with a header and footer using a sql query.
I have an sql results in datagridview and then saved as text file. but everytime i run this query, i should get an header and footer from the sql query. So I am thinking of creating 2 buttons for headers and footers and when i click these it gets the value from sql to datagridview and it copies to textfile and the same for footer just appending different text to the samefile. I want any suggestions can i do like that.
private void btnGetData_Click(object sender, EventArgs e)
{
this.btnGetData.Enabled = false;
Application.DoEvents();
string stringSql = " SELECT distinct " +
"'" + comboBox6.Text + "' as RecordType" +
" , left([CPC No] +' ',30) " +
" , space(1983) " +
",'" + comboBox6.Text + " 'as RecordType" +
, left(t.t_reference + ' ' ,24 ) as ClaimantReference " +
" , left([Claim Number] +' ',30) " +
" , " + comboBox4.Text + " as CourtCode" +
" ,left(ta_title +' ',30) as Title " +
" ,left(ta_surname +' ',30) as Surname " +
", space(180), bat.PCN_Charge as ClaimAmount " +
",[Court Fee] " +
",[Solictors Fees]" +
", (bat.PCN_Charge + [Court Fee]) as TotalAmount" +
",[POC1]" +
",'" + textBox2.Text + "' as RequestType" +
//",'" + comboBox1.Text + "' as RecordType" +
",'" + textBox3.Text + "' as TotalCourtFee" +
",'" + textBox4.Text + "' as TotalClaimAmount" +
" , space(1966) " +
" FROM tickets t " +
" LEFT OUTER JOIN " +
"( " +
" SELECT ticket_addresses.ta_system_ref, ta_title, ta_othername, ta_surname, ta_house_number, ta_address_1, ta_address_2, " +
" ta_address_3, ta_address_4, ta_post_code, ta_telephone, ta_organisation " +
" FROM ticket_addresses " +
" INNER JOIN " +
" ( " +
" SELECT ticket_addresses.ta_system_ref, MAX(ta_address_code) AS ta_address_code " +
" FROM ticket_addresses " +
" GROUP BY ta_system_ref " +
" ) ad " +
" ON (ticket_addresses.ta_system_ref=ad.ta_system_ref AND ticket_addresses.ta_address_code=ad.ta_address_code) " +
")ta " +
"ON (t.t_number=ta.ta_system_ref) " +
" " +
" Inner JOIN " +
" ticket_hold_record b " +
" ON ( t.t_number = b.thr_system_ref) " +
" " +
"Inner JOIN " +
"Rpt_PCNBalance_ALLTickets bat " +
"ON (t.t_number = bat.t_number) " +
" " +
"Inner JOIN " +
"hold_reasons ch " +
"ON (b.thr_hold_type = ch.hr_code) " +
" " +
"Inner JOIN " +
" [VCS].[dbo].[Courtfees] cf " +
" ON (bat.Payments >= cf. [Min ClaimAmount]) and (bat.Payments <= cf.[Max Claim Amount]) " +
" " +
"Inner JOIN " +
" [VCS].[dbo].[sites] s " +
" ON (t.t_contract = s.Contract) " +
" " +
"Inner JOIN " +
" [VCS].[dbo].[claim info] cc " +
" ON (cc.Code COLLATE DATABASE_DEFAULT = t.t_offence_code COLLATE DATABASE_DEFAULT) " +
" and t.t_reference IN {where} ";
//Generate list of Ticket IDS for SQL Where Clause
string whereClause = "";
string[] tempArray = new string[this.txt.Lines.Length];
tempArray = this.txt.Lines;
if (this.txt.Lines.Length == 0)
{
return;
}
for (int counter = 0; counter <= tempArray.Length-1; counter++)
{
if (tempArray[counter].Trim().Length > 0)
{
whereClause = whereClause + "'" + tempArray[counter] + "'" + ", ";
}
}
whereClause = whereClause.TrimEnd(' ', ',');
whereClause = "(" + whereClause + ")";
stringSql = stringSql.Replace("{where}", whereClause);
myDataset = new DataSet("SQL");
SqlConnection myConn = new SqlConnection();
SqlCommand myCommand = new SqlCommand();
myCommand.CommandType = CommandType.Text;
myCommand.CommandText = stringSql;
myCommand.Connection = myConn;
SqlDataAdapter myAdapter = new SqlDataAdapter();
myAdapter.SelectCommand = myCommand;
myAdapter.Fill(myDataset);
this.dataGridView1.DataSource = myDataset.Tables[0];
for (int counter = 0; counter < myDataset.Tables[0].Columns.Count; counter++)
{
this.dataGridView1.Columns[counter].SortMode = DataGridViewColumnSortMode.NotSortable;
}
this.dataGridView1.Refresh();
myConn.Close(); this.btnGetData.Enabled = true;
this.btnSave.Enabled = true;
Application.DoEvents();
}

DB Query to foreach loops c#

I'm developing some tool for a client, and they don't want to use LINQ to SQL, they also don't want to use WPF but I'm using it, they are afraid to new coding or something... But i can't with database, because they don't know anything about LINQ... so they gave me this annoying class (http://pastebin.com/VUzvN44i too long for paste here, sorry) which is "ok" when retrieving row by row...
But I have the following function, which is fully functional with linq to sql, but I'm lost with this class they gave me... So if someone can give me some advice on how to do it without modify the class it will be very helpful. Thanks.
private void GenerarFichero_Click(object sender, RoutedEventArgs e)
{
string valEmisora = "02827003";
string codigoCabecera;
if (DateTime.Today.Day > 7)
codigoCabecera = "AE570200";
else
codigoCabecera = "AE570100";
DataBaseDataContext dc = new DataBaseDataContext();
using (StreamWriter sw = new StreamWriter(codigoCabecera.Remove(6, 2)))
{
sw.WriteLine("775701 " + DateTime.Now.ToString("yy") + DateTime.Now.Month.ToString("d2") + DateTime.Now.AddDays(1).Day.ToString("d2") + DateTime.Now.Hour.ToString("d2") + DateTime.Now.Minute.ToString("d2") + "008910 00" + txtBanco.Text + codigoCabecera + txtBanco.Text + "");
sw.WriteLine("0170 " + valEmisora + " " + this.txtBanco.Text + " 10" + DateTime.Now.ToString("MM") + DateTime.Now.ToString("yy"));
var OutputQuery =
from o in dc.Seguros
group o by o.emisora;
List<int> TotalRegistros = new List<int>();
List<int> TotalSumas = new List<int>();
foreach (var grupo in OutputQuery)
{
sw.WriteLine("0270 " + valEmisora + grupo.Key + " " + this.txtBanco.Text + " 10" + DateTime.Now.ToString("MM") + DateTime.Now.ToString("yy"));
List<int> Suma = new List<int>();
foreach (var data in grupo)
{
Suma.Add(Convert.ToInt32(data.importe + data.importe_dec));
sw.WriteLine("6070 " + valEmisora + data.emisora + "1" + data.banco + data.sucursal + data.fecha + data.importe + data.importe_dec + data.identificacion + " " + data.referencia);
}
TotalRegistros.Add((grupo.Count() + 2));
TotalSumas.Add(Suma.Sum());
sw.WriteLine("8070 " + valEmisora + grupo.Key + " " + (grupo.Count() + 2).ToString().PadLeft(6, '0') + " " + Suma.Sum().ToString().PadLeft(12, '0'));
}
sw.WriteLine("9070 " + valEmisora + " " + (TotalRegistros.Sum() + 2).ToString().PadLeft(6, '0') + " " + TotalSumas.Sum().ToString().PadLeft(12, '0'));
this.txtTotal.Text = TotalSumas.Sum().ToString();
}
MessageBox.Show("El fichero ha sido guardado, ya no se puede editar");
}
I am not proud of what I'll be saying but since you desperately need a solution here it goes...
The quick and dirty way to work with this is:
var connection = InitalizeConnectionToDb();
var select = new SelectBD();
// Get the data from database
select.Open(connection, #"select * from Seguros");
// Simulate the grouping
var dictionary = new Dictionary<string, List<DataRow>>();
foreach(var row in select.DataTable)
{
var key = Convert.ToString(row["emisora"]);
if(!dictionary.ContainsKey(key))
{
dictionary[key] = new List<DataRow>();
}
dictionary[key].Add(row);
}
Now you can use the dictionary above to perform your calculations because the data is grouped by emisora field.
Well, I have the final code working, I've replaced OutputQuery with DataTable.AsEnumerable and actually it is working... This is what the code looks now
private void GenerarFichero_Click(object sender, RoutedEventArgs e)
{
string valEmisora = "02827003";
string codigoCabecera;
if (DateTime.Today.Day > 7)
codigoCabecera = "AE570200";
else
codigoCabecera = "AE570100";
using (StreamWriter sw = new StreamWriter(codigoCabecera.Remove(6, 2)))
{
sw.WriteLine("775701 " + DateTime.Now.ToString("yy") + DateTime.Now.Month.ToString("d2") + DateTime.Now.AddDays(1).Day.ToString("d2") + DateTime.Now.Hour.ToString("d2") + DateTime.Now.Minute.ToString("d2") + "008910 00" + txtBanco.Text + codigoCabecera + txtBanco.Text + "");
sw.WriteLine("0170 " + valEmisora + " " + this.txtBanco.Text + " 10" + DateTime.Now.ToString("MM") + DateTime.Now.ToString("yy"));
SelectBD sel = new SelectBD(App.ConexBD, "SELECT * FROM Seguros");
var Query = sel.DataTable.AsEnumerable().Select(row =>
{
return new
{
banco = row["banco"].ToString(),
emisora = row["emisora"].ToString(),
sucursal = row["sucursal"].ToString(),
fecha = row["fecha"].ToString(),
identificacion = row["identificacion"].ToString(),
importe = row["importe"].ToString(),
importe_dec = row["importe_dec"].ToString(),
provincia = row["provincia"].ToString(),
referencia = row["referencia"].ToString(),
};
});
var OutputQuery = Query.GroupBy(l => l.emisora);
List<int> TotalRegistros = new List<int>();
List<int> TotalSumas = new List<int>();
foreach (var grupo in OutputQuery)
{
sw.WriteLine("0270 " + valEmisora + grupo.Key + " " + this.txtBanco.Text + " 10" + DateTime.Now.ToString("MM") + DateTime.Now.ToString("yy"));
List<int> Suma = new List<int>();
foreach (var data in grupo)
{
Suma.Add(Convert.ToInt32(data.importe + data.importe_dec));
sw.WriteLine("6070 " + valEmisora + data.emisora + "1" + data.banco + data.sucursal + data.fecha + data.importe + data.importe_dec + data.identificacion + " " + data.referencia);
}
TotalRegistros.Add((grupo.Count() + 2));
TotalSumas.Add(Suma.Sum());
sw.WriteLine("8070 " + valEmisora + grupo.Key + " " + (grupo.Count() + 2).ToString().PadLeft(6, '0') + " " + Suma.Sum().ToString().PadLeft(12, '0'));
}
sw.WriteLine("9070 " + valEmisora + " " + (TotalRegistros.Sum() + 2).ToString().PadLeft(6, '0') + " " + TotalSumas.Sum().ToString().PadLeft(12, '0'));
this.txtTotal.Text = TotalSumas.Sum().ToString();
}
MessageBox.Show("El fichero ha sido guardado, ya no se puede editar");
}

AppendAllLines alternative solution

I am trying to write a txt file from C# as follows:
File.WriteAllText("important.txt", Convert.ToString(c));
File.AppendAllLines("important.txt", (from r in rec
select r.name + " " + r.num1 + " " + r.num2 + " " + r.mult + " " + r.rel).ToArray());
I am getting error AppendAllLInes not found for system.IO.File any alternative approach or how can I include AppendAllLInes
Write all at once.
var part1 = Convert.ToString(c);
var part2 = String.Join(Environment.NewLine,
rec.Select(r => r.name + " " + r.num1 + " " + r.num2 + " " + r.mult + " " + r.rel)
.ToArray());
System.IO.File.WriteAllText("important.txt", part1 + part2);
You could also use WriteAllLines in 3.5:
var allLines = new []{Convert.ToString(c)}
.Concat(rec.Select(r => r.name + " " + r.num1 + " " + r.num2 + " " + r.mult + " " + r.rel))
.ToArray();
System.IO.File.WriteAllLines("important.txt", allLines);

Categories

Resources