I am working on a project (simple phone book) for personal use. Basically, the contacts are displayed in a listview and stored in a XML file. I am having trouble deleting the last remaining item in my listview (listView1).
This is how my XML looks like:
<People>
<Person>
<Name>John</Name>
<Hometown>Washington</Hometown>
<Address>231 Main Street</Address>
<Birthday>130299511993682082</Birthday>
<Phone>555-555-555</Phone>
<Email>john@gmail.com</Email>
<AdditionalInfo>Skype: john.123</AdditionalInfo>
</Person>
<Person>
<Name>123</Name>
<Hometown>
</Hometown>
<Address>
</Address>
<Birthday>130299621669230698</Birthday>
<Phone>
</Phone>
<Email>
</Email>
<AdditionalInfo>
</AdditionalInfo>
</Person>
</People>
So, let's say that I have got 5 contacts in the list and when I try to remove all of them, it's not possible. It is possible to remove only 4 of them. When I try to remove all of them and then close/run the application, there will be no removed contacts - all of them will still be there, like I haven't removed them at all. When I try to remove 4 of them and I close/run the program, they would be deleted. When I try to remove the last one, it is not possible either - when I close/run app it would always remain there.
Also, sometimes there is an error as well, when I try to just select some of the contacts - usually after already deleting them. So, just to make things clear, let's say that I have got 10 contacts and remove 5 of them, when I try to select the sixth contact - the error shows:
IMAGE
I think maybe this part of the code is bugged:
private void listView1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (listView1.SelectedItems.Count == 0)
{
toolStripEdit.Enabled = false;
RefreshAll();
return;
}
Person person = new Person();
person = FindPerson(listView1.SelectedItems[0].Text);
txt_Name.Text = person.Name;
txt_City.Text = person.Hometown;
txt_Address.Text = person.Address;
txt_Phone.Text = person.Phone;
txt_Mail.Text = person.Email;
txt_MoreInfo.Text = person.AdditionalInfo;
dateTimePicker1.Value = person.Birthday;
ReadOnlyON();
toolStripEdit.Enabled = true;
ExpandThis();
}
and this one as well:
void Rmv()
{
if (Properties.Settings.Default.Remove == true)
{
DialogResult dialogResult = MessageBox.Show("Are you sure you want to delete this contact?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dialogResult == DialogResult.Yes)
{
Remove();
if (Properties.Settings.Default.Sync == true) { Sync(); }
}
else if (dialogResult == DialogResult.No)
{
return;
}
}
else
{
Remove();
if (Properties.Settings.Default.Sync == true) { Sync(); }
}
}
void Remove()
{
Person person = new Person();
person = FindPerson(listView1.SelectedItems[0].Text);
if (listView1.SelectedItems.Count > 0)
{
try
{
if (listView1.SelectedItems.Count == 0) return;
people.RemoveAt(listView1.SelectedItems[0].Index);
foreach (ListViewItem eachItem in listView1.SelectedItems)
{
listView1.Items.Remove(eachItem);
}
}
catch { }
ClearAll();
ReadOnlyON();
}
else
{
MessageBox.Show("Nothing is selected!", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
ReadOnlyOFF();
UserCount();
if (Properties.Settings.Default.Sync == true) { Sync(); }
}
Since it doesn't makes sense to upload the whole code here, I have uploaded it
here if someone wants to help and take a closer look at it.
Thanks in advance.
How can I solve this issue?
@EDIT:
This is the connection between listview and XML:
private void Main_Load(object sender, EventArgs e)
{
LoadContacts();
}
void LoadContacts()
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string phonebook_path = path + "\\Phonebook\\Contacts.xml";
if (!File.Exists(phonebook_path))
{
XmlTextWriter xW = new XmlTextWriter(phonebook_path, Encoding.UTF8);
xW.WriteStartElement("People");
xW.WriteEndElement();
xW.Close();
}
XmlDocument xDoc = new XmlDocument();
xDoc.Load(phonebook_path);
foreach (XmlNode xNode in xDoc.SelectNodes("People/Person"))
{
Person p = new Person();
p.Name = xNode.SelectSingleNode("Name").InnerText;
p.Hometown = xNode.SelectSingleNode("Hometown").InnerText;
p.Address = xNode.SelectSingleNode("Address").InnerText;
p.Birthday = DateTime.FromFileTime(Convert.ToInt64(xNode.SelectSingleNode("Birthday").InnerText));
p.Phone = xNode.SelectSingleNode("Phone").InnerText;
p.Email = xNode.SelectSingleNode("Email").InnerText;
p.AdditionalInfo = xNode.SelectSingleNode("AdditionalInfo").InnerText;
people.Add(p);
listView1.Items.Add(p.Name);
UserCount();
}
}
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
XmlDocument xDoc = new XmlDocument();
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string phonebook_path = path + "\\Phonebook\\Contacts.xml";
xDoc.Load(phonebook_path);
XmlNode xNode = xDoc.SelectSingleNode("People");
xNode.RemoveAll();
foreach (Person p in people)
{
XmlNode xTop = xDoc.CreateElement("Person");
XmlNode xName = xDoc.CreateElement("Name");
XmlNode xHometown = xDoc.CreateElement("Hometown");
XmlNode xAddress = xDoc.CreateElement("Address");
XmlNode xBirthday = xDoc.CreateElement("Birthday");
XmlNode xPhone = xDoc.CreateElement("Phone");
XmlNode xEmail = xDoc.CreateElement("Email");
XmlNode xAdditionalInfo = xDoc.CreateElement("AdditionalInfo");
xName.InnerText = p.Name;
xHometown.InnerText = p.Hometown;
xAddress.InnerText = p.Address;
xBirthday.InnerText = p.Birthday.ToFileTime().ToString();
xPhone.InnerText = p.Phone;
xEmail.InnerText = p.Email;
xAdditionalInfo.InnerText = p.AdditionalInfo;
xTop.AppendChild(xName);
xTop.AppendChild(xHometown);
xTop.AppendChild(xAddress);
xTop.AppendChild(xBirthday);
xTop.AppendChild(xPhone);
xTop.AppendChild(xEmail);
xTop.AppendChild(xAdditionalInfo);
xDoc.DocumentElement.AppendChild(xTop);
xDoc.Save(phonebook_path);
}
}