0

Currently I'm trying to connect SQL server DB from asp.net 4.5, keeping the connection string on Web.config file, using the following code to retrieve the connection string, but its retunr NULL value,

ConnectionStringSettings connString = ConfigurationManager.ConnectionStrings["ConString"];                 
SqlConnection Con = new SqlConnection(connString.ConnectionString);

the connection string in Web.Config is like

<connectionStrings>
  <add name="ConString" 
    connectionString="Data Source=myservername;Initial Catalog=dbname;User ID=userid;Password=password;Integrated Security=True" 
    xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings> 

I'm not understanding why it return null value, previously, in 3.5 I have used the following code to get the connection string,

Con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);

it was working fine asp.net 3.5, but this also getting runtime error while using. Please let me know if I did any mistake.

10
  • Is connString null, or is the ConnectionString property of connString null? Commented Jul 5, 2013 at 12:19
  • What is the runtime error? Commented Jul 5, 2013 at 12:20
  • Error In Get DataObject reference not set to an instance of an object. Commented Jul 5, 2013 at 12:22
  • What are the Transform and Locator attributes for? I've never seen that in a connection string setting before. Commented Jul 5, 2013 at 12:24
  • it coming automatically in 4.5, I can remove that Commented Jul 5, 2013 at 12:25

3 Answers 3

4

Although, this is a mere suggestion/things to check, I am adding this as an answer

1) Can you please check if the web.config is at its right location?
2) Also, do you have any web.config (which overrides main web.config) in the same folder as that of your page?

Sign up to request clarification or add additional context in comments.

Comments

0

Try below code:

string connectionstring = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString.ToString();

I hope it will help you.. :)

3 Comments

ConnectionString property is a string and besides the useless ToString() it's code he's using.
getting this error "Error In Get DataObject reference not set to an instance of an object."
yeah, i forgot to remove that.. :P
-1

I use this code. It works in our application.
If that doesn't work, you have some other problem.

    Protected Shared strStaticConnectionString As String = Nothing

    Public Shared Function GetConnectionString() As String
        Return GetConnectionString(Nothing)
    End Function

    ' Requires reference to System.Configuration
    Public Shared Function GetConnectionString(ByVal strIntitialCatalog As String) As String
        Dim strReturnValue As String = Nothing

        If String.IsNullOrEmpty(strStaticConnectionString) Then
            Dim strConnectionStringName As String = System.Environment.MachineName

            If String.IsNullOrEmpty(strConnectionStringName) Then
                strConnectionStringName = "LocalSqlServer"
            End If

            ' Walk through the collection and return the first 
            ' connection string matching the connectionString name.
            Dim settings As System.Configuration.ConnectionStringSettingsCollection = System.Configuration.ConfigurationManager.ConnectionStrings
            If (settings IsNot Nothing) Then
                For Each cs As System.Configuration.ConnectionStringSettings In settings
                    If StringComparer.OrdinalIgnoreCase.Equals(cs.Name, strConnectionStringName) Then
                        strReturnValue = cs.ConnectionString
                        Exit For
                    End If
                Next
            End If

            If String.IsNullOrEmpty(strReturnValue) Then
                strConnectionStringName = "server"

                Dim conString As System.Configuration.ConnectionStringSettings = System.Configuration.ConfigurationManager.ConnectionStrings(strConnectionStringName)

                If conString IsNot Nothing Then
                    strReturnValue = conString.ConnectionString
                End If
            End If

            settings = Nothing
            strConnectionStringName = Nothing
        Else
            If String.IsNullOrEmpty(strIntitialCatalog) Then
                Return strStaticConnectionString
            End If

            strReturnValue = strStaticConnectionString
        End If

        If String.IsNullOrEmpty(strReturnValue) Then
            strReturnValue = GetConnectionString_Old()

            If String.IsNullOrEmpty(strReturnValue) Then
                Throw New ArgumentNullException("ConnectionString ""server"" in file web.config.")
            End If
        Else
            Dim sb As New System.Data.SqlClient.SqlConnectionStringBuilder(strReturnValue)

            If String.IsNullOrEmpty(strStaticConnectionString) Then
                If Not sb.IntegratedSecurity Then
                    sb.Password = DeCrypt(sb.Password)
                End If
                strReturnValue = sb.ConnectionString
                strStaticConnectionString = strReturnValue
            End If

            If Not String.IsNullOrEmpty(strIntitialCatalog) Then
                sb.InitialCatalog = strIntitialCatalog
            End If

            strReturnValue = sb.ConnectionString
            sb = Nothing
        End If

        Return strReturnValue
    End Function ' GetConnectionString

Then this web.config file:

<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

  <appSettings>

  </appSettings>


  <connectionStrings configSource="connections.config"/>


  <system.web></system.web>


  <system.webServer></system.webServer>

</configuration>

And this connection string file (connections.config)

<?xml version="1.0"?>
<connectionStrings>

  <remove name="server"/>
  <add name="server" connectionString="Data Source=localhost;
    Initial Catalog=YOUR_DB;
    Persist Security Info=False;
    User Id=YOUR_USER;
    Password=YOUR_PW;
    MultipleActiveResultSets=False;
    Packet Size=4096;
    Application Name=&quot;YOUR_APPLICATION_NAME&quot;" 
    providerName="System.Data.SqlClient"/>

</connectionStrings>

1 Comment

Not sure how this helps OP with their issue - the code they are using should work. Your code, while interesting, is overkill for their problem (IMO) (not to mention OP is using C#).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.