0
private void okbtn_Click(object sender, EventArgs e)
{
    OleDbConnection conn = new OleDbConnection();
    conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Desktop\GameMuseumManagementSystem.accdb";
      
    try
    {
        conn.Open();

        String Name = txtName.Text.ToString();
        String Email = txtEmail.Text.ToString(); 
        String Password = txtPassword.Text.ToString();

        String my_query = "INSERT INTO Member(Member_Name,Member_Password,Member_Email)VALUES('" + Name + "','" + Email + "','" + Password + "')";

        OleDbCommand cmd = new OleDbCommand(my_query, conn);
        cmd.ExecuteNonQuery();

        MessageBox.Show("Data saved successfuly...!");
    }
    catch (Exception ex)
    {
        MessageBox.Show("Failed due to" + ex.Message);
    }
    finally
    {
        conn.Close();
    }
}

I am coding for the member registeration for a guest to use it. I have 3 pieces of data, member_name, member_ID, and password. I coded this and I get an error. My Visual Studio is connected to my MS Access database via the tools, after I write this code, the data can't be stored in Access, what should I do now? Any suggestion?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mi Ke
  • 1
  • 4
  • 2
    Rule one of databases: do not concatenate input into queries. Rule two: see rule one. Please please please investigate parameters. Rule one of security: do not store passwords, even encrypted (one does not encrypt passwords unless one is writing a password manager). Rule two: see rule one. Please please please investigate secure salted password *hashing*. As a side note, `conn` and `cmd` are both `IDisposable`; it is easier to use `using` than `try`/`finally`. – Marc Gravell Sep 16 '16 at 10:03
  • 2
    Are you so kind to tell us the error message? – Steve Sep 16 '16 at 10:03
  • 1
    "the data cant store in access" - what happens? does it throw an exception? or do you just find that your data isn't in the database when you look afterwards? if the first; what is the exception? if the second: is it possible that you have two different db files, on in the project file, and a **copy** in the `bin/debug` / `bin/release` folder? – Marc Gravell Sep 16 '16 at 10:05
  • 1
    @MarcGravell I bet that reversing the order of the mail and password values to be in the same order of the field names should give a relief, but as you say this code is wrong from many points of view, – Steve Sep 16 '16 at 10:08
  • my input cant put inside queries? – Mi Ke Sep 16 '16 at 11:08
  • Not user input as you open yourself to SQL injection attack https://en.wikipedia.org/wiki/SQL_injection ; you want to use a parameterised query - this Q + A might help http://stackoverflow.com/questions/5893837/using-parameters-inserting-data-into-access-database – Ben Adams Sep 16 '16 at 11:20
  • i dont understand what u mean by SQL injection attack even i google – Mi Ke Sep 17 '16 at 02:25

0 Answers0