views:

32

answers:

1

I want to input data into my table (sql 2008) using linq to sql:

    public static bool saveEmail(Emailadressen email)
    {
        TBL_Emailadressen saveMail = new TBL_Emailadressen();

        destil_loterijDataContext db = new destil_loterijDataContext();


        saveMail.naam = email.naam;
        saveMail.emailadres = email.emailadres;
        saveMail.lotnummer = email.lotnummer;



        try
        {
            saveMail.naam = email.naam;
            saveMail.lotnummer = email.lotnummer;
            saveMail.emailadres = email.emailadres;

            db.TBL_Emailadressens.InsertOnSubmit(saveMail);

            return true;

        }
        catch (Exception ex)
        {
            Console.WriteLine("Opslaan niet gelukt!" + ex.ToString());
            return false;
        }

    }

For some reason nothing is being added to this table. My table has the following fields:

ID (Auto incr int)

Naam (varchar50)

lotnummer (varchar50)

emailadres (varchar50)

My object im trying to save (saveMail) always has an ID = 0 , and i don't know why. I think that is preventing me from saving to the DB?

+7  A: 

Be sure to call SubmitChanges on your DataContext-derived class:

using(var dc = new MyDataContext())
{
    saveEmail(new Emailadressen(...));
    dc.SubmitChanges();
}
Anton Gogolev
Pffff, i'm a noob :). Thanks alot!
Younes