1
I have this table called Articlebarcode:
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'.
Already tried to mark the column as Identity?
– Randrade
@Randrade the column is Identity, she is the primary key
– D C
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.
– Oralista de Sistemas
@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"
– D C
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
@Randrade It turns out I didn’t create the database, I can only work with it
– D C
@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
– D C
If you are allowed to edit, you can do this. If not, it complicates a little more.
– Randrade
@Randrade therefore, I do not have these permissions
– D C
@Randrade already edited the code so you can see the error you give me
– D C
@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 intoArticleBarCode
, tried to insert in Article first?– Mr. Mister
@Filipeandradelopes That’s exactly what fixed and worked ! thank you very much
– D C
@You’re welcome!!! Vote for my comment...
– Mr. Mister