Cyro1_
Cyro1_

Reputation: 1

You cannot add or change a record because a related record is required in table 'tbl_routines'

My code is to register an account by storing username and password into an access database. I get the error You cannot add or change a record because a related record is required in table 'tbl_routines. From reading around I think it is because the value in my routine_id column of the main table is being left blank yet in tbl_routine there is no blank routine id as an option. In my program I am trying to register username and password first, and then later on in the program the user chooses a routine and it is stored in the table, but for now it needs to be left blank. I would assume that just adding a username and password into the table wouldn't affect the routine_id column since it is not listed as required, yet this error still pops up, and I don't know how to fix it.

if (txtRegPass.Text == txtRegPassConf.Text)
{
    con.Open();
    string register = "INSERT INTO tbl_users (username, [password]) VALUES ('" + txtRegUser.Text + "','" + txtRegPass.Text + "')"; 
    cmd = new OleDbCommand(register, con); 
    cmd.ExecuteNonQuery(); 
    con.Close();

    MessageBox.Show("Account successfully created.", "Registration Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
    txtRegUser.Text = "";
    txtRegPass.Text = "";
    txtRegPassConf.Text = ""; 
}
else
{
    MessageBox.Show("Passwords do not match.", "Registration Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    txtRegPass.Text = "";
    txtRegPassConf.Text = ""; 
}

Here is the image of the relationship in the database:

tbl_routines 1 to ∞ tbl_users

Upvotes: 0

Views: 111

Answers (1)

Vivek Nuna
Vivek Nuna

Reputation: 1

You can set the routine_id column as nullable in tbl_users table. In that case it won’t create this problem.

Upvotes: 0

Related Questions