2

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

4
  • just asking, have you closed the dialog which shows the table data and refreshed the page and checked again? Commented Feb 24, 2012 at 18:25
  • You're setting the Position twice. The second should probably be the password. Typo? Commented Feb 24, 2012 at 18:25
  • How do I insert the encrypted password? it gave me an error that it cannot be converted to a string Commented Feb 24, 2012 at 18:34
  • Did you update the question? Commented Jan 29, 2019 at 13:49

1 Answer 1

3

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();
}
Sign up to request clarification or add additional context in comments.

10 Comments

Exactly correct - and @user962206, I would recommend wrapping the whole thing in a using {} statement; i.e., using (DataClasses1DataContext myDbContext = new DataClasses1DataContext(dbPath)) { // your code }
@David Khaykin it gave me this error Error 2 'PAS.DataClasses1DataContext' does not contain a definition for 'Context' and no extension method 'Context' accepting a first argument of type 'PAS.DataClasses1DataContext' could be found (are you missing a using directive or an assembly reference?) C:\Users\John\Documents\Visual Studio 2010\Projects\PAS\PAS\AddUserWindow.cs 51 25 PAS
@user962206: Check the DBML file and make sure the "accounts" table is included; if it is not found in the context, it means the DBML file doesn't contain that table.
@user962206 that is correct, DataClasses1DataContext does not contain a property called context; the "context" is the name of the variable that represents your DataClasses1DataContext, in your example, it is "myDbContext"; so if the table name is accounts, and your variable is called myDbContext, then you write myDbContext.accounts.InsertOnSubmit(newUser); myDbContext.SubmitChanges() - just like shown in this answer.
@David Khaykin: +1 on the using statment. In fact I'm going to edit to incorporate.
|

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.