user962206
user962206

Reputation: 16117

Query in LINQ TO SQL not inserting

I've created a simple query in adding users in the database but there error is that the data is not showing. here's my code

private void button1_Click(object sender, EventArgs e)
        {
            using (DataClasses1DataContext myDbContext = new DataClasses1DataContext(dbPath))
            {

                //Instantiate a new Hasher Object
                var hasher = new Hasher();

                hasher.SaltSize = 16;

                //Encrypts The password
                var encryptedPassword = hasher.Encrypt(txtPass.Text);

                Account newUser = new Account();

                newUser.accnt_User = txtUser.Text;
                newUser.accnt_Position = txtPosition.Text;


                // Replace AccountTableName with the actual table
                // name found in your dbml's context
                myDbContext.Accounts.InsertOnSubmit(newUser);
                myDbContext.SubmitChanges();
                MessageBox.Show("XD");
            }
        }

And My Table data shows only this

enter image description here

Upvotes: 2

Views: 1427

Answers (1)

Joel Etherton
Joel Etherton

Reputation: 37533

You need to set the InsertOnSubmit directly on the context:

Edit: Using statement added. Reminder courtesy of @David Khaykin

using (DataClasses1DataContext myDbContext = new DataClasses1DataContext(dbPath))
{

    //Instantiate a new Hasher Object
    var hasher = new Hasher();

    hasher.SaltSize = 16;

    //Encrypts The password
    var encryptedPassword = hasher.Encrypt(txtPass.Text);

    Account newUser = new Account();

    newUser.accnt_User = txtUser.Text;
    newUser.accnt_Position = txtPosition.Text;
    newUser.accnt_Position = encryptedPassword;

    // Replace AccountTableName with the actual table
    // name found in your dbml's context
    myDbContext.AccountTableName.InsertOnSubmit(newUser);
    myDbContext.SubmitChanges();
}

Upvotes: 3

Related Questions