0

I am following this example to understand how to use a DataGridView component can be used to display and manipulate a database table.

I have configured the database access through a Data Connection using Tools->Connect to Database menu in Visual Studio 2017.

Data Connections

Now I am trying to get the Sqlconnection using the name of that connection configured through the Visual Studio, but I do not know if I need to hardcode the connection string or it shall be retrieved from the configuration.

I have tried this:

Dim DBConnection As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("localhost(wildfire)").ConnectionString)

and this:

Dim DBConnection As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("wildfire").ConnectionString)

In both cases I get:

System.NullReferenceException: 'Object reference not set to an instance of an object.'
System.Configuration.ConnectionStringSettingsCollection.this[string].get returned Nothing.

What shall I do to get the connection string? Do I need to hardcode it in the source code?

Note: I tag the question also in C# in case the answer is similar. I can convert the syntax from C# to VB if the way to retrieve the connection string is similar.

M.E.
  • 4,955
  • 4
  • 49
  • 128

1 Answers1

0

When you select the Data Connection in the server explorer you can obtain the connection string by pressing the F4 button to view the properties. You'll see a Connection String property. The next step is copy that string and set it into your app.config. If you're unsure how to do that read the following answers to this question...

What is App.config in C#.NET? How to use it?

Mick
  • 6,527
  • 4
  • 52
  • 67