Insert Sequential Number into column C#

Asked

Viewed 175 times

1

I have this table called Articlebarcode:

inserir a descrição da imagem aqui

In the column Code I need to insert a sequential number, that is, I have to read the maximum number that is there and insert the next one that is available ! I have to use the following line of code to see what is the available value:

SELECT ISNULL(MAX(Code), 0) + 1 FROM ArticleBarCode

Can anyone help me with this situation? Thank you !

This is the code I use to enter values in this table:

conn.Open();
comm.CommandText = @"INSERT INTO ArticleBarCode(Code, Code_Article, BarCode, CreatedBy, CreatedOn, ModifiedBy, ModifiedOn, IsDeleted)
                                   VALUES (@code, @codearticle, @barcode, 1, @date, 1, @date, 0)";
comm.Parameters.AddWithValue("@code", next);
comm.Parameters.AddWithValue("@codearticle", code);
comm.Parameters.AddWithValue("@barcode", numbercode);
comm.Parameters.AddWithValue("@date", DateTime.Now);
comm.ExecuteNonQuery();
conn.Close();

That next has to be the sequential value ! Everything else works.

I have this code that can fetch the next available value and then I try to insert:

 conn.Open();
 SqlCommand cmd5 = new SqlCommand("SELECT ISNULL(MAX(Code), 0) + 1 FROM ArticleBarCode", conn);
 Int32 next = (Int32) cmd5.ExecuteScalar();
 conn.Close();

 conn.Open();
 comm.CommandText = @"INSERT INTO ArticleBarCode(Code, Code_Article, BarCode, CreatedBy, CreatedOn, ModifiedBy, ModifiedOn, IsDeleted)
                    VALUES (@code, @codearticle, @barcode, 1, @date, 1, @date, 0)";
  comm.Parameters.AddWithValue("@code", next);
  comm.Parameters.AddWithValue("@codearticle", code);
  comm.Parameters.AddWithValue("@barcode", numbercode);
  comm.Parameters.AddWithValue("@date", DateTime.Now);
  comm.ExecuteNonQuery();
  conn.Close();

But when I do this, it gives me this mistake:

The INSERT statement conflicted with the FOREIGN KEY Constraint "Fk_articlebarcode_article". The Conflict occurred in database "Cardozugestdb", table "dbo. Article", column 'Code'.

  • 2

    Already tried to mark the column as Identity?

  • @Randrade the column is Identity, she is the primary key

  • If the column is the table’s identity, then any attempt to assign an arbitrary value to it will result in error. Who controls the value that each record receives is the bank ;) In other words, you do not give the value that this column will have.

  • @Randrade just saw and I think after all the column is not Identity, I went to the column properties in the database and there in front of Identity says "false"

  • If not, you can add. However, you can change your table, add to sequence and you wouldn’t even need to change anything in your C code#

  • @Randrade It turns out I didn’t create the database, I can only work with it

  • @Randrade already managed to get the next available value but now it happens that I can not insert, I will edit the question to Voce see

  • If you are allowed to edit, you can do this. If not, it complicates a little more.

  • @Randrade therefore, I do not have these permissions

  • @Randrade already edited the code so you can see the error you give me

  • @DC my impression is that gave FK error because it is missing in the table Article the item with the same Code that you are trying to insert into ArticleBarCode, tried to insert in Article first?

  • @Filipeandradelopes That’s exactly what fixed and worked ! thank you very much

  • @You’re welcome!!! Vote for my comment...

Show 8 more comments
No answers

Browser other questions tagged

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