Do you have SqlConnectionString properly configured in your configuration
file? I think it's missing.
Try using System.Configuration.ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString,
LocalSqlServer is configured in machine.config file, so you should be able
to get that.
J.W.
Hello vinnie,
> i'm at the very beginning, and while trying to use the following small
> code, i got an error msg thta said use the "new" keyword to create an
[quoted text clipped - 9 lines]
>
> Thanks
> i'm at the very beginning, and while trying to use the following small
> code, i got an error msg thta said use the "new" keyword to create an
> object instance.
It's lying to you.
:)
Well, actually...it might be that the error includes other information.
But if all it says is to use "new", then at least based on the line of
code you posted it doesn't appear that advice is correct.
> Actually, since i'm at the very beginning, i don;t really know hot to
> change it. This is the code:
>
> string strConnectionString =
> ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
I haven't used that part of the API, but it should just work fine as
you've posted the code.
The ConnectionStrings property returns a
ConnectionStringSettingsCollection and using the ["SqlConnectionString"]
indexer on it should return a ConnectionStringSettings instance, which
has a ConnectionString property that is of the type string.
In other words, the line of code should return a string and assign it to
your variable "strConnectionString". (All of that assumes the
"SqlConnectionString" identifies an actual item in the collection, but
that wouldn't be a compile-time error in any case).
So...
Given that the code posted should work, there must be something else you
didn't describe about the code that's causing a problem. For best
results, you should post a concise-but-complete example of code that
reproduces the compiler error. Emphasis on "concise".
But, before you do that, you should check to make sure that you have
actually added System.Configuration as a reference for your project
(some namespaces require explicitly being added as a reference, some are
already in the default references). I can see how you'd get a compiler
error that tries to be helpful about how to instantiate a string if the
problem was actually that the compiler wasn't aware of the API you're
trying to use.
If that's not the problem, then along with the example of code I suggest
above, you should also post the _exact_ text of the error. Error
messages have very specific details within them that often can lead
directly to a solution. But we can only offer that solution if you post
those exact details.
Pete
Jianwei Sun - 28 Aug 2007 02:19 GMT
Hello Peter,
>> i'm at the very beginning, and while trying to use the following
>> small code, i got an error msg thta said use the "new" keyword to
[quoted text clipped - 23 lines]
> ConnectionStringSettings instance, which has a ConnectionString
> property that is of the type string.
[If the SqlConnectionString is not configured properly, it could return null
here, and I think this is
OP's problem.]
> In other words, the line of code should return a string and assign it
> to your variable "strConnectionString". (All of that assumes the
[quoted text clipped - 23 lines]
>
> Pete
Peter Duniho - 28 Aug 2007 03:07 GMT
>> The ConnectionStrings property returns a
>> ConnectionStringSettingsCollection and using the
[quoted text clipped - 4 lines]
> [If the SqlConnectionString is not configured properly, it could return
> null here, and I think this is OP's problem.]
Could be. The OP wasn't clear at all about whether they were getting a
compile error or a run-time error. If the former, then a missing key
isn't the issue. But if the latter, it could very well be.
Hard to say without seeing the exact error. Which of course reinforces
the point that one should always post clear, precise descriptions of
their problems, including the full text of error messages and under what
circumstances they occur.
Pete
Ben Voigt [C++ MVP] - 28 Aug 2007 21:41 GMT
>>> The ConnectionStrings property returns a
>>> ConnectionStringSettingsCollection and using the
[quoted text clipped - 10 lines]
>
> Hard to say without seeing the exact error. Which of course reinforces
Bet it is a NullReferenceException, which may very well give instructions to
users on how to get an instance of a class.
int i; i.ToString(); // ok
Form f; f.ToString(); // new programmers might not understand why this
doesn't work, NullReferenceException message is meant for them
Peter Duniho - 29 Aug 2007 00:09 GMT
> Bet it is a NullReferenceException, which may very well give instructions to
> users on how to get an instance of a class.
Sure, that could be a good bet. Even when I started out with C#, that's
not an exception I ever had to deal with, so can't say I'm all that
familiar with the exact text of the runtime error.
Still, we're just betting until the OP clarifies.
Pete
Ben Voigt [C++ MVP] - 29 Aug 2007 23:45 GMT
>> Bet it is a NullReferenceException, which may very well give instructions
>> to users on how to get an instance of a class.
>
> Sure, that could be a good bet. Even when I started out with C#, that's
> not an exception I ever had to deal with, so can't say I'm all that
> familiar with the exact text of the runtime error.
I cause that error every now and then myself, but a programming on the same
project likes to use try/catch instead of testing the variable beforehand...
> Still, we're just betting until the OP clarifies.
>
> Pete