Connecting to database via String in XML
So i have done searching after searching and i keep getting stuck on having the connection error as a string.
Creating the new Connection is throwing error
cannot convert from 'System.Configuration.ConnectionStringSettings' to 'string'
Thanks
40 Replies
because
Connect
is a ConnectionStringSettings
object. You even correctly print the ConnectionString
in your second-to-last code blockI just changed to mycon and still getting it
please look at your middle code block
it is not the same as what you just showed
You want
var mycon = ConfigurationManager.ConnectionStrings["Test"].ConnectionString;
System.NullReferenceException: 'Object reference not set to an instance of an object.'
And your middle code block works?
Set a breakpoint on the line and see what it is returning. It sounds like it isn't finding the ConnectionString named "Test".
i tried to get a msg block working to see if it will show my connectionstring when i seen your post let me back track
Ok, so it can't find it.
What does GetConnectionStrings() output.
And for the record, the providerName should be likely be System.Data.SqlClient not localhost.
Not that it's breaking anything for you.
It's there to support the connection factory which you aren't using, so you can safely ignore it.
Comment out that line, rerun it, and it should output stuff to the console.
sea saw
You are coding in Visual Studio?
yes
Or Visual Studio Code?
2022
studio
There should be a window at the bottom that has text in it
MIght have an "Output" tab
Or might look like this when you are running at the bottom right:
If you can't find the output window... That's fine. Put the cursor on the line that reads "foreach (ConnectionStringSettings cs in settings)" and hit the F9 key. It should put a red dot on that line on the left hand side. Hit the F5 key and it will run your application until it gets there.
Then you can hover your mouse over the word settings and you can see what is in there.
Hit the F10 key and it should go to the next line. If it doesn't, that means settings is empty and you have a problem with your config file.
yes, im having to rewrite the callout to xml, Im new at C# Sorry
No problem, we all started there once.
So iv tried old school way and this is my result
You don't have those settings in your app.config file?
i cant get it to pull from there
Is this a .NET Framework application or a .NET Core application?
When you compile the code, it should compile into the bin folder somewhere. Is there a Store My Data.exe in there? And is there a Store My Data.exe.config in the same folder?
In your Solution Explorer, right click on your project name. In the menu that appears, select "Add new item...":
If the compact view appears like this:
Click the Show All Templates button.
Then you should get a dialog like this, and select "Application Configuration File":
Leave the default "App.config" alone at the bottom, and click add
Then you should have an App.config file in your application and that is where ConfigurationManager expects it to be.
Like this:
And if you open that file, it should look like the familiar:
This is the App.config that I added your stuff to:
Hope that helps
I had a pre-existing project called "RedboxDressed" that I just shoved that into.
Let me give it a shot
@Motley That worked TY SO Very Much
Glad to help!