3
I have a table with the name Produtos
in which I inserted 3 products for testing. Now I’m assembling the product registration form and I need to always generate a sequential product code from the last id already registered + 1.
The code I made is this, I followed my logic but n is working.
private void GerarNovoCodigo()
{
string strCon = "Data Source=MEUPC; Initial Catalog=MEUDB; Integrated Security=SSPI";
SqlConnection sqlCon = new SqlConnection(strCon);
SqlCommand sqlCom = new SqlCommand();
sqlCom.Connection = sqlCon;
sqlCon.Open();
sqlCom.CommandText = "Select max(prod_codigo) + 1 from Produtos";
txt_prod_cod.Text = Convert.ToString(sqlCom.ExecuteNonQuery());
}
Except for the fact that this is a danger in a concurrent environment and that probably there should be a lot of code repeated in the application, for described I could not see problem. Need to add more details.
– Maniero
Welcome to [en.so]. Please read the guide [Ask] and do a [tour], to learn a little more about the operation of the site to increase your chances of getting a good answer.
– Randrade
Apparently, the code performs correctly, which is the problem faced?
– Julio Borges
@bigown How would Voce always generate a new code in a simple way? It is a basic application for now, of registration, consultation and movement of various products. There is repeated code in the application, but for now I want to make the basics to dry later, since I am in the learning phase still. Thank you for the reply.
– Leonardo Coelho
@Julioborges The textbox is not receiving the new code. When running the application it loads blank.
– Leonardo Coelho
@Leonardocoelho appears in white or always appears 1?
– Maniero