InvalidOperationException: Connection property has not been initialized in Asp.Net Core

Dung Do Tien Sep 11 2021 880

Hello Guys, I have a small project with Asp.Net Core and Postgree database. I have a function to help get all data as below

 public List<Entities.Websatellite> GetAll()
    string storeName = "func_ad_websatellite_getall";
        using (var db = new PostgresSQL())
            var lst = new List<Entities.Websatellite>();

            using (var command = db.CreateCommand(storeName, true))
                using (var reader = command.ExecuteReader())
                    if (reader.HasRows)
                        while (reader.Read())
                            var obj = new Entities.Websatellite();
                            EntityBase.SetObjectValue(reader, ref obj);
            return lst;
    catch (Exception ex)
        throw new Exception(string.Format("{0} => {1}", storeName, ex.ToString()));

 But when I run code I get an exception Connection property has not been initialized.

  Message=func_ad_websatellite_getall => System.InvalidOperationException: Connection property has not been initialized.
   at Npgsql.NpgsqlCommand.CheckReadyAndGetConnector()
   at Npgsql.NpgsqlCommand.<ExecuteDbDataReader>d__100.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at Npgsql.NpgsqlCommand.ExecuteReader()
   at ABC.WIS.DAL.Websatellite.WebsatelliteDal.GetAll() in D:\Mex\admin\automexico\Backend\Websatellite\WebsatelliteDal.cs:line 24
   at ABC.WIS.DAL.Websatellite.WebsatelliteDal.GetAll() in D:\Mex\admin\automexico\Backend\Websatellite\WebsatelliteDal.cs:line 42
   at ABC.MexicoAuto.BSL.AutoCrawlReviewBLL..ctor(IBrandBLL brandBLL, IModelBLL modelBLL, IVersionBLL versionBLL, IRegionsBLL regionBLL, ICitiesBLL citiesBLL, IProductDal productDal, IWebsatelliteDal websatelliteDal, IProductBo productBo) in D:\Mex\admin\automexico\Common\AutoCrawlReviewBLL.cs:line 63
   at Ad_AutoCrawlReview.Process..ctor() in D:\Mex\admin\automexico\Ad_AutoCrawlReview\Process.cs:line 58
   at Ad_AutoCrawlReview.Program.Main() in D:\Mex\admin\automexico\Ad_AutoCrawlReview\Program.cs:line 23

I check the connection, it is not empty. it is below;Port=3423;User Id=user_web;Password=32444334;Database=food_eat;CommandTimeout=320

I use core version 3.1. Thanks for any suggestions.

Have 1 answer(s) found.
  • J

    Jide Sep 11 2021

    Connection property has not been initialized throw when you can't open connection. have some reason below:

    1. Wrong syntax of the connection string
    2. Information is not correct such as server name, user or pass are not correct.
    3. The connection string is right but you do not have permission to access it.

    But see your connection string I think you are the wrong syntax. change :;Port=3423;User Id=user_web;Password=32444334;Database=food_eat;CommandTimeout=320


     Server=;Port=3423;User Id=user_web;Password=32444334;Database=food_eat;CommandTimeout=320

    I hope it work well for you.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close