C# CRUD - Insert Error (Cannot add or update a Child Row: a Foreign key Constraint fails...)

Asked

Viewed 413 times

0

Good morning, I had managed to do everything right up to this error by inserting something into the database. localhost/phpmyadmin - xampp.

It’s something with the id_user but I don’t know what it is. I don’t use a single insert txtbox nor combobox to the id_user because I don’t know what value I have to give him. What to do?

Code and prints below!

I’m stuck here for two days and I don’t know how to fix it.

NOTE if it is perciso more INFO I give.

Erro ao Inserir na BD Form Tabela registos - Relation View Tabela registos e as suas colunas Tabela users e as suas colunas

Code Insert>

    private void btt_inserir_Click(object sender, EventArgs e)
    {
        try
        {

            string Conexao_BD = "datasource=127.0.0.1;port=3306;username=root;password=;database=tempos;SslMode = none;";

            string Query = "INSERT INTO `registos`(`id_registo`, `data`, `id_tipo`, `id_tec`, `id_user`, `id_processo`, `id_tarefa`, `horas`, `comentarios`) VALUES (NULL,'" + datatp_1.Text + "','" + comboBox_tipo1.Text + "','" + comboBox_tec1.Text + "','" + txt_teste.Text + "','" + comboBox_pro1.Text + "','" + comboBox_passos1.Text + "','" + txt_horas1.Text + "','" + txt_com1.Text + "')";

            MySqlConnection CONEXAO_BD2 = new MySqlConnection(Conexao_BD);
            MySqlCommand Comando2 = new MySqlCommand(Query, CONEXAO_BD2);
            MySqlDataReader Ler_BD2;
            CONEXAO_BD2.Open();
            Ler_BD2 = Comando2.ExecuteReader();     
            msg.Inserir();


            CONEXAO_BD2.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
  • Briefly the message says that you are trying to insert an item in registos that is not informing the id_user or is informing a id_user invalid (which does not exist in the table users). I see you’re getting this information from txt_teste.Text, make sure this field is passing a valid value.

  • Yes, that was it but I decided to put the ID value, either 1(for user) or 2(for admin) but now appeared the same error but with the combobox_type1 A of the Types! And I don’t know why or how to solve it. And I would like to know how to eliminate the id_users because I have it but I don’t use it

  • You got it the previous problem? If you don’t need it, would just delete that column from the table in the bank and go on to not declare it further in your instructions.

  • The problem is that when I try to delete the id_user column from the table records from this error: https://imgur.com/a/jsZkKO2

  • Where do I delete it in Relation View?

  • I can’t say. I’m sorry.

Show 1 more comment

1 answer

1


Friend your problem is in the syntax of your Query. Do not assign value to ids that are auto-increment in your INSERT.

I advise you to try running the Query at hand to be simpler to solve the problem. Close your IDE for now and try to register manually. There are several alternatives: through the Console, Mysql Workbench or directly from phpmyadmin that will certainly be simpler for you. How to run SQL in phpmyadmin?

1 - Fix your query because it is wrong.

2 - Implement the query in your program.

3 - I really advise you to create a class to better manage your database connection.

Obey good programming practices:

  • Variables or attributes must be declared with their first minuscule letter.

You are wrong: string Conexao_bd = "";string Query = "";

Correct is: connectionBD and query. Using uppercase letter improperly causes your IDE not to easily identify what are the things in your code. Methods and Class with uppercase letter and attributes with minuscule letter.

  • Create a class to manage your connection to the Database. You don’t need to be creating a client and closing every button you click. Besides it makes it difficult to read the code depending on the situation your program may get heavier both in disk size and execution in memory RAM.
  • Read the error this helps a lot! Maybe English complicates a little but just go on google translator.

Error: Cannot add or update a Child Row: a Foreign key Constraint fails ('times'. 'records', CONSTRAINT 'registosjbfkl' FOREIGN KEY ('useid. REFERENCES users' ('id.user') ON DELETE CASCADE ON UPDATE CASCADE)

EN error: Unable to add or update a child line: a foreign key constraint fails ('times'. 'Records', CONSTRAINT 'debatejbfkl' FOREIGN KEY ('id.useo REFERENCES users' ('id.user') ON DELETE CASCADE ON UPDATE CASCADE)

Word of Motivation for you! rsrs And most important of all, never give up. Sometimes you spend hours or days solving a problem and end up solving it in a fraction of a second. Rest your mind, don’t stress you are interning and your obligation is to learn so have fun with the problems that pass you. If you have felt ecstatic go get a drink of water and rest your mind a bit. You’re learning and at the end of the day the best teacher is yourself.

In programming the practice speaks louder. Read simple program codes that you have desire to create in the Github. Note the organization of classes and the way they use object orientation. A code in which the developer knows how to use object orientation makes the code more readable and makes maintenance much easier later.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.