Search first empty space in database

Asked

Viewed 52 times

0

I have a small application in C#, which creates the automatic supplier code. I wonder if anyone knows any way of the system to search for an empty code, ex:

1 - Manoel

2 - Daffodil

4 - Mustela

The next code he should pull would be 3, most it always goes to the last record.
Follow the code below:

private void btnGravar_Click(object sender, EventArgs e)
{
    string Id = txtID.Text;
    string Nome = txtNome.Text;
    string CNPJ = txtCNPJ.Text;
    if (txtNome.Text == "" || txtCNPJ.Text=="" )
    {
        MessageBox.Show("Favor informar os dados para cadastro!", "Alerta",
        MessageBoxButtons.OK,
        MessageBoxIcon.Warning);
        return;
    }
 
    try
    {
        CONEXAO.Open();
        comando = new MySqlCommand("INSERT INTO fornecedores (ID, Nome, CNPJ)" + "VALUES ('"
            + Id + "','" + Nome + "','" + CNPJ + "')", CONEXAO);
        comando.Parameters.AddWithValue(Id, txtID.Text);
        comando.Parameters.AddWithValue(Nome, txtNome.Text);
        comando.Parameters.AddWithValue(CNPJ, txtCNPJ.Text);
        comando.ExecuteNonQuery();
        // Registra os fornecedores no Data Grid.
        registraFornecedor(txtID.Text, txtNome.Text, Convert.ToString(txtCNPJ.Text));
        ClearControls();
    }
    catch
    {
        MessageBox.Show("A conexão com o banco de dados falhou");
    }
    finally
    {
        CONEXAO.Close();
    }
}
  • 2

    It doesn’t answer what you want, but overall it’s bad, and even if you want to insist you need to take a lot of care that goes beyond what you’re posting in this question. My fear is someone responding in a naive way that seems right, but it only works by coincidence, unless they have no competition, which is highly unlikely. Generally to do this right needs a sophisticated system of own control.

  • 1

    I agree with Maniero, the answer to what you want is complex, but trying to help I must tell you that you should rethink the logic, avoid designating a code before the user confirms the data and before you do the consistencies, or you make a field auto-increment and then redeem the value or create a Generator function to be used on all screens you will insert in this table. Maintaining sequential integrity of a field that is not auto-increment is no simple task.

  • just complementing the other comments: and totally unnecessary to do something like this

  • Rovann by the example code he is using Mysql, a auto-increment field would solve the problem, but let’s assume that he was using DBF he would not have this facility, formerly these fields were controlled in the nail, it was a bad job. I still have clients who question me a request number that lost the sequence for some reason, I convince him that the sequence should not be stuck, since we have reports and the "sequential number" of the request is the least important, unless it is NF number then changes the thing.

  • I’m using auto-increment, but I’ve seen this function that I asked for help, in Delphi, now in c# I’m not getting.

  • Reinforce the observations of Maniero, take a look at this other question, maybe I can help you in some way: https://answall.com/questions/324399/busca-intervalo-de-registros-missing

Show 1 more comment
No answers

Browser other questions tagged

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