Answered by:
Using a connection string with sqlconnection object

Question
-
I am developing a windows forms application. Something I haven't done in many years. I have mainly been using ASP.NET to build websites.
I have added a connection sring to my windows forms project. If I go to My Project->Settings, I can see it there no problem.
However now I need to reference it in my code so I can loop through records and manipulate data.
This is how I would normally do this in a web application (where the connection string is defined in a web.config file):
Dim myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("NameofConnectionString").ConnectionString) myConnection.Open()
However when I do that, it does not understand what "configurationManager" is. So how do I define a new sqlconnection and open it using the connection string?Thursday, January 31, 2013 9:07 PM
Answers
-
Have you imported or used the namespace System.Configuration? This namespace is available in System.Configuration.dll. You need to ensure that this dll is references from your project as well.Thursday, January 31, 2013 9:15 PM
-
hi Tony,
Object reference not set to an instance of an object means the code is trying to access a member of a reference type variable is set to null. Please debug your code and check if any variable is null.
Best Regards,
Bob Wu
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Marked as answer by Bob Wu-MT Monday, February 11, 2013 10:32 AM
Monday, February 4, 2013 9:27 AM
All replies
-
Have you imported or used the namespace System.Configuration? This namespace is available in System.Configuration.dll. You need to ensure that this dll is references from your project as well.Thursday, January 31, 2013 9:15 PM
-
Hi Tony,
You can also access the connection string through Properties.Settings.
http://social.msdn.microsoft.com/Forums/is/netfxbcl/thread/89783687-266d-4437-8a9f-9589c2a21261
Best Regards,
Bob Wu
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.Friday, February 1, 2013 7:01 AM -
Have you imported or used the namespace System.Configuration? This namespace is available in System.Configuration.dll. You need to ensure that this dll is references from your project as well.
Thanks Sambeet. I added the system.configuration namespace and reference. And that clears out the error in the intellisence.
However now when i play the code, it crashes on the "Dim myConnection" line with the following error:
An unhandled exception of type 'System.NullReferenceException' occurred in ProjectStatus.exe
Additional information: Object reference not set to an instance of an object.
- Edited by TonyLoco23 Friday, February 1, 2013 4:39 PM
Friday, February 1, 2013 4:39 PM -
hi Tony,
Object reference not set to an instance of an object means the code is trying to access a member of a reference type variable is set to null. Please debug your code and check if any variable is null.
Best Regards,
Bob Wu
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Marked as answer by Bob Wu-MT Monday, February 11, 2013 10:32 AM
Monday, February 4, 2013 9:27 AM