Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm writing a web tool using asp.net and VB. I've been instructed by my client to use the web.config file to hold the database connection string (instead of using a global variable, which I've been doing up to now).

Most of the online help for this agrees on how to do this, with minor differences. The web.config file:

<configuration>
  <connectionStrings>
     <add name="MyConn"
         connectionString="Provider=SQLOLEDB;Data Source=MyServer;
             Initial Catalog=DBName;Persist Security Info=True;User ID=MyUser;
             Password=MyPass"
         providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

And in my code-behind page:

Imports System.Configuration
Dim conn as String

conn = 
ConfigurationManager.ConnectionStrings["MyConn"].ConnectionStrings

The forums I've looked at agree that this should work in vb or C#. But in my code-behind, I'm getting the error "Value of type 'System.Configuration.ConnectionStringSettingsCollection' cannot be converted to 'String'." If I change the connection string to an object:

Dim conn as object

Then the error goes away, but I don't think that's solving the problem. Can anyone tell me why the above code won't render the string in my web.config file? According to multiple forums, this ought to work.

Thanks for any help.

EDIT:

I did try ending the line with 'ConnectionString' (final 's' removed), which is in the online help - same result. I left the final 's' in my code because the Microsoft 'Intellesense' thing puts it in there, but it gives me the same result either way.

FURTHER EDIT:

When I changed the conn to a 'Object' type, my code tells me 'Identifier Expected' where the ["MyConn"] is. There does seem to be something wrong, even though several online resources tell me this is correct.

Thanks again.

share|improve this question
    
Is the s at the very end of conn = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionStrings a typo? –  Andrew Morton Apr 1 at 15:51
    
Thanks Andrew - please see edit. –  Stanton Apr 1 at 16:39
add comment

1 Answer

up vote 2 down vote accepted
conn = 
ConfigurationManager.ConnectionStrings["MyConn"].ConnectionStrings

Must be (pay attention to the VB.NET style)

conn = 
ConfigurationManager.ConnectionStrings("MyConn").ConnectionString

And that returns a string.

share|improve this answer
    
Thanks for your reply. I neglected that in my post, but I did try deleting the last 's' in the syntax - same result. I left the final 's' in my code because the Microsoft 'Intellesense' thing puts it in there, but it gives me the same result either way. –  Stanton Apr 1 at 16:37
    
ok, your code sample is C#... i will change it to VB.NET –  T McKeown Apr 1 at 16:40
    
try using the example i posted, VB.NET must use parens for indexing –  T McKeown Apr 1 at 16:45
1  
@T McKeown, that was the problem. Thank you so much for your help! –  Stanton Apr 1 at 16:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.