Print total of query count in a label

0

How about, I'm trying to print the total of a query count in a label , but when I call the result only prints a System.Data.DataRow

This is my query

/***********GET ALL THE FAKE TOOLS FROM FCH*************/
public class area_success_FCH
{
    public DataTable Sfchconnect()
    {
        string myConnectionString = @"C:\Users\gutiece\Desktop\database\" + "Database1.accdb";

        DataTable SfchTable = new DataTable();

        OleDbConnection connection = new OleDbConnection();
        OleDbCommand command = new OleDbCommand();
        OleDbDataAdapter adapter = new OleDbDataAdapter();
        DataSet dataset = new DataSet();

        try
        {
            connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data source= " + myConnectionString;
            bool ok = System.IO.File.Exists(myConnectionString);
            String qry = "SELECT COUNT(*) FROM area WHERE standby = 1 AND area = 'FCH'";

            command.Connection = connection;
            command.CommandText = qry;

            adapter.SelectCommand = command;

            command.Connection.Open();
            OleDbDataReader reader = command.ExecuteReader(); // close conn after 
            SfchTable.Load(reader);
            if (!reader.IsClosed)
            {
                reader.Close();
            }

            return SfchTable;
        }
        catch (Exception)
        {
        }
        return SfchTable;
    }
}
/*******************************************************/

the query is in a folder called conexiones and it has a class where it has the query

then the DataTable I send it to call from the dashboard.aspx that is a common page in the root file

/********************************FILL COUNT CLOUD******************************/
    conexiones.area_success_FCH Sfchconnect = new conexiones.area_success_FCH();
    conexiones.area_success_FCH SfchTable = new conexiones.area_success_FCH();
    /******************************************************************************/

if (!IsPostBack)
        {

            Lbl_notu.Text = notu.Rows[0].ToString();

result

    
asked by Cesar Gutierrez Davalos 09.06.2017 в 21:28
source

2 answers

1
Lbl_notu.Text = notu.Rows[0][0].ToString();

So that you can access the element of [Reglón] [Column], I hope it helps you, I have a function to return only 1 query element in case you occupy it. Greetings

    
answered by 09.06.2017 / 22:32
source
1

If you are going to use aggregation functions (which return a single value), you should not return a DataTable , but a value, in this case to be a COUNT , an integer.

Then, declare a int count and return there the result of ExecuteScalar , which returns the first row of the first column of the result of your query ( COUNT )

try
{
    connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data source= " + myConnectionString;
    bool ok = System.IO.File.Exists(myConnectionString);
    String qry = "SELECT COUNT(*) FROM area WHERE standby = 1 AND area = 'FCH'";

    command.Connection = connection;
    command.CommandText = qry;
    connection.Open();
    count = command.ExecuteScalar();
    connection.Close();
}
catch (Exception)
{
}
return count;
    
answered by 09.06.2017 в 22:38