Gergely Bakos
Gergely Bakos

Reputation: 21

C# SubmitChanges dont work

I'm trying to make a database. I made a form and I want to get the data from that. Everything seems okay, but my database didn't get the update and didn't change. I'm using a local database. I tried to change "Copy to output directory" but nothing happened... The SubmitChanges seems ok: I get the Message, but after the program is closed the database doesn't contain the new data... I saw a lot of posts with similar problems, but I didn't find a solution.

My Code:

private void felvetButton_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2();
    if (f2.ShowDialog() == DialogResult.OK)
    {
        Match m = new Match();
        try
        {
            m.datum = f2.datumTextBox.Text;
            m.mod_fk = Convert.ToInt32(f2.modTextBox.Text);
            m.acc_fk = Convert.ToInt32(f2.accTextBox.Text);
            m.champion = f2.champTextBox.Text;
            m.szerep_fk = Convert.ToInt32(f2.roleTextBox.Text);
            m.kimenet = f2.resultTextBox.Text;
            m.mhossz = f2.lengthTextBox.Text;
            m.kill = Convert.ToInt32(f2.killTextBox.Text);
            m.death = Convert.ToInt32(f2.deathTextBox.Text);
            m.assist = Convert.ToInt32(f2.assistTextBox.Text);
            m.kda = float.Parse(f2.kdaTextBox.Text);
            m.killpart = float.Parse(f2.kpTextBox.Text);
            m.farm = Convert.ToInt32(f2.farmTextBox.Text);
            m.ward = Convert.ToInt32(f2.wardTextBox.Text);
            m.redward = Convert.ToInt32(f2.redwTextBox.Text);

            db.SubmitChanges();
            MessageBox.Show("New match added to the database!");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

Upvotes: 2

Views: 121

Answers (1)

GantTheWanderer
GantTheWanderer

Reputation: 1292

Don't you have to make the entity layer aware of this new object? I imagine you need to call something like this for new entities:

db.Matches.InsertOnSubmit(match);

Its perfectly fine to load entities from the database via this entity layer, modify them, and then use db.SubmitChanges() because the entity layer already knows about existing objects! How is it supposed to know about this new object if the entity layer doesn't have any reference to it?

Upvotes: 4

Related Questions