Search code examples
c#sqlms-access-2010oledbparameter

Inserted fields in the wrong place in the table C# OleDB


I'm making a successfull Insert into the table, but for some reason the fields are in a messed up position. Working on Visual Studio 2012 C# with an MS Access Database 2010.

(Don't have enough rep to post imagens directly so please bear with the links) This is what the table structure is like (Sorry but can't post more than 2 links)

( column = fieldtype):

  • Codigo = text
  • Data saida (aka @data) = date/hour
  • Entidade = text
  • Data Ent = data/hour
  • GT Ent = text
  • Estado = text
  • GT saida = text
  • observacaoes = text
  • requisitante = number
  • certificado = text
  • resultado = text
  • selecionar = Yes/No
  • tipo int = text

This is a good example in the table of a correct row like row Good Row

This is a the row I'm getting with the INSERT Messed up Row

This is how I make the INSERT

                OleDbCommand cmd = l.CreateCommand();

                cmd.Parameters.Add(new OleDbParameter("@codigo", cal.CodEtiq.ToString()));
                cmd.Parameters.Add(new OleDbParameter("@data", cal.Data));
                cmd.Parameters.Add(new OleDbParameter("@entidade", cal.EntidadeCal));
                cmd.Parameters.Add(new OleDbParameter("@observacao", cal.Observacao));
                cmd.Parameters.Add(new OleDbParameter("@certificado", cal.Certificado));
                cmd.Parameters.Add(new OleDbParameter("@resultado", cal.Resultado));
                cmd.Parameters.Add(new OleDbParameter("@selecionar", cal.Selecionar));

                cmd.CommandText = "INSERT INTO [Movimento Ferramentas] VALUES (@codigo, @data , @entidade, null, null,  'Calibração', null, @observacao, null, @certificado, @resultado,   @selecionar , null)";   

                result = cmd.ExecuteNonQuery();

What am I doing wrong here?


Solution

  • Since you haven't specified the columns in your INSERT statement, It is inserting values on the bases of column orders in table. Not really sure what is your actual column order, But you can fix your issue by specifying your columns in INSERT statement.

    cmd.CommandText = "INSERT INTO [Movimento Ferramentas] "+
                      "(Codigo , [Data saida], ...............) " + // columns
                      "VALUES (@codigo, @data , @entidade, null, null,  'Calibração', null, @observacao, null, @certificado, @resultado,   @selecionar , null)";