Populating data from database into multiple colums equally? - c#

I'm trying to populate two different container equally with data coming from the database. Putting 5 locations in the first container and 6 at the second. Every time that run this code, it populates all into the left container.
Any Ideas what am I missing?
shopDataContext dc = new shopDataContext();
var areas = (from s in dc.Areas select s);
String content = "";
foreach(var a in areas) {
if (a.id >= 1 && a.id <= 6) {
content += "<div><h3>" + a.Tittle + "</h3>";
var aStores = (from s in dc.Stores where s.areaid == a.id select s);
foreach(var s in aStores) {
content += "<p>" + s.Tittle + "</p>";
}
leftContent.InnerHtml = leftContent.InnerHtml + content;
} else {
content += "<div><h3>" + a.Tittle + "</h3>";
var aStores = (from s in dc.Stores where s.areaid == a.id select s);
foreach(var s in aStores) {
content += "<p>" + s.Tittle + "</p>";
}
rightContent.InnerHtml = rightContent.InnerHtml + content;
}
}

I guess you want to reset the added content in each iteration of the loop:
...
foreach(var a in areas) {
string content = "";
if (a.id >= 1 && a.id <= 6) {
...
}
}
....
Otherwise the content is added several times.

Related

C# - Cannot populate grandchild of second child in tree view

I am populating a treeview from an arrayList and I am not able to populate the grandchildren of the second child.
I was going for something like this
parent
- child1
- gc1
- gc2
- child2
- gc1
- gc2
But I am stuck with only just populating the child2 as I am having exceptions (ArgumentOutOfRangeException) when trying to populate the grandchildren of the second child
string currentDatabaseParent = "";
string currentSchemaParent = "";
string currentTableParent = "";
int databasecount = 0;
int schemacount = 0;
int tablecount = 0;
foreach (var schema in schemaArr)
{
int order = Array.IndexOf(schemaArr, schema);
if ((schema.column_name != "Number") && (schema.schema_name != "INFORMATION_SCHEMA") && (schema.database_name != "SAMPLE_DATA"))
{
if ((currentDatabaseParent == "") || (currentDatabaseParent != schema.database_name))
{
treeViewSchemaList.Nodes.Add(schema.database_name);
sb.AppendLine(schema.database_name);
currentDatabaseParent = schema.database_name;
}
else if (currentDatabaseParent != schema.database_name)
{
treeViewSchemaList.Nodes.Add(schema.database_name);
sb.AppendLine(schema.database_name);
currentDatabaseParent = schema.database_name;
databasecount = databasecount + 1;
schemacount = 0;
tablecount = 0;
}
if ((currentSchemaParent == ""))
{
treeViewSchemaList.Nodes[schemacount].Nodes.Add(schema.schema_name);
sb.AppendLine(" - " + schema.schema_name);
currentSchemaParent = schema.schema_name;
}
else if (currentSchemaParent != schema.schema_name)
{
treeViewSchemaList.Nodes[schemacount].Nodes.Add(schema.schema_name);
sb.AppendLine(" - " + schema.schema_name);
currentSchemaParent = schema.schema_name;
schemacount = schemacount + 1;
}
if (currentTableParent == "")
{
treeViewSchemaList.Nodes[schemacount].Nodes[tablecount].Nodes.Add(schema.table_name);
sb.AppendLine(" - " + schema.table_name);
currentTableParent = schema.table_name;
}
else if (currentTableParent != schema.table_name)
{
treeViewSchemaList.Nodes[schemacount].Nodes[tablecount].Nodes.Add(schema.table_name);
sb.AppendLine(" - " + schema.table_name);
currentTableParent = schema.table_name;
tablecount = tablecount + 1;
}
//DataType dataType = new DataType();
//dataType = JsonConvert.DeserializeObject<DataType>(schema.data_type);
//sb.AppendLine(" - " + schema.column_name + "(" + dataType.Type + ")");
//treeViewSchemaList.Nodes[databasecount].Nodes[schemacount].Nodes[tablecount].Nodes.Add(schema.column_name + "(" + schema.data_type + ")");
}
}
The code stucks to this when viewed from the StringBuilder
parent
- child1
- gc1
- gc2
- child2
I have already tried modifying some code by manipulating the count variables but still can't get to work

how to get more than 5000 entities by using Factories GetByFilter method with prestasharp

