Error 1 The type or namespace name 'DataReader' could not be found (are you missing a directive or an assembly reference?)

0

Good afternoon, I was performing a code to query my database when I encountered these errors: Error 1 The type or namespace name 'DataReader' could not be found (are you missing a directive or an assembly reference?)

I know it's something simple, but if you could give me some help, I'd be grateful. Here is the code:

   {
        string connectionString = "Data Source=localhost; Initial Catalog=DB_SACC; User id=sa Password=1234;";
        decimal? average;
        try
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    string textt = "SELECT AVG (Total_Divida) AS 'AVG_DIVIDA' FROM t_pagamentos";
                    cmd.CommandText = textt;
                    connection.Open();
                    cmd.Connection = connection;
                    cmd.CommandType = CommandType.Text;

                    using (DataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            average = decimal.parse(reader["AVG_DIVIDA"].ToString());
                            break;

                        }
                    }
                }
            }


            TextBox3.Text = average.HasValue ? average.ToString() : "Unknown error occurred";

        }
        catch (Exception ex)
        {
            MessageBox.Show("Unable to retrieve the average, reason: " + ex.Message);


        }
    
asked by anonymous 07.04.2017 / 18:52

2 answers

1

The method ExecuteReader() returns a SqlDataReader :

using (SqlDataReader reader = cmd.ExecuteReader())
{

}
    
07.04.2017 / 19:03
0

Try replacing the type of reader for var on using and check if the value that is arriving is not null :

{
    string connectionString = "Data Source=localhost; Initial Catalog=DB_SACC; User id=sa Password=1234;";
    decimal? average;
    try
    {
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                string textt = "SELECT AVG (Total_Divida) AS 'AVG_DIVIDA' FROM t_pagamentos";
                cmd.CommandText = textt;
                connection.Open();
                cmd.Connection = connection;
                cmd.CommandType = CommandType.Text;

                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        average = Convert.IsDBNull(reader["AVG_DIVIDA"]) ? 0 : Convert.ToDecimal(reader["AVG_DIVIDA"].toString());
                        break;

                    }
                }
            }
        }

        TextBox3.Text = average.HasValue ? average.ToString() : "Unknown error occurred";
    }
    catch (Exception ex)
    {
        MessageBox.Show("Unable to retrieve the average, reason: " + ex.Message);
    }
}
    
07.04.2017 / 19:01