Click here to Skip to main content
15,170,331 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello ,

I am working on a windows form using C# to get data from an application where its data is saved in an SQL tables and display the results in a DataGridView. 

The Database of this application is installed sometimes locally and sometimes on an external server( depends on the customer requirements) and in all cases it creates a connection string in the registry.

So adding a connection string in the app.config file is not a best practice as we don't know when this DB is locally or external. and since the connection string is created in the registry , what is the best practice to get it from the registry and fetch the data and display them to the GridView.
We can use : string text = registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\myappname", "CONNECTIONSTRING"). and we create a query to the Db " Select *from tablex valueabc...
but how to display the values from the query to a datagridview
Thank you in advance
Jim .

What I have tried:

I did not know what code to use
Updated 14-Nov-21 14:57pm

Don't use the registry: that is decidedly not the right way to do it.
It used to be recommended, but the registry rapidly became bloated, and it gave a lot of problems. So access to the registry has deliberately been made harder, and is very likely to become even harder in future releases to discourage such use.

App.Config is exactly the right place: particularly if there is a chance it may need to be modified, or in a "application data" folder as described here: Where Should I Store My Data?[^]
Personally, I use this these days: Instance Storage - A Simple Way to Share Configuration Data among Applications[^]
JimmyTannoury 15-Nov-21 12:52pm
Hello ,

Thank you for the both answers , however it seems my question was not clear regarding the connection string . My windows form i am creating is only to access the data from this ApplicationX SQL table and display them in the DataGrid.

I am not doing anything on this application X , it is a 3rd party application and its installation comes out of the box with a connection string in the Registry , so I cannot control where to save the connection String and i don't mind where they save it as it is their application. My concern is how to read the data from SQL DB using this connection string( saved in registry) and display them on the DataGrid.
In the normal scenario where the Connection String is stored in the app.config file , i know how to fetch the data and display them, but in my case now i need to connect to the database where its connection string is in Registry ( user name and password already saved in the registry as well under Key called connectionString)
so i can get this key maybe using : string text = registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\myappname", "CONNECTIONSTRING") , and query the DB using Select statement , but from here how to continue to display the Data.
Hope this explanation is clear.

You seem to have multiple questions:

1) How/where to store the connection string
2) How to display query results in a grid

1) wherever you store the connection string, if it contains credentials, make sure you encrypt it...especially if it's not your database! Additionally, use a db login with the least required privileges. If you decide to use the registry, consider using HKCU instead of HKLM to avoid permissions issues.

2) get your query results into a datatable and set that as the datasource to the grid. You can set the columns up at design time and decide how you want to present the results. This is all elementary stuff and there are plenty of articles if you just google datagridview datasource.

BTW, regarding #1, we use a hybrid approach where the database connection is stored (encrypted) in a HKCU key so that it can be used by any of our apps/modules that a customer may have installed. Normally, our apps reside on a network share and are used by multiple end users. On the initial user setup, a cfg file containing the encrypted connection string is created. When a new user is added, the main application will note the lack of a reg key and looks for the cfg file...if it finds one it populates the HKCU key for that user and moves on. Additionally, if the reg key is found AND a cfg file is present, AND there is a difference, the cfg contents are written to the registry. This allows us to easily migrate a workgroup to a new server which happens often enough that this approach became necessary.

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