question

Anja-7727 avatar image
0 Votes"
Anja-7727 asked DuaneArnold-0443 commented

Still having big problems with Object reference not set to an instance of an object in my solution but have nailed it down.

Hi
I get the above error and when removing all try/catch stack trace sais the problem is in the method Conn in my Helper Class in the DAL project.

 using System;
 using System.Configuration;
    
 namespace DAL
 {
     public class Helper
     {
         private static readonly string ConnectionStringName = "AnsiBugDb";        
    
         public static string Conn()
         {
             return ConfigurationManager.ConnectionStrings[ConnectionStringName].ConnectionString;
         }
     }
 }


Can it be, that it is because the name AnsiBugDb is set in the UI project (in App.config) if so, what have I to do to set the db connection in the DAL project instead?

I have tried to copy/paste the text in the ConnectionStringName from the App.Config in the AnsiBug project with no success. I have tried to make a new App.config in the DAL project again without success.

So I'm totally lost in what to do.

I have also tried to set a .ToString() in the return

 public static string Conn()
         {
             return ConfigurationManager.ConnectionStrings[ConnectionStringName].ConnectionString.ToString();
         }



I'm using the string in the DalCategory.cs class

 using System;
 using System.Collections.Generic;
 using System.Text;
 using System.Data;
 using Model.Account;
 using System.ComponentModel;
 using Dapper;
 using System.Linq;
 using System.Collections.ObjectModel;
 using System.Configuration;
    
 namespace DAL.Account
 {
     public class DalCategory
     {
         #region Get
         public List<Category> GetCategories()
         {
             string connectionString = Helper.Conn();
             using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(connectionString))
             {
                 var list = connection.Query<Category>("dbo.Category_GetAll").ToList();
                 return list;
             }          
         }
         #endregion
    
         #region Insert
         #endregion
    
         #region Update
         #endregion
    
         #region Delete
         #endregion
     }
 }

I hope someone of you can help me.

Best regards
SimsenVejle :-)

dotnet-csharpwindows-wpf
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.

DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered Anja-7727 commented

I'm pretty confused right now, so I've decided to stick with the app.settings approach. It's too much for me to run back and forth between json and app.settings

Your pretty confused, becuase you went off track trying to use .NET Core way of doing things concerning using configuration information that the program needs for a .NET Framework solution you are trying to apply it to.

You should have just used ConfigurationManager and gotten the connectionstring from the app.config that is used in the root project.

https://www.codeproject.com/Tips/416198/How-to-get-Connection-String-from-App-Config-in-Cs

That's it. What's in the link is that simple that can be used in any layer to get the connectionstring.

How you got to this Helper.Con() is a mystery using the ConfigurationBuilder class. How you got to an appsettings.json is questionable in a Windows desktop solution that is not a .NET Core solution. Maybe it's becuase you're trying to use Dapper that sent you down the path that you are on that is questionable IMHO.

If you look at the .NET Core Windows form app in the Startup.cs, it is using the ConfigurationBuilder class that is going to get the connectionstring information out of the appsettings.json with the connectionsting being sent to the DAL.Models. PublichingComnayContext.cs for using Entity Farmework that is using dependency injection to get the connectionstring information there.

https://github.com/darnold924/PubComanyWinCore

My take on this is that you should just simply use the ConfigurationManager that would have gotten the connectionstring out the app.config in the root project so that it could be used by any class in the project or a class in a layer like the DAL.

BTW, this not my first rodeo in using layered or n-tier in Windows desktop such as Windows form, WPF or ASP.NET Web solutions using .NET Core or .NET Framework. :)

https://docs.microsoft.com/en-us/previous-versions/msp-n-p/ee658117(v=pandp.10)

I discovered that if you want to see the link and not get q 404 not found, then you have to copy it and come completely out of Q&A and paste it into the browser's address line. It will display the info in the link.


· 3
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.

Hi again

I now using the ConfigurationManager to get the connectionstring but still got the error80460-stacktrace3.jpg



My App.Config connectionstring:

 <connectionStrings>
     <add name="AnsiBugDb" connectionString="Server=DESKTOP-HHUAD9Q;Database=AnsiBug;Trusted_Connection=True;" providerName="System.Data.SqlClient"/>
   </connectionStrings>


0 Votes 0 ·
stacktrace3.jpg (468.0 KiB)

And my GetCategories() method

 public List<Category> GetCategories()
         {
             string cnn = ConfigurationManager.ConnectionStrings["AnsiBugDb"].ConnectionString;
             using (IDbConnection connection = new SqlConnection(cnn))
             {
                 if (connection.State == ConnectionState.Closed)
                 {
                     connection.Open();
                 }
    
                 return connection.Query<Category>("dbo.Category_GetAll").ToList();
             }
         }

0 Votes 0 ·

I have just seen, that you use GitHub. So I have just make a profile and added my project -> https://github.com/SimsenVejle/AnsiBug

There's one thing that makes me wonder that is that you use the word Core around my project.

I do not know that I use Core. I chose (when I started up) to use wpf as UI and classes to build my mvvm model. Is there anything I do not get in terms of core application?

Best regards
Simsen :-)

0 Votes 0 ·
DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered DuaneArnold-0443 commented

I assumed you were using .NET Core, because of the usage of appsettings.json file. The appsettings.json file is predominately used in .NET Core solutions. You are not using .NET Core you are using .NET Framework. They are two different types of .NET solution usages.

Now, l don't think you have a connectionstring problem. The error you are getting is related to something else. You need to set a debugger
breakpoint on a line where the exception was thrown, start single stepping until it blows up and then you use debugger Quickwarch and find out what object is null valued object meaning that the object is not in memory it doesn't exist.









,re




· 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.

I have to be sure now.

Using F11 when debugging is stepping into each single thing? If yes. I don't get any error at all I read the call stack nothing at all only show the things I am stepping into.

The error is just on the error list when focus on the MainWindow.xaml. I will now supress the error disabling the project code.

Thank you for your huge help. I'm so grateful and I've learned a lot from you the last few days.

Best regards
Simsen :-)

0 Votes 0 ·

I cannot say not to use WPF and MVVM. But to me, they both are a PITA to use together IMHO. The usage of MVVM is a Seperation of Duty.

The same can be achieved by using MVP and Windows forms solution, which is much simpler to use. It's something you may want to consider in future projects.

I am using MVP with the .NET Core Windows form solution presented on Github to you.

https://www.codeproject.com/Articles/228214/Understanding-Basics-of-UI-Design-Pattern-MVC-MVP

http://polymorphicpodcast.com/shows/mv-patterns/

0 Votes 0 ·