Click here to Skip to main content
15,665,942 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to put all my CRUD function on one form.
The form works to create the DB and a Table no issues.
So I decided to move the readFriendsData function to the same form.
This form is named frmMakeDB.Then make the CALL from frmSelect which
populates a DataGridView which is not bound to the DB.
I know the DB has one record & the readFriendsData code works if it is
on the frmSelect.
With my limited testing I do not believe any data is being returned ?
I am using C# 7.3 and NetFrameWork 4.8 VS 2019 I have looked at this a lot.
How to call a method from another form[^]
I am NO Pro but The Tuple code look like spaghetti or an after thought !

What I have tried:

Form Select where the method is called and used when returned
private void frmSelect_Load(object sender, EventArgs e)
    //using (frmMakeDB frm = new frmMakeDB())

    dgvPerson.Rows.Add(frmMakeDB.gv_parentInt, frmMakeDB.gv_firstName, frmMakeDB.gv_lastName);

This is frmMakeDB where the variables are declared as public static strings
    public static string readFriendsData()//string gv_parentInt,string gv_firstName,string gv_lastName
        using (SQLiteConnection conn = new SQLiteConnection($"Data Source = '{"Contacts.db"}';Version=3;"))
            using (SQLiteCommand cmd = new SQLiteCommand($"SELECT * FROM FriendsData", conn))
                using (var rdr = cmd.ExecuteReader())
                    while (rdr.Read())
                        gv_parentInt = rdr["FID"].ToString().Trim();
                        gv_firstName = rdr["fxFirstName"].ToString().Trim();
                        gv_lastName = rdr["fxLastName"].ToString().Trim();
                        //dgvPerson.Rows.Add(gv_parentInt, gv_firstName, gv_lastName);


        return Tuple<string, string, string>(gv_parentInt, gv_firstName, gv_lastName);
        //return (gv_parentInt, gv_firstName,gv_lastName);

public static T3 Tuple<T1, T2, T3>(T3 gv_parentInt, T3 gv_firstName, T3 gv_lastName)
    throw new NotImplementedException();
Updated 6-Mar-23 10:11am
PIEBALDconsult 5-Mar-23 20:19pm    
Write yourself a proper backend/DAL; database code has no place in frontend/UI/Form code.

Why are you putting the CRUD functions in a form at all? All you're doing is making it more difficult to use those functions from other parts of your code.

Put the CRUD functions in their own project and you can use the functionality in multiple applications and application types.
Share this answer
Choroid 6-Mar-23 14:01pm    
Dave Kreskowiak Still learning C# Sounds like a great design.
Learning to call Methods or Functions in another form is the first step.
Perhaps I can tackle using Methods or Functions in a project can be the second step.
Have a nice day thanks for the suggestion.
Dave Kreskowiak 6-Mar-23 14:09pm    
The problem with you putting the database code in a form is if you need to use that code from a second form, you have to create another instance of the first form with the database code in it.

That's not exactly the right way to do things. Try to avoid learning bad habits because they will be harder to break later on.
Choroid 6-Mar-23 14:39pm    
Dave Kreskowiak I have managed to implement the Update and Save functions
The killer is the Read which goes the other direction from Update and Save
with regards to the form The Create should be easy.
Because I use SQLite and AddWithValue still thinking about how that works with CRUD in a project
Need to find some code and have a look. It took me a while to realize some code is very cookie cutter. So I learned one way and have not changed my process with DB work.
Time for new adventures and expanded thinking
My Solution now has raised more question.
First the issue was not being able to return multiple static strings.
So I did some reading about returning multiple values and found Tuple suggestions.
It did not work and I should have trusted my first thought about the code design.
I am NO Pro but The Tuple code looks like spaghetti or an after thought !

So during testing to return one static string I tried return gv_firstName; ONLY.
It worked all values were sent to the frmSelect.
I also tested making the public static bool readFriendsData() and returned true
all values were sent to frmSelect.
Why not using Parameters worked?
Has anyone used the Tuple design?
Share this answer

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