When making a request for HistoricalDataRequest on PX_LAST for a period of a month, I am getting back bulk data. What is wrong with my syntax because I keep getting "Cannot convert SEQUENCE to Element".
//FIELDS
Element fields = security.GetElement(FIELD_DATA);
if (fields.NumElements > 0)
{
int numElements = fields.NumElements;
for (int j = 0; j < numElements; ++j)
{
Element field = fields.GetElement(j);
if (field.Datatype == Schema.Datatype.SEQUENCE || field.IsArray)
{
processBulkField(field, ticker, response);
}
else
{
processRefField(field, ticker, response);
}
}
}
here is the process bulk fields: Error is on the line: Element bulkElement = refBulkfield.GetValueAsElement(i);
private static void processBulkField(Element refBulkfield, string ticker, List<BBGSecurity> response)
{
// Get the total number of Bulk data points
int numofBulkValues = refBulkfield.NumValues;
for (int i = 0; i < numofBulkValues; ++i)
{
Element bulkElement = refBulkfield.GetValueAsElement(i);
// Get the number of sub fields for each bulk data element
int numofBulkElements = bulkElement.NumElements;
// Read each field in Bulk data
for (int j = 0; j < numofBulkElements; ++j)
{
Element field = bulkElement.GetElement(j);
response.Add(new BBGSecurity { Security = ticker, DataDate = DateTime.Parse(d_dataDate), DataSessionID = d_dataSession, Field = field.Name.ToString(), PropertyValue = field.GetValueAsString() });
}
}
}
Thanks in advance!
Please remove field.Datatype == Schema.Datatype.SEQUENCE from the if condition, it should look like:
if (field.IsArray)
{
processBulkField(field, ticker, response);
}
else
{
processRefField(field, ticker, response);
}
Bulk fields are identified by property field.IsArray = True.
Bulk fields contains data set structured as a table with columns and rows, while SEQUENCE field contains data set represented like a C struct.
Related
I want to delete some of the particular lines from a sales order documents Lines part by Add On using c# .
You need to first get the document you want to alter using DocEntry.
You then have to set the line number that needs deletion. See below
Documents oDoc = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oOrders);
int docEntry = 109;
int lineNum = 2;
// Load your sales orders
if (oDoc.GetByKey(docEntry))
{
// Go through your line items
for (int i = 0; i < oDoc.Lines.Count; i++)
{
oDoc.Lines.SetCurrentLine(i);
if (oDoc.Lines.LineNum == lineNum) //Find your line number that you want delete.
{
oDoc.Lines.Delete(); //Delete
break;
}
}
// Update your sales order
if (oDoc.Update() != 0)
MessageBox.Show(oCompany.GetLastErrorDescription());
}
private void Delete_Single_Line_Row(string docentry, int lNum)
{
SAPbobsCOM.Documents oSalesOrd = null;
oSalesOrd = (SAPbobsCOM.Documents)SBOC_SAP.G_DI_Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oOrders);
int docEntry = Convert.ToInt32(docentry);
int lineNum = lNum;
// Load your sales orders
if (oSalesOrd.GetByKey(docEntry))
{
// Go through your line items
for (int i = 0; i < oSalesOrd.Lines.Count; i++)
{
oSalesOrd.Lines.SetCurrentLine(i);
if (oSalesOrd.Lines.LineNum == lineNum) //Find your line number that you want delete.
{
oSalesOrd.Lines.Delete(); //Delete
break;
}
}
// Update your sales order
int result = oSalesOrd.Update();
}
} enter code here
Documents oDoc = Utils.oCompany.GetBusinessObject(BoObjectTypes.oOrders);
oDoc.GetByKey(123);
oDoc.Lines.SetCurrentLine(0);
oDoc.Lines.Delete();
int lret = oDoc.Update();
if (lret != 0)
{
//HANDLE ERROR
}
This is pretty straight forward I think.
This code is supposed to take two samples one original and one new, then determine the length of the smallest single consecutive piece that has been inserted into the first sequence.
When trying some samples I get the following error message:
System.ArgumentOutOfRangeException: 'Index and length must refer to a
place within the string. Parameter name: length
Here is the code:
class Program
{
static void Main(string[] args)
{
Console.WriteLine(GetSample());
Console.ReadKey();
}
public static int GetSample()
{
string sample1 = Console.ReadLine();
string sample2 = Console.ReadLine();
if (sample1 == sample2) return 0;
if (sample1.Length >= sample2.Length)
{
for (int i = 0; i < sample2.Length; i++)
{
if (!(sample1[i] == sample2[i]))
{
sample1 = sample1.Substring(i, sample1.Length);
sample2 = sample2.Substring(i, sample2.Length);
break;
}
}
int var = sample1.Length - sample2.Length;
for (int i = sample2.Length - 1; i >= 0; i--)
{
if (sample2[i] == sample1[i + var])
sample2 = trimlast(sample2);
}
}
else
{
for (int i = 0; i < sample1.Length; i++)
{
if (!(sample1[i] == sample2[i]))
{
sample1 = sample1.Substring(i, sample1.Length);
sample2 = sample2.Substring(i, sample2.Length);
break;
}
}
int var = sample2.Length - sample1.Length;
for (int i = sample1.Length - 1; i >= 0; i--)
{
if (sample2[i + var] == sample1[i])
sample2 = trimlast(sample2);
}
}
return sample2.Length;
}
public static string trimlast(string str)
{
return str.Substring(0, str.Length - 1);
}
}
}
The problem is:
sample1 = sample1.Substring(i, sample1.Length);
and the other similar method calls. The second parameter of Substring is the length (i.e. the number of characters to retrieve for the substring). Thus, if i is greater than 0, it should fail in this case because the method will try to retrieve characters that aren't in the string.
One of your loops is trying to access an element that doesn't exist. For example you have an array a ={1,2,3}, you are trying to access the fourth element, which doesn't exist.
If you can't find out where exactly, there could be an issue, try using print statements inside your loops, displaying the counter (i) values. It will point out for which iteration, your code is failing.
I'm currently working on a project where I have to choose a file and then sort it along with other files. The files are just filled with numbers but each file has linked data. So the first number in a file is linked to the first number in the second file and so on. I Currently have code that allows me to read a file and display the file unsorted and sorted using the bubble sort. I am not sure how I would be able to apply this same principle to multiple files at once. So that I could choose a file and then sort it in line with the same method I have for a single file.
Currently, the program loads and asks the user to choose between 1 and 2. 1 Shows the code unsorted and 2 shows the code sorted. I can read in the files but the problem is sorting and displaying in order. Basically How do I sort multiple files that are linked together. What steps do I need to take to do this?
An example of one file:
4
28
77
96
An example of the second file:
66.698
74.58
2.54
48.657
Any help would be appreciated.
Thanks
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
public class Program
{
public Program()
{
}
public void ReadFile(int [] numbers)
{
string path = "Data1/Day_1.txt";
StreamReader sr = new StreamReader(path);
for (int index = 0; index < numbers.Length; index++)
{
numbers[index] = Convert.ToInt32(sr.ReadLine());
}
sr.Close(); // closes file when done
}
public void SortArray(int[] numbers)
{
bool swap;
int temp;
do
{
swap = false;
for (int index = 0; index < (numbers.Length - 1); index++)
{
if (numbers[index] > numbers[index + 1])
{
temp = numbers[index];
numbers[index] = numbers[index + 1];
numbers[index + 1] = temp;
swap = true;
}
}
} while (swap == true);
}
public void DisplayArray(int[] numbers)
{
for(int index = 0; index < numbers.Length; index++)
{
Console.WriteLine("{0}", numbers[index]);
}
}
}
The main is in another file to keep work organised:
using System;
public class FileDemoTest
{
public static void Main(string[] args)
{
int[] numbers = new int[300];
Program obj = new Program();
int operation = 0;
Console.WriteLine("1 or 2 ?");
operation = Convert.ToInt32(Console.ReadLine());
// call the read from file method
obj.ReadFile(numbers);
if (operation == 1)
{
//Display unsorted values
Console.Write("Unsorted:");
obj.DisplayArray(numbers);
}
if (operation == 2)
{
//sort numbers and display
obj.SortArray(numbers);
Console.Write("Sorted: ");
obj.DisplayArray(numbers);
}
}
}
What I would do is create a class that will hold the values from file 1 and file 2. Then you can populate a list of these classes by reading values from both files. After that, you can sort the list of classes on either field, and the relationships will be maintained (since the two values are stored in a single object).
Here's an example of the class that would hold the file data:
public class FileData
{
public int File1Value { get; set; }
public decimal File2Value { get; set; }
/// <summary>
/// Provides a friendly string representation of this object
/// </summary>
/// <returns>A string showing the File1 and File2 values</returns>
public override string ToString()
{
return $"{File1Value}: {File2Value}";
}
}
Then you can create a method that reads both files and creates and returns a list of these objects:
public static FileData[] GetFileData(string firstFilePath, string secondFilePath)
{
// These guys hold the strongly typed version of the string values in the files
int intHolder = 0;
decimal decHolder = 0;
// Get a list of ints from the first file
var fileOneValues = File
.ReadAllLines(firstFilePath)
.Where(line => int.TryParse(line, out intHolder))
.Select(v => intHolder)
.ToArray();
// Get a list of decimals from the second file
var fileTwoValues = File
.ReadAllLines(secondFilePath)
.Where(line => decimal.TryParse(line, out decHolder))
.Select(v => decHolder)
.ToArray();
// I guess the file lengths should match, but in case they don't,
// use the size of the smaller one so we have matches for all items
var numItems = Math.Min(fileOneValues.Count(), fileTwoValues.Count());
// Populate an array of new FileData objects
var fileData = new FileData[numItems];
for (var index = 0; index < numItems; index++)
{
fileData[index] = new FileData
{
File1Value = fileOneValues[index],
File2Value = fileTwoValues[index]
};
}
return fileData;
}
Now, we need to modify your sorting method to work on a FileData array instead of an int array. I also added an argument that, if set to false, will sort on the File2Data field instead of File1Data:
public static void SortArray(FileData[] fileData, bool sortOnFile1Data = true)
{
bool swap;
do
{
swap = false;
for (int index = 0; index < (fileData.Length - 1); index++)
{
bool comparison = sortOnFile1Data
? fileData[index].File1Value > fileData[index + 1].File1Value
: fileData[index].File2Value > fileData[index + 1].File2Value;
if (comparison)
{
var temp = fileData[index];
fileData[index] = fileData[index + 1];
fileData[index + 1] = temp;
swap = true;
}
}
} while (swap);
}
And finally, you can display the non-sorted and sorted lists of data. Note I added a second question to the user if they choose "Sorted" where they can decide if it should be sorted by File1 or File2:
private static void Main()
{
Console.WriteLine("1 or 2 ?");
int operation = Convert.ToInt32(Console.ReadLine());
var fileData = GetFileData(#"f:\public\temp\temp1.txt", #"f:\public\temp\temp2.txt");
if (operation == 1)
{
//Display unsorted values
Console.WriteLine("Unsorted:");
foreach (var data in fileData)
{
Console.WriteLine(data);
}
}
if (operation == 2)
{
Console.WriteLine("Sort on File1 or File2 (1 or 2)?");
operation = Convert.ToInt32(Console.ReadLine());
//sort numbers and display
SortArray(fileData, operation == 1);
Console.WriteLine("Sorted: ");
foreach (var data in fileData)
{
Console.WriteLine(data);
}
}
Console.Write("\nDone!\nPress any key to exit...");
Console.ReadKey();
}
If you wanted to use an int to decide which field to sort on, you could do something like the following:
public static void SortArray(FileData[] fileData, int sortFileNumber = 1)
{
bool swap;
do
{
swap = false;
for (int index = 0; index < (fileData.Length - 1); index++)
{
bool comparison;
// Set our comparison to the field sortFileNumber
if (sortFileNumber == 1)
{
comparison = fileData[index].File1Value > fileData[index + 1].File1Value;
}
else if (sortFileNumber == 2)
{
comparison = fileData[index].File2Value > fileData[index + 1].File2Value;
}
else // File3Value becomes default for anything else
{
comparison = fileData[index].File3Value > fileData[index + 1].File3Value;
}
if (comparison)
{
var temp = fileData[index];
fileData[index] = fileData[index + 1];
fileData[index + 1] = temp;
swap = true;
}
}
} while (swap);
}
I write some data into csv file from List but some list indexes has empty string but another indexes has value
in these cases the data compared with another list wrote in the same csv file
this is my csv file opened using excel sheet
in the third column there exist ID for the the second column cell so in the coming rows i want to detect the name of the ID based on previous rows
like in row 3 it's ID is 19 and name is I/O so in the 7th row the ID is 19 and want to fill the second cell now
info : the IDs is already known above and any next ID will be exist before
by the follwing code.
bool isInList = ms.IndexOf(ShapeMaster) != -1;
if (isInList)
{
savinglabelnamefortextbox = t.InnerText;
string replacement =
Regex.Replace(savinglabelnamefortextbox, #"\t|\n|,|\r", "");
xl.Add("");
dl.Add(replacement);
ms.Add(ShapeMaster);
}
and I use the following code to write to the csv file
using (StreamWriter sw = File.CreateText(csvfilename))
{
for (int i = 0; i < dl.Count; i++)
{
var line = String.Format("{0},{1},{2}", dl[i], xl[i],ms[i]);
sw.WriteLine(line);
}
}
Try this
for (int x = 0; x < ms.Count; x++)
{
if (xl[x] != "")
{
continue;
}
else if (xl[x] == "")
{
for (int y = 0; y<xl.Count; y++)
{
if (ms[y] == ms[x])
{
xl[x] = xl[y];
break;
}
}
continue;
}
}
Im working on a website where X amount of fields are show, I have a button beneath that allows the user to click and duplicate the same fields - I have this working, the only problem is that my data is caching, so when you click the button, all fields contained cloned values. Can anybody see where i may be going wrong?
private void NewDelegates()
{
int NewDelegate = 2;
if (ViewState["NewDelegate"] != null)
NewDelegate = int.Parse(ViewState["NewDelegate"].ToString());
else
ViewState.Add("NewDelegate", 2);
List<DelegateNoList> DelNo = new List<DelegateNoList>();
for (int i = 0; i < NewDelegate; i++)
DelNo.Add(new DelegateNoList { id = i });
LV_Delegates.DataSource = DelNo;
LV_Delegates.DataBind();
}
public class DelegateNoList
{
public int id { get; set; }
}
Because you loop two times
your max is 2
int NewDelegate = 2;
initialized state is 0
for (int i = 0; i < NewDelegate; i++)
DelNo.Add(new DelegateNoList { id = i });