inserir em arquivo XML
ta dando erro nesse trecho
codigo completo
int next = contatos.Contato[contatos.Contato.Count - 1].Id + 1;
codigo completo
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.Xml;
namespace System.XML_Example
{
public partial class Form3 : Form
{
Contatos contatos = null;
public Form3()
{
InitializeComponent();
}
private void BindListBox() {
contatos = SContatos.Read();
listBox1.DataSource = contatos.Contato;
listBox1.DisplayMember = "Nome";
listBox1.ValueMember = "Id";
}
private void Form3_Load(object sender, EventArgs e)
{
this.BindListBox();
}
private void btnSalvar_Click(object sender, EventArgs e)
{
Contato c = new Contato();
c.Id = this.NextId();
c.Nome = txtNome.Text;
c.Telefone = txtTelefone.Text;
contatos.Contato.Add(c);
SContatos.Write(contatos);
this.BindListBox();
}
private int NextId() {
int next = contatos.Contato[contatos.Contato.Count - 1].Id + 1;
return next;
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex > -1)
{
Contato c = contatos.Contato.Find(p => p.Id == (int)listBox1.SelectedValue);
contatos.Contato.Remove(c);
SContatos.Write(contatos);
this.BindListBox();
}
else
{
MessageBox.Show("Nenhum ítem selecionado");
}
}
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
Contato c = contatos.Contato.Find(p => p.Id == (int)listBox1.SelectedValue);
MessageBox.Show("Nome: " + c.Nome + "\n" + "Telefone: " + c.Telefone);
}
}
}
Roniere Almeida
Curtidas 0
Respostas
Joel Rodrigues
21/06/2013
Que erro, cara?
GOSTEI 0
Roniere Almeida
21/06/2013
When using the overloaded-two-argument FindString or FindExacString methods with a ComboBox or ListBox, check the startIndex parameter.
if you are working with a collection, make sure the index is less than the size of the collection.
Make sure the arguments to this method have valid values.
Get general help for this exception.
if you are working with a collection, make sure the index is less than the size of the collection.
Make sure the arguments to this method have valid values.
Get general help for this exception.
GOSTEI 0
Joel Rodrigues
21/06/2013
Põe um breakpoint nessa linha e veja se a lista não está vazia.
GOSTEI 0
Roniere Almeida
21/06/2013
vc ta se referindo ao arquivo XML?
GOSTEI 0
Joel Rodrigues
21/06/2013
Na verdade eu não sabia nem que tinha um XML aí no meio. Me refiro a contatos.Contato.Count. Isso tem de ser maior que zero, indicando que tem itens na lista.
GOSTEI 0
Roniere Almeida
21/06/2013
esqueci de mencionar esse arquivo, os dados são inseridos nele, desculpa.
GOSTEI 0
Joel Rodrigues
21/06/2013
Provavelmente a lista de contatos está vazia. É preciso verificar isso.
GOSTEI 0
Roniere Almeida
21/06/2013
ah é isso mesmo, o que posso fazer para adicionar via aplicação
GOSTEI 0
Joel Rodrigues
21/06/2013
Bom, aí já tem um método para adicionar itens á lista. O que ocorre é que você usa o método NextId para criar um novo Id, mas se a lista não tem nada, ele nunca vai encontrar esse primeiro Id.
GOSTEI 0
Joel Rodrigues
21/06/2013
Resumindo, você precisa tratar o método NextId para que, caso não existam itens na lista, ele retorne o Id=1;
GOSTEI 0
Roniere Almeida
21/06/2013
ok, vou tentar resolver, obrigado Joel.
GOSTEI 0
Aluisio Cavalcante
21/06/2013
é possivel fazer um sisteminha de cadastro usando esse arquivo xml ou apenas um arquivo tipo txt?
GOSTEI 0
Joel Rodrigues
21/06/2013
é possivel fazer um sisteminha de cadastro usando esse arquivo xml ou apenas um arquivo tipo txt?
Pode-se perfeitamente fazer um cadastro utilizando XML, Aluisio. Dê uma olhada nesse post de minha autoria, nele eu dou um exemplo básico de como fazer isso: [url]http://www.linhadecodigo.com.br/artigo/3449/manipulando-arquivos-xml-em-csharp.aspx[/url].
GOSTEI 0
Joel Rodrigues
21/06/2013
Roniere, conseguiu resolver?
GOSTEI 0
Roniere Almeida
21/06/2013
sim Joel, 100%
GOSTEI 0
Joel Rodrigues
21/06/2013
sim Joel, 100%
Obrigado, amigo, pelo retorno.Sendo assim, estou encerrando o tópico.
Abraço.
GOSTEI 0