Error 1 The type or namespace name 'Datareader' could not be found (are you Missing a using Directive or an Assembly Reference?)

Asked

Viewed 355 times

0

Good afternoon, I was performing a code to query my database when I appeared these error: Error 1 The type or namespace name 'Datareader' could not be found (are you Missing a using Directive or an Assembly Reference?)

I know it’s simple, but if you could give me a hand, I’d really appreciate it. 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);


        }
  • 1

    Try replacing: Datareader Reader = cmd.Executereader() with: var Reader = cmd.Executereader().

  • resulted , but now this giving the following error : Error 1 'decimal' does not contain a Definition for 'parse'.

  • want to say this error:Error 1 Use of unassigned local variable 'Average'

  • Just declare the variable: var Average = Convert.Todecimal(Reader["AVG_DIVIDA"]. Tostring());

  • thanks to all this was the mistake that came after having followed the advice of Mr Marcell : Error 1 A local variable named 'Average' cannot be declared in this Scope because it would Give a Different meaning to 'Average', which is already used in a 'Parent or Current' Scope to denote Something Else

2 answers

1

The method ExecuteReader() returns a SqlDataReader:

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

}

0

Try replacing the Reader type to var in the using and check if the value that is coming 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);
    }
}
  • I have tried it and it appears the following error :Error 1 Use of unassigned local variable 'Average '

  • decimal? Average; removes nullable. Put: decimal Average = 0;

  • Just declare the variable: var Average = Convert.Isdbnull(Reader["AVG_DIVIDA"]) ? 0 : Convert.Todecimal(Reader["AVG_DIVIDA"]. toString());

  • thanks to all this was the mistake that came after having followed the advice of Mr Marcell : Error 1 A local variable named 'Average' cannot be declared in this Scope because it would Give a Different meaning to 'Average', which is already used in a 'Parent or Current' Scope to denote Something Else

  • @Diogogomes Just read the text that will understand what the problem is.

  • I realized which is the mistake I’m not understanding is how I should solve it. thanks for your attention!

  • @Diogogomes tested to remove nullable?

  • ok is already giving , although it says that failed to login in user sa and password

Show 3 more comments

Browser other questions tagged

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