I have a table with the name Produtos
where I inserted 3 products for testing. Now I'm setting up 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 it is not 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());
}