0

what my code is supposed to do is add new rows with new drills to a datagridview, my problem is whenever i add a new row to my datagridview this creates a new id at the top of the datagridview with the wrong number, im using the following code to add new rows.

    private void button5_Click(object sender, EventArgs e)
    {
        string strcon = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Directory.GetCurrentDirectory() + @"\database\DatabaseENG.accdb"; // String de Conexão com a Database
        string comando = @"INSERT INTO [Norms Table] 
                        ([Drills (according to the norm EN 61439-2)])
                        values
                        (@drills)"; // Query de Inserção de Normas 

        //Representa uma conexão aberta a uma Datasource
        using (OleDbConnection con = new OleDbConnection(strcon))
        {
            using (OleDbCommand com = new OleDbCommand(comando, con))
            {
                // Igualar valor dos parametro a textbox
                com.Parameters.Add("@drills", OleDbType.VarChar).Value = textBox5.Text;

                //try and catch para efetuar a conexão com a database
                try
                {
                    con.Open();
                    com.ExecuteNonQuery();
                    MessageBox.Show("Save Well Succeded !");
                    norms_TableDataGridView.Refresh();
                    button9_Click(sender, e);

                }
                catch (Exception E)
                {
                    MessageBox.Show(E.Message);
                }
            }
        }

    }

if you need any extra code to understand please let me know, thanks for any help in advance, the following image is an example of what is happening. enter image description here

Souz4x
  • 45
  • 5
  • "*this creates a new id at the bottom of the datagridview*" - it is not clear to me what you mean by this. – Crowcoder Feb 23 '21 at 23:28
  • im sorry, this creates a new id at the top of the datagridview with the wrong number – Souz4x Feb 23 '21 at 23:32
  • If that is an autonumber then it is not wrong, it is the next available identity value. You probably have some deleted rows. – Crowcoder Feb 23 '21 at 23:34
  • yes i deleted 1 row before, but i tried without deleting none and it kept giving id 21 when it has to 20 – Souz4x Feb 23 '21 at 23:40
  • You lost me a little there, but according [to this](https://stackoverflow.com/questions/6498221/retrieve-next-autonumber-for-access-table) you can check the next autonumber to see if it lines up with what you are getting. – Crowcoder Feb 23 '21 at 23:46
  • Sorry, I am lost in that example, my knowledge of C# is very basic What I inteded to say is that my problem is: when I add a new row it comes with the wrong ID, for example, in my original DGV I have 19 rows (without counting the headers) when I add another row it comes with ID = 21 instead of ID = 20 If I delete one row and then add another, it's ID will still be 21 for some reason – Souz4x Feb 23 '21 at 23:49
  • I'm less familiar with ms access internals than ms sql server, but I don't think you have a problem at all. I believe if you compact and repair the database you will recover unused autonumber values. But you should not care about autonumber keys, just let the database deal with it, it doesn't matter if they are not contiguous. – Crowcoder Feb 23 '21 at 23:55
  • thank you for trying to help, but this is for an apresentation so i really need to get this fixed. – Souz4x Feb 23 '21 at 23:59
  • There is nothing to fix, it is not broken. It is common for auto generated numeric keys to have gaps. If you need contiguous values then delete everything from the table, compact and repair the database, and then re-insert everything. That should work until you delete something or something else causes a gap. – Crowcoder Feb 24 '21 at 00:02

0 Answers0