-4

I created a Windows Forms solution. I also created a (db.dll) Class Library Project in the Solution what is a database manager Class. I mean inside there add/edit/delete and the details of the connection. I have another Project in the same Solution for my other Classes and Forms. Like a Form where list the whole data from the database. Like where you can add more data into the database.

I can't access any data from my database manager Class across the other Classes what is managing the data on the Forms.

My db.dll can't access any kind of Classes, or Methods in my other Forms. Just one example: My db.dll can't see any DataGridView.

MySqlConnection con = new MySqlConnection("datasource= localhost; database=sampledb;port=3306; username = root; password= ");
con.Open();
DataGridView1.DataSource = null;
MySqlDataAdapter adapter = new MySqlDataAdapter("select * from information", con);
DataTable dt = new DataTable();
adapter.Fill(dt);
DataGridView1.DataSource = dt;
con.Close();

The name 'DataGridView1' does not exist in current context.

How can I access from one Class (in .dll with same Solution) to other Class? I also very interesting how to can communicate two different .dll file in the same Solution?

The db.dll added as Reference and also added as using db.dll;.

Marcell Nemeth
  • 97
  • 3
  • 4
  • 13

1 Answers1

1

You just need to add a reference from one project to another one and add a using to this project in the file you wish to use the class.

If you are using Visual Studio, you can just right-click the project References and add the reference to the other project.

enter image description here

UPDATE:

It seems that you are doing a wrong reference between the projects.

In order to let your code recognize DataGridView1 class, you need to add a reference from the db project to the project where the DataGridView1 is located and add a using to the DataGridView1 namespace.

It might be that you won't be able to add this reference because of a circular projects reference because the other project probably already references db.dll so that you can't add a reference from the db.dll to the other dll.

The solution for this would be to map your DataGridView1 model to an entity class that the DB project knows. This entity class can be located in the project of the db.dll (or another project that db.dll has a reference to it).

There are many advantages working with entities and models. You can read more about it here.

Misha Zaslavsky
  • 8,414
  • 11
  • 70
  • 116
  • I already add as Reference and added as "using" as well. Any Forms and Classes are can seen my .dll and can use any Class from there. But the Classes inside .dll can't seen any others Classes, Methods or what else. That's whats I try to find out why? – Marcell Nemeth Sep 07 '21 at 15:58
  • @MarcellNemeth It is not clear to me what you mean. Could you please update your question and give some more information. What are the projects? What are the classes? Where each class is? When you specify enough information you will get a helpful answer. – Misha Zaslavsky Sep 07 '21 at 16:05