Library Version:
1.2.9
NuGet Package Url:
https://www.nuget.org/packages/PrestaSharp/1.2.9
Prestashop version:
1.7.7.0
Describe the Bug:
PrestaSharp GetByFilter with pagination always return same entity list
Since ProductFactory's GetByFilter method returns null if there are more than 5000 products that match the filter. I decide to get them by pagination like this
_productFactory.GetIdsByFilter(filter, null, "[" + startingIndex.ToString() + "," + count.ToString() + "]");
but even if startingIndex(because of a loop) changes the result is the same
Full code:
filter.Add("date_upd", "[" + dFrom + "," + dTo + "]");
int i = 0;
List<long> AllProducts = new List<long>();
List<long> products;
while (true) // this loop never breaks
{
int startingIndex = i++ * count;
products = _productFactory.GetIdsByFilter(filter, null, "[" + startingIndex.ToString() + "," + (count).ToString() + "]"); // returns same products in every iteration
if (products?.Any() == true) // to check the list is not empty
{
AllProducts.AddRange(products);
if (products.Count < count)
{
break;
}
}
else
break;
}
You just have to remove the brackets from the 'limit' parameter. It is an error in the Github documentation when they give the example with brackets. Here's an own implementation where I send multiple requests in parallel to speed up the processing, regards.
public async Task<List<PS_Entity>> GetElements(int pageSize = 100) {
try {
var numberOfElements = factory.GetIds().Count;
var numberOfParallelTasks = numberOfElements >= pageSize ? numberOfElements / pageSize : 1;
var loadElementsTasks = Enumerable.Range(0, numberOfParallelTasks).Select(taskNumber => factory.GetByFilterAsync(null, "id_ASC", $"{taskNumber * pageSize},{pageSize}")).ToList();
if (numberOfElements % pageSize != 0) {
var skiped = numberOfParallelTasks * pageSize;
loadElementsTasks.Add(factory.GetByFilterAsync(null, "id_ASC", $"{skiped},{numberOfElements - skiped}"));
}
var elements = (await Task.WhenAll(loadElementsTasks)).SelectMany(elements => elements).ToList();
return elements;
} catch (PrestaSharpException e) {
if ((int)e.ResponseHttpStatusCode == 404)
Console.WriteLine($"No existen {RemoveFactoryFromName(factory)}'s.");
return new List<PS_Entity>();
}
}

C# sort values from textboxes from min to max

