This question already has answers here:
How to save a List<string> on Settings.Default?
(4 answers)
Closed 6 years ago.
I've modified my Settings.settings file in order to have system_Filters be a System.String[].
I would like to populate this variable within the Visual Studio designer and not from within the code. I can get it to work within the code using the following:
Properties.Settings.Default.system_Filters = new string[] { "a", "b", "c" };
string _systemFilters = Properties.Settings.Default.system_Filters;
This works correctly. However, I don't have a nice place to put this in my code and ideally want to type it in the Settings.setting file. This value will not be modified. I've tried almost every variant I can think of, but the result is always the same.
Does anyone know the correct syntax? I'm also open to using a StringCollection if that's easier.
Try to use StringCollection instead of string[]. It is natively supported by the settings designer.
The value is stored as XML, but you don't need to edit it manually. When you select StringCollection in the Type column, the editor in the Value column changes; there is a ... button, which opens a dialog to type the strings.
Related
I've begun the task of bringing our CR to the 21st century. The templates were created more than 10 years ago, as well as the report generator application written in Visual Basic that allows a user to pick different options before they click a button for generating a report.
So far the process hasn't been too bad but I've stumbled upon something that I can't figure out.
In VB, there's a line of code that sorts the report by a database table field:
Report.RecordSortFields.Add Report.Database.Tables(1).fields.GetItemByName("Name"), crAscendingOrder
I've successfully got that same logic working using the lines below
DatabaseFieldDefinition databaseFieldDefinition = Report.Database.Tables["Bureau"].Fields["Name"];
SortField sortField = Report.DataDefinition.SortFields[0];
sortField.Field = databaseFieldDefinition;
sortField.SortDirection = SortDirection.AscendingOrder;
The other type of sorting is what I'm struggling with. The problem is if that DataDefinition only contains a single SortField using a database tables field, I can't seem to make it order using a formula field. If you iterate through the formula fields you can see multiple fields like #Name, #XX, however if you look in the sort field it looks like it's specifying just a table like 'Assets.Name'
In the original code, this was done simply using the this VB line of code:
Report.RecordSortFields.Add Report.FormulaFields.GetItemByName("AssetName"), crAscendingOrder
For report files that already have a sort field with an #XXX, it works because I'm just replacing the SortField with this new FormulaDefinition that I've got from the below code (that also uses the #XXX), however if the sort field is only using a 'Table.Field' it doesn't work
Report.DataDefinition.FormulaFields["AssetName"];
My obvious first guess is that I need to modify the report file to get this working, however the report files are still allowing sorting via formulafield even in our old VB app, meaning if they could do it back 10+ years ago in vb I should be able to do it in C# without modifying the report.
So I have a slightly interesting problem right here. What I'm trying to achieve is to add between 1000 to 2000 string variables to the list and later compare them with another strings. So far I can achieve this by using below code:
var list = new List<string>();
var item_1 = "USA";
var item_2 = "Canada";
var item_3 = "Cuba";
...................
var item_N = "Country_N";
list.Add(item_1);
list.Add(item_2);
list.Add(item_3);
.................
list.Add(item_N);
And comparing them with another strings looks like below (using FluentAssertions class):
list[0].Should().Match(stringToCompare_1);
list[1].Should().Match(stringToCompare_2);
list[2].Should().Match(stringToCompare_3);
The biggest problem that I see here is it's sort of hard to memorize what string was added to a particular index of the list (unstoppable scrolling between the code is boring). My question: is there any more elegant way to handle this situation? Something that might look like below (List class method ValueOf is fictional):
var list = new List<string>();
var item_1_USA = "USA";
var item_2_Canada = "Canada";
var item_3_Cuba = "Cuba";
list.ValueOf(item_1_USA).Should().Match(stringToCompare_1);
list.ValueOf(item_1_Canada).Should().Match(stringToCompare_2);
list.ValueOf(item_1_Cuba).Should().Match(stringToCompare_3);
As many suggested (including #Postlagerkarte) to edit the question to clarify what I'm trying to achieve. I'm testing the web app, and while going through every step of this application (imagine booking engine), I need to capture and store different info (like user's First Name, Last Name, Email Address, Phone Number, etc). The amount of the captured info can sometimes exceed 2000 items. Currently I'm using data structure List. Later, at the final step of my application, I need to compare my stored values with what ever present on this final page. For instance: user's first name, that was captured on Step 2 of the booking engine must match with value on final step. List Contains method won't be suitable here as it can validate incorrect information. Accessing the values using list[0]....list[N] is very inconvenient, as I can forget what exactly was stored at that index. I'm solving it now by scrolling through the code to return back. Any wise navigation is appreciated.
You can use a Dictionary<string, string> for this. The key of each value would be a field or info identifier: userFirstName, userAge, etc. As you process data you add it to your dictionary with whatever value you are reading.
When validating, you look up the stored value with the field identifier and compare it to whatever data shows upon your last page.
You can also consider using an enumeration as your key instead of string although if you avoid magic strings literals and use constants you should be ok.
Why can't you use the Contains() method saying
list.Contains(stringToCompare_1);
I'm trying to make an rdlc report in Visual Studio 2015 for a single instance of an object. It seems as though I always have to pass in an enumerable list as the datasource, and it always has a table.
Is it possible to just pass in a single object and not have a table? I want to have a background image and use text boxes to display the data that I want to display.
Thank you for any responses!
There are a couple of ways to accomplish this.
If it's just a single item, why not just use a parameter? Just pass your single value via a parameter.
If you want to go the data source route, yes, you have pass in an enumerable, but that is as easy as this:
var data = new string[] { "my value"}
and using the First function:
=First(Fields!ProductNumber.Value, "Category")
Either way, you can still use textboxes.
This question already has answers here:
How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?
(47 answers)
Closed 9 years ago.
I use Entity Framework together with Linq to fill my gridviews.
I would like to get the same data I get from the Linq in an excel.
Also a second tab in the same excel sheet should come from another Linq query.
Whats the easiest and best way to do this?
There are two parts to this. The first is to serialise your linq collection to CSV. You can serialise an enumerable method like this:
public static string ToCsv<T>(string separator, IEnumerable<T> objectlist)
{
Type t = typeof(T);
FieldInfo[] fields = t.GetFields();
string header = String.Join(separator, fields.Select(f => f.Name).ToArray());
StringBuilder csvdata = new StringBuilder();
csvdata.AppendLine(header);
foreach (var o in objectlist)
csvdata.AppendLine(ToCsvFields(separator, fields, o));
return csvdata.ToString();
}
That method won't help you with deep objects but it will get you to CSV. The second stage is to build the XSLX format (assuming that 2 CSVs won't be enough). For that I would recommend the Open XML SDK from Microsoft to build the two worksheets from your CSV string. I also like this answer for building an Excel spreadsheet.
Currently, I'm populating a drop down list when a file is created in a configurable folder.
if (!downloadRspDropDown.Items.Contains(new ListItem(txt, fileData.FullName))
Then, I add the file and remove "No Responses Available".
However, if the same file is resubmitted (i.e., the file name is the same but the timestamp is different), then I want to remove the older entry and replace it with a new entry in the drop down list.
I have the filename, so I go into the "else" block from the line of code above. From there, I'm checking to see if I have the same filename and a different creation time.
if (downloadRspDropDown.Items.Contains(new ListItem(txt, fileData.FullName) &&
downloadRspDropDown.Items.Contains(new ListItem(txt, fileData.CreationTime)
From here, I want to find the position, remove it, and add the new text. This approach isn't working. Can anyone offer an alternate approach?
I dont know if that is your current problem, but this line is definitly causing trouble:
if (!downloadRspDropDown.Items.Contains(new ListItem(txt, fileData.FullName))
What you probably want to achieve is a check for equality based on a value. But this line of code performs an equality check based on a reference and can't be true, because you create a new ListItem that definitly has another reference than the item already added to the Dropdownlist.
Try to use the .FindByValue method instead.
Edit based on comment:
Try something like this:
ListItem _li = downloadRspDropDown.Items.FindByValue(fileData.FullName);
if(_li != null)
{
downloadRspDropDown.Items.Remove(_li);
}