Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I seldom work with arrays. So adding items to the array and reading them BIG
accomplishment.
What I am trying to accomplish is in a Class Library Project read data from
a SQLite DB into the Array list.Then pass that Array list back to another project an on a form populate the rows of a DataGridView.
NO PLEASE I do not want to bind the data to the DataGridView.
I have passed a string from a Project to a Class Library Project.
I have searched and I see no code questions related to Project to Project.
This makes me wonder it this is a bad design idea on my part?
I not asking for code but that would be nice because I am totally lost on this one.

OR a hint of what to look at.


What I have tried:

Her is the code where I read from the DB and use listAdd.
Created the list Top Level.
public static List<string> list = new List<string>();


Now Read from the DB in the Class Library Project.
private void readFriendsData()
{
    using (SQLiteConnection conn = new SQLiteConnection($"Data Source = '{"APeople.db"}';Version=3;"))
    {
        conn.Open();

        using (SQLiteCommand cmd = new SQLiteCommand($"SELECT * FROM FriendsData", conn))
        {
            using (var rdr = cmd.ExecuteReader())
            {
                while (rdr.Read())
                {
                    gv_parentInt = rdr["FID"].ToString().Trim();
                    list.Add(gv_parentInt);
                    gv_firstName = rdr["fxFirstName"].ToString().Trim();
                    list.Add(gv_firstName);
                    gv_lastName = rdr["fxLastName"].ToString().Trim();
                    list.Add(gv_lastName);

                    //dgvPerson.Rows.Add(gv_parentInt, gv_firstName, gv_lastName);
                    //dgvPerson.Sort(dgvPerson.Columns[2], ListSortDirection.Ascending);

                    dgvPerson.Rows.Add(list[i], list[i + 1], list[i+2]);
                    i = i + 3;// Crude but it works ?

                    dgvPerson.Sort(dgvPerson.Columns[2], ListSortDirection.Ascending);

                }
                rdr.Close();
            }
        }
    }
}
Posted
Updated 3-Apr-23 15:35pm

If you have a public class with a public property or method in a class library, then you can link the class library to another class library or application and access that class and property/method just like if the class was in the application itself via a reference to the location of the class.

Also, check out this video on Repository Pattern | YouTube[^]. It's a common structured method of interfacing with a database.

As for you code, based on the description above, you need to change this:
C#
private void readFriendsData()
{
    // do work here
}

To:
C#
public void ReadFriendsData()
{
    // do work here
}

This is Access Modifiers 101 | Microsoft Learn[^].

Lastly, watch your naming conversion for your naming. You can read more here: Snake Case VS Camel Case VS Pascal Case VS Kebab Case – What's the Difference Between Casings?[^]
 
Share this answer
 
v3
Comments
Choroid 29-Mar-23 11:59am    
Graeme_Grant Thanks for the time you spent on this answer it is HUGE
I need to expand my knowledge on Classes & Interfaces as well working with obj relation mapping
Need to fix VS every time I create something it makes it Private ?
I try to use camelCase but bad habits when creating test projects I just use odd naming
Thanks
I am posting my answer because all that offered solutions I did not give up!
I made this call to my Class Library Form and got a obj list back.
And populated the rows of the DataGridView.
public void loadDGV()
{
    frmHDB form = new frmHDB();
    {
        form.readData(list);
        foreach (string str in list)
        {
            dgvPerson.Rows.Add(list[i], list[i + 1], list[i + 2]);
            i = i + 3;
            dgvPerson.Sort(dgvPerson.Columns[2], ListSortDirection.Ascending);
        }
    }
}

Here is the code in the Class Library that gets data from the SQLite Database
public List<object> readData(List<object> list)
{
    using (SQLiteConnection conn = new SQLiteConnection($"Data Source = '{dbName}';Version=3;"))
    {
        conn.Open();
        using (SQLiteCommand cmd = new SQLiteCommand($"SELECT * FROM FriendsData", conn))
        {
            using (var rdr = cmd.ExecuteReader())
            {
                while (rdr.Read())
                {
                    gv_parentInt = rdr["FID"].ToString().Trim();
                    list.Add(gv_parentInt);
                    gv_firstName = rdr["fxFirstName"].ToString().Trim();
                    list.Add(gv_firstName);
                    gv_lastName = rdr["fxLastName"].ToString().Trim();
                    list.Add(gv_lastName);
                }
            }
        }

    }
    return list;
}

I am not too sure about the i = i + 3 counter but it works
 
Share this answer
 
Comments
Richard Deeming 5-Apr-23 4:38am    
foreach (string str in list)
{
    dgvPerson.Rows.Add(list[i], list[i + 1], list[i + 2]);
    i = i + 3;

That's really bad.

For simplicity, assume your function returns two rows - list.Count = 6.

Iteration 1: str is the FID for record 1, i = 0 - You add a row for the FID, first name, and last name of record 1.
Iteration 2: str is the first name for record 1, i = 3 - You add a row for the FID, first name, and last name of record 2.
Iteration 2: str is the last name for record 1, i = 6 - You get an IndexOutOfRangeException, because i is now past the end of the list.

Use a for loop instead. You'll also want to move the sort operation outside of the loop, so you don't keep sorting the grid over and over again as you add the rows.
for (int index = 0; index < list.Count - 2; index += 3)
{
    dgvPerson.Rows.Add(list[i], list[i + 1], list[i + 2]);
}

dgvPerson.Sort(dgvPerson.Columns[2], ListSortDirection.Ascending);
Choroid 5-Apr-23 11:53am    
Richard Thanks I was so happy when it worked I posted my code in haste
I have since gone back and cleaned up a few things and decided to use list.Count as you suggested.
Also noticed I was making a call to the Class Library three times when I only need one call
Really Appreciate the comment

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900