I want to take the values from my textboxes when I press the FIFO button, detect the smallest one and do the operations starting with the smallest number in ti and t until the biggest one, then store all that in tf.
This is what my program looks like:
Here's my code:
private void fifo_Click(object sender, EventArgs e)
{
int c = 0;
//Hace tf
tfA.Text = (Convert.ToInt32(ta.Text) + Convert.ToInt32(tiA.Text)).ToString();
c = Convert.ToInt32(tfA.Text);
tfB.Text = (Convert.ToInt32(tb.Text) + c).ToString();
c = Convert.ToInt32(tfB.Text);
tfC.Text = (Convert.ToInt32(tc.Text) + c).ToString();
c = Convert.ToInt32(tfC.Text);
tfD.Text = (Convert.ToInt32(td.Text) + c).ToString();
c = Convert.ToInt32(tfD.Text);
tfE.Text = (Convert.ToInt32(te.Text) + c).ToString();
c = Convert.ToInt32(tfE.Text);
tfF.Text = (Convert.ToInt32(tf.Text) + c).ToString();
// Hace T
T1.Text = (Convert.ToInt32(tfA.Text) - Convert.ToInt32(tiA.Text)).ToString();
T2.Text = (Convert.ToInt32(tfB.Text) - Convert.ToInt32(tiB.Text)).ToString();
T3.Text = (Convert.ToInt32(tfC.Text) - Convert.ToInt32(tiC.Text)).ToString();
T4.Text = (Convert.ToInt32(tfD.Text) - Convert.ToInt32(tiD.Text)).ToString();
T5.Text = (Convert.ToInt32(tfE.Text) - Convert.ToInt32(tiE.Text)).ToString();
T6.Text = (Convert.ToInt32(tfF.Text) - Convert.ToInt32(tiF.Text)).ToString();
// Hace E
E1.Text = (Convert.ToInt32(T1.Text) - Convert.ToInt32(ta.Text)).ToString();
E2.Text = (Convert.ToInt32(T2.Text) - Convert.ToInt32(tb.Text)).ToString();
E3.Text = (Convert.ToInt32(T3.Text) - Convert.ToInt32(tc.Text)).ToString();
E4.Text = (Convert.ToInt32(T4.Text) - Convert.ToInt32(td.Text)).ToString();
E5.Text = (Convert.ToInt32(T5.Text) - Convert.ToInt32(te.Text)).ToString();
E6.Text = (Convert.ToInt32(T6.Text) - Convert.ToInt32(tf.Text)).ToString();
//Hace I
I1.Text = (Convert.ToDecimal(ta.Text) / Convert.ToDecimal(T1.Text)).ToString();
I2.Text = (Convert.ToDecimal(tb.Text) / Convert.ToDecimal(T2.Text)).ToString();
I3.Text = (Convert.ToDecimal(tc.Text) / Convert.ToDecimal(T3.Text)).ToString();
I4.Text = (Convert.ToDecimal(td.Text) / Convert.ToDecimal(T4.Text)).ToString();
I5.Text = (Convert.ToDecimal(te.Text) / Convert.ToDecimal(T5.Text)).ToString();
I6.Text = (Convert.ToDecimal(tf.Text) / Convert.ToDecimal(T6.Text)).ToString();
//X1 2 y 3
X1.Text = ((Convert.ToDecimal(T1.Text) + Convert.ToDecimal(T2.Text) + Convert.ToDecimal(T3.Text) + Convert.ToDecimal(T4.Text) + Convert.ToDecimal(T5.Text) + Convert.ToDecimal(T6.Text)) / 6).ToString();
X2.Text = ((Convert.ToDecimal(E1.Text) + Convert.ToDecimal(E2.Text) + Convert.ToDecimal(E3.Text) + Convert.ToDecimal(E4.Text) + Convert.ToDecimal(E5.Text) + Convert.ToDecimal(E6.Text)) / 6).ToString();
X3.Text = ((Convert.ToDecimal(I1.Text) + Convert.ToDecimal(I2.Text) + Convert.ToDecimal(I3.Text) + Convert.ToDecimal(I4.Text) + Convert.ToDecimal(I5.Text) + Convert.ToDecimal(I6.Text)) / 6).ToString();
comp1 = Convert.ToDecimal(X3.Text);
}
EDIT: I managed to do what I wanted. I just put the values in an array and sorted them, then equaled the textbox.Text to each one. Worked pretty well. Thank you for the help.
My suggestion would be:
// list holding the ti value and the three controls: ti, t and tf
var controls = new List<Tuple<int, Control, Control, Control>>();
// add all the controls together with the sorting key, the ti value (just an example, but here you add the tiValue and the ti control, t control and tf control
controls.Add(new Tuple<int, Control, Control, Control>(0, null, null, null));
// sort everything
controls.Sort((t1, t2) => t1.Item1.CompareTo(t2.Item1));
// loop through the controls in the right order and perform the logic
foreach (var t in controls)
{
var tiValue = t.Item1;
var tiControl = t.Item2;
var tControl = t.Item3;
var tfControl = t.Item4;
// Do the things to the controls and their values...
}
Can't access pastebin or imgur where I am but I'll give this a shot. Textboxes are a terrible idea for this though.
public static void SortAndSumTextboxes()
{
Form form = new Form();
var tiControls = form.Controls.OfType<TextBox>().Where(tb => Regex.IsMatch(tb.Name, "^ti.$"));
var tControls = form.Controls.OfType<TextBox>().Where(tb => Regex.IsMatch(tb.Name, "^t.$"));
var tiSorted = tiControls.Select(tb => int.Parse(tb.Text)).OrderBy(i => i);
var tSorted = tControls.Select(tb => int.Parse(tb.Text)).OrderBy(i => i);
int c = 1; // some constant
var tfValues = tiSorted.Zip(tSorted, (a, b) => a + b + c).ToArray();
var tfControls = form.Controls.OfType<TextBox>().Where(tb => tb.Name.StartsWith("tf")).OrderBy(tb => tb.Name).ToArray(); ;
for (int i = 0; i < tfControls.Length; i++)
{
tfControls[i].Text = tfValues[i].ToString();
}
}

Search between two dates not working

I have the following code:
var q = context.Fuels.AsQueryable();
if (dateEdit1.EditValue != null && dateEdit2.EditValue != null)
{
q.Where(d => d.FuelDate >= dateEdit1.DateTime && d.FuelDate <= dateEdit2.DateTime);
}
and it's working, it will get all rows in table.
but when I'm using this code:
if (dateEdit1.EditValue != null && dateEdit2.EditValue != null)
{
var r = context.ExecuteStoreQuery<Fuel>(String.Format("SELECT * FROM Fuel WHERE FuelDate BETWEEN '{0}' AND '{1}'",
dateEdit1.DateTime.Year + "-" + dateEdit1.DateTime.Month + "-" + dateEdit1.DateTime.Day,
dateEdit2.DateTime.Year + "-" + dateEdit2.DateTime.Month + "-" + dateEdit2.DateTime.Day));
}
it's working but I need to use the first way to get relations working.
Any idea what is the problem in the first one?
Inside of your if clause, you need to assign it to q, if you don't do that q is just a query for all Fuels
This should work:
q = q.Where(d => d.FuelDate >= dateEdit1.DateTime && d.FuelDate <= dateEdit2.DateTime);

How can I add an unknownn number of dropdownlists programatically to a page and retrieve the selected values with c# on submit

