0

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?

1 Answer 1

7

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

using(var dc = new MyDataContext())
{
    saveEmail(new Emailadressen(...));
    dc.SubmitChanges();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Pffff, i'm a noob :). Thanks alot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.