问题描述:

I have problem with saving my combobox data. When saving fist time I don't have problem, but when I do it again with the same combobox value I receive the following error:

DbUpdateException was unhandled An error occurred while updating the entries.

private void FillCombo1()

{

context = new PhonebookEntities();

comboBox1.DataSource = context.Cities.ToList();

comboBox1.DisplayMember = "City";

comboBox1.SelectedIndex = -1;

}

private void FillCombo3()

{

context = new PhonebookEntities();

comboBox3.DataSource = context.Firmas.ToList();

comboBox3.DisplayMember = "Firma";

//comboBox3.Invalidate();

comboBox3.SelectedIndex = -1;

}

private void save_Click(object sender, EventArgs e)

{

context = new PhonebookEntities();

int new_id;

if (context.Books.Count() < 1)

{

new_id = 1;

}

else

{

int curr_id = context.Books.Max(b => b.id);

new_id = curr_id + 1;

}

Book newbook = new Book();

newbook.id_firma = Convert.ToInt32(comboBox3.SelectedIndex.ToString());

newbook.id = new_id;

newbook.name = name1.Text;

newbook.phone = phone1.Text;

newbook.phone2 = phone2.Text;

newbook.note = note.Text;

context.Books.Add(newbook);

context.SaveChanges();

MessageBox.Show("Записът е успешен");

LoadBook();

}

This is my database schema:

相关阅读:
Top