c # and access get the largest value in a column

0

good day I'm trying to get the last record entered in a table, there are already several clients entered however in the execution when I try to give the value read to a variable shows me an error "There is no data available for the row or column "

        try
        {
            OleDbConnection probar = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source= C:\Users\user1\Desktop\base_datos_red.accdb; Persist Security Info=false");
            probar.Open();
            OleDbCommand c = new OleDbCommand("select max(clave) from clientes", probar);
            OleDbDataReader a = c.ExecuteReader();
            MessageBox.Show(a.GetString(0));
            probar.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(""+ex);
        } 
    
asked by jose marquez 14.11.2016 в 19:27
source

2 answers

1

What you should do to get 1 or more values from a query is to go through the reader of your query. In terms of improving the code a little the using you can define them

string connString= "Provider=Microsoft.ACE.OLEDB.12.0; Data Source= C:\Users\user1\Desktop\base_datos_red.accdb; Persist Security Info=false";

string consulta = "select max(clave) from clientes";

using (OleDbConnection probar = new OleDbConnection(connString))
using (OleDbCommand c = new OleDbCommand(consulta, probar))
{
    try
    {
        probar.Open();
        OleDbCommand c = new OleDbCommand("select max(clave) from clientes", probar);
        OleDbDataReader a = c.ExecuteReader();
        while(a.Read()){
           //Leer el DataReader y obtener el/los campos del registro
           MessageBox.Show(a[0].ToString());
        }
        a.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
    
answered by 14.11.2016 / 19:34
source
1

There are some basic problems:

  • After running c.ExecuteReader() , you must run a.Read() once before using a.GetString() to be able to read the result.
  • The parameter you use for a.GetString(-1) is incorrect. It should be 0 : a.GetString(0)

Additional suggestions:

  • Since your query guarantees that it will return a single row and a single value, use a.ExecuteScalar() rather.
  • ""+ex is a little weird. ex.ToString() seems better.
  • Do not forget to close the OleDbDataReader
  • To avoid having to close objects in case of exceptions, get used to using blocks using .

Suggested code:

try
{
    string resultado = null;
    using (OleDbConnection probar = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source= C:\Users\user1\Desktop\base_datos_red.accdb; Persist Security Info=false"))
    {
        probar.Open();
        using (OleDbCommand c = new OleDbCommand("select max(clave) from clientes", probar))
        {
            resultado = (string)c.ExecuteScalar();
        }
    }

    // Mostrar el resultado después de cerrar la conexión
    // es mejor para evitar guardar la conexión abierta innecesariamente
    MessageBox.Show(resultado);
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
} 
    
answered by 14.11.2016 в 19:34