Creating A project connection

Harcon

Member
Joined
May 8, 2012
Messages
18
Programming Experience
1-3
I am starting to build a forms application. And i want to open a database connection at Startup of the application.
Now is my question hou can i set up a connection that runs thrue out the whole program.

rather then opening a connection in each form.
 
That answer is that you don't. You can, but you absolutely should not do it that way. Opening a connection in each form is the correct way to use ADO.NET. In fact, opening several different connections at different times within the same form is the right way to go unless you have a specific reason to do otherwise. ADO.NET was designed specifically to be used that way. Here are some code examples of the appropriate way to manage ADO.NET objects:

Retrieving and Saving Data in Databases
 
Thank you!!

When i am correct you start each form with the opening of the database connection and then retrieve the informatie form the database!
 
You open the connection when you need to use it and not before. You close the connection immediately after using it. If you need to execute multiple queries one after the other then it's OK to leave the connection open for that but not longer. For instance, if you need to retrieve when the form opens and save data when the form closes then you would open a connection at the start, retrieve the data and then close the connection, then open a connection again at the end, save the data and then close the connection again. You might even use two different connection objects, depending on the circumstances. That is demonstrated in the link I provided earlier.
 
Thank you... That is working fine.

But i have a question, i loaded the data from the database in a adapter en loaded that in to a datagridview.
Because my database gives an empty row at the bottom that goes in to the adapter en show in the datagridview.

How can i avoid showing the NULL row in the grid
 
Are you sure that that row is coming from the database? By default, the DataGridView displays an empty data entry row at the bottom, which is where the user types new data to add a new row. Are you sure that's not what you're seeing? Do you want users to be able to add new rows by typing directly into the grid? If not then you need to set the AllowUsersToAddRows (or something like that) property of the grid to False.
 
Back
Top