I'm trying to do this and it is a bit confusing for me.
Basically the scenario is like this, I'm getting an XML from a 3rd party application with available dates for booking, for each day there are types of rooms the person can choose, single, double, etc.
Each hostel will return me an unknown number of room types. But dont get too confused with this.
The thing is simple I just need to add an unknown number of dropdownlists (or HTML Select) with the numbers of persons to book for. Now because I don't know how many of those dropdowns I will have I need to add them programatically inside a "for int i=0; i
How can I add an unknownn number of dropdownlists programatically to a page and retrieve the selected values with c# on submit?
The last column on the screenshot
http://i.stack.imgur.com/37chw.png
Update:
I'm creating the code from the xml results as a string that will print as html code:
XmlDocument xmlDoc2 = new XmlDocument();
xmlDoc.LoadXml(getPrices());
XmlNodeList prices = xmlDoc.GetElementsByTagName("RoomType");
string[] bookingDates = new string[Convert.ToInt32(Request.QueryString["nights"])];
string[] bookingDays = new string[Convert.ToInt32(Request.QueryString["nights"])];
bookingDates[0] = Request.QueryString["date"].ToString();
string[] dateArray = Request.QueryString["date"].ToString().Split('-');
DateTime initialDate = new DateTime(Convert.ToInt32(dateArray[0]), Convert.ToInt32(dateArray[1]), Convert.ToInt32(dateArray[2]));
bookingDays[0] = initialDate.DayOfWeek.ToString();
for (int z = 1; z < bookingDates.Length; z++)
{
DateTime nextDay = initialDate.AddDays(z);
string month = nextDay.Month.ToString();
string day = nextDay.Day.ToString();
if (day.Length == 1)
{
day = "0" + day;
}
if (month.Length == 1)
{
month = "0" + month;
}
bookingDates[z] = nextDay.Year + "-" + month + "-" + day;
bookingDays[z] = nextDay.DayOfWeek.ToString();
}
string pricesHeader = "<table width='100%'>";
pricesHeader += "<tr><td>Room Type</td>";
for (int x = 0; x < bookingDates.Length; x++)
{
string[] bookingDay = bookingDates[x].Split('-');
pricesHeader += "<td align='center'>" + bookingDays[x].Substring(0, 3) + "<br>" + bookingDay[2] + "</td>";
}
pricesHeader += "<td>Persons</td></tr>";
string pricesContent = "<tr>";
int dropNumber = 1;
foreach (XmlElement node in prices)
{
XmlNodeList roomTypeDescriptionN = node.GetElementsByTagName("roomTypeDescription");
string roomTypeDescriptionS = roomTypeDescriptionN[0].InnerText;
pricesContent += "<td>" + roomTypeDescriptionS + "</td>";
XmlNodeList priceN = node.GetElementsByTagName("price");
string priceS = priceN[0].InnerText;
XmlNodeList currencyN = node.GetElementsByTagName("currency");
string currencyS = currencyN[0].InnerText;
if (currencyS == "EUR")
{
currencyS = "&euro";
}
string avDates = "";
XmlNodeList availableDatesN = node.GetElementsByTagName("date");
int dateNumber = 0;
foreach (XmlElement avDate in availableDatesN)
{
avDates += availableDatesN[dateNumber].InnerText + ",";
dateNumber++;
}
for (int c = 0; c < bookingDates.Length; c++)
{
if (avDates.Contains(bookingDates[c]))
{
pricesContent += "<td>" + priceS + currencyS + "</td>";
}
else
{
pricesContent += "<td><center>X</center></td>";
}
}
pricesContent += "<td><select runat=server name='pers" + dropNumber + "' id='pers" + dropNumber + "'>" +
"<option>0</option><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option><option>6</option><option>7</option><option>8</option></select></td></tr>";
dropNumber++;
}
pricesLabel.Text = pricesHeader + pricesContent + "</table>";
I know that doing that and adding the runat=server won't help on my control, there is where my main problem is now, how to add the code on the html to be able to get the dropdownlist selected value later with c#. Can I do that with Request.Form ? was trying but so far I couldnt do it.
You can use the Repeater Class for generation of controls
You can also use the Request.Form Collection for obtaining of user's choice
You can use List for saving your created dropdownlists. On submit, you can read the data from your list.
List<DropDownList> ddlList = new List<DropDownList>{};
for(int i=0;i<count;i++)
{
//add control to page
ddlList.items.add(YourNewlyCreatedDdl);
}
for (int i = 0; i < 5; i++)
{
DropDownList ddl = new DropDownList();
ddl.ID=string.Format("ddl_{0}",i);
this.form1.Controls.Add(ddl);
}
This creates 5 empty DropDownLists.
You will need to rebuild and repopulate the controls on each postback in order for ASP to recognise their values.

Categories

Resources