C # CRUD - Insert Error (Can not add or update child row: a foreign key constraint fails ...)

0

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

It's something with id_user but I do not know what it is. I do not insert a txtbox nor combobox into id_user because I do not know what value I have to give it to him. What to do?

Code and prints below!

I'm stuck here for 2 days and I do not know how to solve it.

NOTE If I need more info I give.

InsertCode>

privatevoidbtt_inserir_Click(objectsender,EventArgse){try{stringConexao_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);
        }
    }
    
asked by anonymous 25.05.2018 / 10:59

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 to run Query on 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 surely 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 connection to the database.

Obey good programming practices:

  • Variables or attributes must be declared with their first lower case.
  

You're wrong: string Connection_BD=""; string Query="";

     

The correct one is: DB connection and query. The use of a capital letter   improperly causes your IDE to not easily identify what   are the things in your code. Methods and Class with capital letter e   lowercase attributes.

  • Create a class to manage your connection to the Database. You do not need to be creating a client and closing every button you click on. It also makes it difficult to read the code depending on the situation your program may be heavier in both disk size and RAM memory.
  • Read the error this helps a lot! Maybe the English will complicate a bit but just google the translator.
  

Error: Can not add or update child row: a foreign key constraint fails       ('times'.' records', CONSTRAINT 'registersjbfkl' FOREIGN KEY ('id.useO       REFERENCES users' ('id.user') ON DELETE CASCADE ON UPDATE CASCADE)

     

Error PT: Can not add or update a child row: 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 the most important of all is never giving up. Sometimes you spend hours or days to solve a problem and then resolve in a fraction of a second. Rest your mind, do not stress yourself, you are practicing and your obligation is to learn so enjoy yourself with the problems that you have. If you have felt yourself extresed you will drink some water and rest your mind a little. You are learning and in the end the best teacher is yourself.

In programming, practice speaks louder. Read the simple program codes you want to create in 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 well makes code more readable and greatly eases maintenance later.

    
25.05.2018 / 12:44