question

Aaronsoggi-6095 avatar image
0 Votes"
Aaronsoggi-6095 asked JackJJun-MSFT answered

Receiving a null DB string and a system.Argument exception

Can someone tell me why im getting a System.ArgumentNullException:


agent.cs:

  public void getStats(int agentId)
     {
         var sqlConnString = _config.GetConnectionString("DBConnectionString");
        
         try
         {
             using(var context = new IceContext(sqlConnString))
             {
                 _logger.LogInformation($"Initiating process for gathering stats for agent : {agentId}");
    
                 StatAgent dbStats = new StatAgent();
    
                 var agentStats = _Context.StatAgent.Where(x => x.AgentId == agentId).ToList();
    
                 if (agentStats == null) _logger.LogInformation(message: $"Error finding agent {agentId}");
    
                 foreach (StatAgent item in agentStats)
                 {
                     dbStats.StartDateTime = item.StartDateTime;
                     dbStats.AgentId = item.AgentId;
                     dbStats.SwitchId = item.SwitchId;  
                     dbStats.OutboundCallsMade = item.OutboundCallsMade;
                 }
    
                 _logger.LogInformation("AgentStats : {0}", dbStats);
                   
             }
                
         }
         catch (Exception e)
         {
    
              _logger.LogError(e, "Error checking QueueStats against DB");
         }


201528-image.png







My project does have an appsettings.json file with the DBConnectionString in there, but for some reason as you can see in the image its not picking it up. Does anyone why this may be?

When the project is run its hitting _logger.LogError(e, "Error checking QueueStats against DB");

dotnet-csharp
image.png (37.5 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What type of project? Show your appsettings.json excluding any sensitive information like passwords.

0 Votes 0 ·

app settings.json file:

 {
   "ConnectionStrings": [
     {
       "DBConnectionString": "Data Source=192.###.##.###,2080;Initial Catalog=;Persist Security Info=True;User ID;"
     }
   ]
 }

I have 2 console projects one which is a test project which is where the appsettings.json file is and the other is what contains the dataContext and models. I am trying to call the "getStats(int agentId)" function - which is inside the first project within "AgentStats.cs" from the test console project in the program.cs.

Below is an image of the folder structure:

201565-image.png


0 Votes 0 ·
image.png (16.4 KiB)

1 Answer

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered

@Aaronsoggi-6095, Welcome to Microsoft Q&A, based on my test, I find that it is json file content problem.

Please remove the [ ] character in your json file, like the following:

 {
   "ConnectionStrings": 
     {
       "DBConnectionString": "Data Source=192.###.##.###,2080;Initial Catalog=;Persist Security Info=True;User ID;"
     }
      
 }

Then, We could get the connectionstring from the following code:

  static void Main(string[] args)
         {
             Console.WriteLine("Starting reading file .json");
    
             Console.WriteLine($"ConnectioString: {GetDbConnection()}");
    
    
         }
         private static string GetDbConnection()
         {
             var builder = new ConfigurationBuilder()
                                 .SetBasePath(Directory.GetCurrentDirectory())
                                 .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
    
             var strConnection = builder.Build().GetConnectionString("DBConnectionString");
    
             return strConnection;
         }

Result:

201653-image.png



Best Regards,

Jack


If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


image.png (4.9 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.