Problem while using Global Connection String

0 views
Skip to first unread message

SV

unread,
May 12, 2008, 3:08:16 AM5/12/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I am new to .Net. I am working on .Net 2005. I am defining connection
string in my web.config file as :

<configuration>
<connectionStrings>
<add name="Conn" connectionString="Data
Source=xxx;Initial
Catalog=yyy;Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>


And I am using that String in my class to open connection as:


Public Sub OpenConnection()


sqlConn = New SqlConnection
sqlComd = New SqlCommand
sqlComd.Connection = sqlConn


strConn =
Configuration.ConfigurationManager.AppSettings("Conn").ToString()


sqlConn.ConnectionString = strConn


sqlConn.Open()


End Sub


But I am getting error “System.NullReferenceException was unhandled
by
user code” and “Object reference not set to an instance of an
object.”


Where I am wrong?


I tried the following syntax also:
strConn =
System.Configuration.ConfigurationManager.AppSettings("Conn").ToString()


But I am still getting same error.


Any help really appreciated.


Thanks,
SV

Andrew Badera

unread,
May 12, 2008, 8:22:23 AM5/12/08
to DotNetDe...@googlegroups.com
<connectionStrings> != <appSettings>

you want ConfigurationManager.ConnectionStrings("Conn").ConnectionString
--
--
--Andy Badera
http://higherefficiency.net
http://flipbitsnotburgers.blogspot.com/
http://andrew.badera.us/
http://changeroundup.com/
and...@badera.us
(518) 641-1280
Google me: http://www.google.com/search?q=andrew+badera

Cerebrus

unread,
May 12, 2008, 12:12:54 PM5/12/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Lol ! That was just plain silly of him/her !

adityo dwiarto

unread,
May 12, 2008, 7:47:59 AM5/12/08
to DotNetDe...@googlegroups.com
mine works
System.Configuration.ConfigurationManager.AppSettings["Conn"].ToString()

Crisatunity (blog.crisatunity.com)

unread,
May 12, 2008, 3:21:38 PM5/12/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Works perhaps, sucks definitely.

On May 12, 6:47 am, "adityo dwiarto" <adityodwia...@gmail.com> wrote:
> mine works
> System.Configuration.ConfigurationManager.AppSettings["Conn"].ToString()

Putting connection strings into the appSettings element when there's a
perfectly good connectionStrings element made for the purpose is
insane.

Rupi

unread,
May 15, 2008, 1:58:27 AM5/15/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
try this one
r u doing coding in c# if yes dan make on more tab in web .config
dat is

<appSettings>
<add key="conn" value="Data Source=xxxx;Initial Catalog=xx;User
Id=sa; pwd=admin06">
</add>
</appSettings>


and dan write following code in class

string ConnString =
System.Configuration.ConfigurationSettings.AppSettings.Get(0);



Joe Enos

unread,
May 15, 2008, 12:57:58 PM5/15/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
As was already mentioned above, there's a connectionStrings section of
web.config which is better suited for connection strings than the
appSettings. In addition, you can't possibly be serious about Get(0)
- I'd be truly frightened to see any code that does that.

Jamie Fraser

unread,
May 16, 2008, 3:47:19 AM5/16/08
to DotNetDe...@googlegroups.com
System.Configuration.ConfigurationSettings.AppSettings.Get(0);

ARE YOU JOKING?!

And who is Dan?

Cerebrus

unread,
May 16, 2008, 12:39:50 PM5/16/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
As Joe and Jamie promptly pointed out, your code is gross and prone to
error. The AppSettings property exposes a readonly NameValueCollection
so what is the point of having a key-value pair if you are not going
to access the value using the key ? In any case, in .NET 2.0, you
should be using the ConfigurationManager.AppSettings, because
ConfigurationSettings is obsolete.

SV

unread,
May 15, 2008, 10:07:26 PM5/15/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Ahhh, it’s really silly.
Now it works perfect with

ConfigurationManager.ConnectionStrings("Conn").ConnectionString


Thanks all for your replies.
I really appreciate it.


SV
> --Andy Baderahttp://higherefficiency.nethttp://flipbitsnotburgers.blogspot.com/http://andrew.badera.us/http://changeroundup.com/
> and...@badera.us
> (518) 641-1280
> Google me:http://www.google.com/search?q=andrew+badera- Hide quoted text -
>
> - Show quoted text -

Rupi

unread,
May 17, 2008, 4:55:59 AM5/17/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
i m using this and its working properly in my application

On May 16, 12:47 pm, "Jamie Fraser" <jamie.fra...@gmail.com> wrote:
> System.Configuration.ConfigurationSettings.AppSettings.Get(0);
>
> ARE YOU JOKING?!
>
> And who is Dan?
>

Cerebrus

unread,
May 18, 2008, 2:38:17 AM5/18/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Read my post above.

Andrew Badera

unread,
May 18, 2008, 4:56:24 AM5/18/08
to DotNetDe...@googlegroups.com
Just because it "works" doesn't mean it "works right" or "will continue to work in the future." You need to program for maintainability and flexibility. If someone adds a key to your config file, before the element you currently have at (0), the new element will take over the spot, which could be inadvertent.

Don't program array/collection/datareader/etc. lookups based on hardcoded index values if you can help it (or at least do it with an easily-accessed CONST). Always use named values whenever possible, or you lose the power and resiliency of these .config files.
Reply all
Reply to author
Forward
0 new messages