I keep getting a NullReferenceException at this line UserRoot.Element("User_ID").Value = User.User_ID.ToString();
What exactly am I doing wrong?
Here's the majority of the code for that method
if (File.Exists(Path2UserDB + User.User_ID.ToString() + ".db") == false)
{
File.Create(Path2UserDB + User.User_ID.ToString() + ".db");
}
XElement UserRoot = new XElement("User");
UserRoot.Element("User_ID").Value = User.User_ID.ToString();
UserRoot.Element("Full_Name").Value = User.Full_Name;
UserRoot.Element("Gender").Value = User.Gender;
UserRoot.Element("BirthDate").Value = User.BirthDate.ToString();
UserRoot.Element("PersonType").Value = User.PersonType.ToString();
UserRoot.Element("Username").Value = User.Username;
UserRoot.Element("Password").Value = User.Password;
UserRoot.Element("Email_adddress").Value = User.Email_Address;
XDocument UserDoc = new XDocument();
UserDoc.Save(Path2UserDB + User.User_ID.ToString() + ".db");
Thanks
I know that saving Usernames and Passwords in plain text is incredibly unsafe, but this is only going to be accessed by one process that I will eventually implement strong security
The Element("User_ID") method returns an existing element named <User_ID>, if any.
Since your XML element is empty, it returns null.
You should create your XML like this:
var userDoc = new XDocument(
new XElement("User",
new XElement("User_ID", User.User_ID),
new XElement("Full_Name", User.Full_Name),
new XElement("Gender", User.Gender),
...
)
);
Alternatively, you can call the Add method to add a node to an existing element.
You are getting this error, because there is no XML element called User_ID under UserRoot to set its value. If you comment it out, you will get the same error on the next line and so on for every other Element, since you haven't added Elements with thos names. To create the tree that you want, try this:
XElement UserRoot =
new XElement("User",
new XElement("User_ID", User.User_ID.ToString()),
new XElement("Full_Name", User.Full_Name),
new XElement("Gender", User.Gender),
new XElement("BirthDate", User.BirthDate.ToString()),
new XElement("PersonType", User.PersonType.ToString()),
new XElement("Username", User.Username),
new XElement("Password", User.Password),
new XElement("Email_adddress", User.Email_Address)
);
The following MSDN link on XML Tree Creation with XElement will be of help.
You want to check if the value is null or empty before running methods on it.
if(!String.IsnullorEmpty(User.User_ID))
UserRoot.Element("User_ID").Value = User.User_ID.ToString();
Related
I have issue with iTextSharp. Let's assume I have two rows of fields in PDF file (the file is given and I don't know how was created)
Row 1:
data[0].#subform[0].Tabella1[0].Riga2[0].DATA[0]
data[0].#subform[0].Tabella1[0].Riga2[0].ORAINIPM[0]
data[0].#subform[0].Tabella1[0].Riga2[0].ORAINILM[0]
data[0].#subform[0].Tabella1[0].Riga2[0].ORAENDLM[0]
data[0].#subform[0].Tabella1[0].Riga2[0].ORAENDAM[0]
data[0].#subform[0].Tabella1[0].Riga2[0].ORAINIPP[0]
data[0].#subform[0].Tabella1[0].Riga2[0].ORAINILP[0]
data[0].#subform[0].Tabella1[0].Riga2[0].ORAENDLP[0]
data[0].#subform[0].Tabella1[0].Riga2[0].ORAENDAP[0]
Row 2:
data[0].#subform[0].Tabella1[0].Riga3[0].DATA[0]
data[0].#subform[0].Tabella1[0].Riga3[0].ORAINIPM[0]
data[0].#subform[0].Tabella1[0].Riga3[0].ORAINILM[0]
data[0].#subform[0].Tabella1[0].Riga3[0].ORAENDLM[0]
data[0].#subform[0].Tabella1[0].Riga3[0].ORAENDAM[0]
data[0].#subform[0].Tabella1[0].Riga3[0].ORAINIPP[0]
data[0].#subform[0].Tabella1[0].Riga3[0].ORAINILP[0]
data[0].#subform[0].Tabella1[0].Riga3[0].ORAENDLP[0]
data[0].#subform[0].Tabella1[0].Riga3[0].ORAENDAP[0]
I read this fields using below code:
String newFile = source.Insert(source.Length - 4, "newModyfiy");
using (FileStream outFile = new FileStream(newFile, FileMode.Create))
{
PdfReader pdfReader = new PdfReader(source);
foreach (KeyValuePair<String, AcroFields.Item> kvp in pdfReader.AcroFields.Fields)
{
int fileType = pdfReader.AcroFields.GetFieldType(kvp.Key);
string filedValue = pdfReader.AcroFields.GetField(kvp.Key);
string transFileName = pdfReader.AcroFields.GetTranslatedFieldName(kvp.Key);
textBox1.Text = textBox1.Text + fileType.ToString() + " " + filedValue + " " + transFileName + Environment.NewLine;
}
pdfReader.Close();
}
I am getting for both rows values of the first row only. My target is to write values to those fields and save new file. When I use:
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create), '\0', true);
I always overwrite values of first row (when I try to set value in second row it appears in first). If I change the last parameter PdfStamper to false it writes fileds correctlly but file is not editable manually.
Is it a matter of pdf file? Is there a way to read and then write values to proper fileds?
I have spent on this few days and could not find reason of this strange behaviour.
Any small help or even clue will be appereciated.
Edit:
I add mentioned PDF file.
https://ufile.io/mwni5
I have deleted some object but general structure is kept.
I am trying to read a file within a controller and store some data in an object, but I cant manage to save it properly. Can anyone help? I am new in Groovy/Grails...
File generals = new File("C:/Grails/Grails-3.3.0/ggts/Test/data.txt")
def line = generals.readLines()
def date = new SetDate(params)
date.save()
date.title = ${line[0]}
date.location = ${line[1]}
date.description = ${line[2]}
date.name = ${line[3]}
date.email = ${line[4]}
date.save()
You may change ${line[0]} to "${line[0]}" and all things alike if you want to use string interpolation.
And as line is a list of String, change ${line[0]} to line[0] is also ok.
I have the code below that splits a text file from IsolatedStorage, populates an Array with the data, sorts it, and then assigns it as the source for a ListPicker:
var splitFile = fileData.Split(';');
string[] testArray = splitFile;
Array.Sort<string>(testArray);
testLocationPicker.ItemsSource = testArray;
However, it doesn't seem to populating the array correctly and the sorting doesn't appear to be working as expected either.
The testArray[0] is blank, when it should be populated. When the output is shown the entry that should be at [0] appears at the bottom.
BEFORE SORTING:
AFTER SORTING:
It's only in sorting the array that it seems to screw up the order.
UPDATE: I tried the suggested:
var splitFile = fileData.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
string[] testArray = splitFile;
Array.Sort<string>(testArray);
testLocationPicker.ItemsSource = testArray;
This still results in the second screenshot, above.
When the app first ever runs I do this:
StringBuilder sb = new StringBuilder(); // Use a StringBuilder to construct output.
var store = IsolatedStorageFile.GetUserStoreForApplication(); // Create a store
store.CreateDirectory("testLocations"); // Create a directory
IsolatedStorageFileStream rootFile = store.CreateFile("locations.txt"); // Create a file in the root.
rootFile.Close(); // Close File
string[] filesInTheRoot = store.GetFileNames(); // Store all files names in an array
Debug.WriteLine(filesInTheRoot[0]); // Show first file name retrieved (only one stored at the moment)
string filePath = "locations.txt";
if (store.FileExists(filePath)) {
Debug.WriteLine("Files Exists");
StreamWriter sw =
new StreamWriter(store.OpenFile(filePath,
FileMode.Open, FileAccess.Write));
Debug.WriteLine("Writing...");
sw.WriteLine("Chicago, IL;");
sw.WriteLine("Chicago, IL (Q);");
sw.WriteLine("Dulles, VA;");
sw.WriteLine("Dulles, VA (Q);");
sw.WriteLine("London, UK;");
sw.WriteLine("London, UK (Q);");
sw.WriteLine("San Jose, CA;");
sw.WriteLine("San Jose, CA (Q);");
sw.Close();
Debug.WriteLine("Writing complete");
}
Then when I add to the file I do this:
StringBuilder sb = new StringBuilder(); // Use a StringBuilder to construct output.
var store = IsolatedStorageFile.GetUserStoreForApplication(); // Create a store
string[] filesInTheRoot = store.GetFileNames(); // Store all files names in an array
Debug.WriteLine(filesInTheRoot[0]); // Show first file name retrieved (only one stored at the moment)
byte[] data = Encoding.UTF8.GetBytes(locationName + ";");
string filePath = "locations.txt";
if (store.FileExists(filePath))
{
using (var stream = new IsolatedStorageFileStream(filePath, FileMode.Append, store))
{
Debug.WriteLine("Writing...");
stream.Write(data, 0, data.Length); // Semi Colon required for location separation in text file
stream.Close();
Debug.WriteLine(locationName + "; added");
Debug.WriteLine("Writing complete");
}
}
I'm splitting using a ";" could this be an issue?
There's no problem with the sort: 'space' is considered to come before 'a', so it appears on top of the list. The real problem is: why do you have an empty entry to begin with?
My guess is that, when creating the file, you're separating every entry with ;, including the last one. Therefore, when parsing the data with the string.Split method, you're left with an empty entry at the end of your array.
An easy way to prevent that is to use an overload of the string.Split method that filters empty entries:
var splitFile = fileData.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
I went a different way using IsolatedStorageSetting and storing Arrays/Lists to do what I wanted.
I have a solution that works for what I want, but I'm hoping to get some slick LINQ types to help me improve what I have, and learn something new in the process.
The code below is used verify that certain column names exist on a spreadsheet. I was torn between using column index values or column names to find them. They both have good and bad points, but decided to go with column names. They'll always exist, and sometimes in different order, though I'm working on this.
Details:
GetData() method returns a DataTable from the Excel spreadsheet. I cycle through all the required field names from my array, looking to see if it matches with something in the column collection on the spreadsheet. If not, then I append the missing column name to an output parameter from the method. I need both the boolean value and the missing fields variable, and I wasn't sure of a better way than using the output parameter. I then remove the last comma from the appended string for the display on the UI. If the StringBuilder object isn't null (I could have used the missingFieldCounter too) then I know there's at least one missing field, bool will be false. Otherwise, I just return output param as empty, and method as true.
So, Is there a more slick, all-in-one way to check if fields are missing, and somehow report on them?
private bool ValidateFile(out string errorFields)
{
data = GetData();
List<string> requiredNames = new [] { "Site AB#", "Site#", "Site Name", "Address", "City", "St", "Zip" }.ToList();
StringBuilder missingFields = null;
var missingFieldCounter = 0;
foreach (var name in requiredNames)
{
var foundColumn = from DataColumn c in data.Columns
where c.ColumnName == name
select c;
if (!foundColumn.Any())
{
if (missingFields == null)
missingFields = new StringBuilder();
missingFieldCounter++;
missingFields.Append(name + ",");
}
}
if (missingFields != null)
{
errorFields = missingFields.ToString().Substring(0, (missingFields.ToString().Length - 1));
return false;
}
errorFields = string.Empty;
return true;
}
Here is the linq solution that makes the same.
I call the ToArray() function to activate the linq statement
(from col in requiredNames.Except(
from dataCol in data
select dataCol.ColumnName
)
select missingFields.Append(col + ", ")
).ToArray();
errorFields = missingFields.ToString();
Console.WriteLine(errorFields);
I have a requirement to display lists of newly-created and updated pages in our Episerver intranet - say the last ten of each. I've tried using FindPagesWithCriteria but this returns no results. Here's the code I've tried:
PageDataCollection recentPages;
PropertyCriteriaCollection criteria;
PropertyCriteria upperBound;
PropertyCriteria lowerBound;
criteria = new PropertyCriteriaCollection();
upperBound = new PropertyCriteria();
upperBound.Condition = CompareCondition.LessThan;
upperBound.Type = PropertyDataType.Date;
upperBound.Value = DateTime.Today.ToString();
upperBound.Name = "Created"; // Or Saved for updated pages
criteria.Add(upperBound);
lowerBound = new PropertyCriteria();
lowerBound.Condition = CompareCondition.GreaterThan;
lowerBound.Type = PropertyDataType.Date;
lowerBound.Value = DateTime.Today.AddDays(-7).ToString();
lowerBound.Name = "Created";
criteria.Add(lowerBound);
recentPages = DataFactory.Instance.FindPagesWithCriteria(PageReference.StartPage, criteria);
I've also tried using the RecentlyChangedPagesFinder (as detailed here) - this returns some results, but when I try to use the set of results to build a PageCollection to databind into a PageList, again I get nothing output. And I can't see that I could use that for new pages, only updated ones.
The property name should be "PageCreated".
http://epiwiki.se/developing/properties/all-built-in-properties
You can also improve your FindPagesWithCriteria-syntax by going something like this:
var criterias = new PropertyCriteriaCollection
{
new PropertyCriteria()
{
Name = "SomeProp",
Type = PropertyDataType.PageType,
Value = "eh",
Condition = CompareCondition.Equal,
Required = true
},
new PropertyCriteria()
{
...
};
var pages = DataFactory.Instance.FindPagesWithCriteria(somePageLink, criterias);