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);
}
Try replacing: Datareader Reader = cmd.Executereader() with: var Reader = cmd.Executereader().
– Aline
resulted , but now this giving the following error : Error 1 'decimal' does not contain a Definition for 'parse'.
– Diogo Gomes
want to say this error:Error 1 Use of unassigned local variable 'Average'
– Diogo Gomes
Just declare the variable: var Average = Convert.Todecimal(Reader["AVG_DIVIDA"]. Tostring());
– Marcell Alves
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
– Diogo Gomes