Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I am trying to fetch connection string from app.config, i have created a connection class and declareing a string which will stores connection string fetched from app.config file

 Public Class ConnectionClassDim 

ConString As String = ConfigurationManager.ConnectionStrings ("ConString").ConnectionString
'Here It will not reading Connectionstring from App.Config What to do for it?
    

    Dim con As SqlConnection
    Dim cmd As SqlCommand
    Dim da As SqlDataAdapter
   
    Public Function GetData(ByVal Query As String) As DataTable

        Dim dt As New DataTable
        Try
            con = New SqlConnection(ConString)
            con.Open()
            da = New SqlDataAdapter(Query, con)

            da.Fill(dt)
            Return dt
        Catch ex As Exception
            Return dt
        End Try
    End Function
End Class


the app.config is like

XML
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <connectionStrings>
    <add name="ConString" connectionString="Data Source=DEV150;Initial Catalog=TrailDb;Integrated Security=false;User Id=sa;Password=sadev150;"
            providerName="System.Data.SqlClient"/>
  </connectionStrings>


</configuration>
Posted
Updated 13-Sep-11 23:55pm
v2
Comments
Herman<T>.Instance 14-Sep-11 6:55am    
you use a datadapter and conn.Open() but not conn.Close(). When using a dataadapter you can also omit conn.Open().

You can use
System.Configuration.COnfigurationSettings.AppSettings["Key"]
 
Share this answer
 

Hi
Please Try this


In Your App.Config file looks like

 <configuration>
  <connectionstrings>
    <add name="DBConnectionString">
      connectionString="Data Source=Manoj;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=admin123"
      providerName="System.Data.SqlClient" />
  </add></connectionstrings>
</configuration>



Please Add Reference "System.Configuration.dll"

IN your code

// Add NameSpaces for
using System.Configuration;

string nwConn = System.Configuration.ConfigurationManager.ConnectionStrings[1].ToString();
 
Share this answer
 
Comments
Phân Tử Hidro 18-Apr-13 6:16am    
this is in C# not VB.NET
Savalia Manoj M 19-Apr-13 0:01am    
Dim nwConn As String = System.Configuration.ConfigurationManager.ConnectionStrings(1).ToString()
Move the read into your page load function - that way it will be done each time you need it.
 
Share this answer
 
Comments
Yatin Bhagat 14-Sep-11 6:07am    
i have created a seperate class for database operations only
OriginalGriff 14-Sep-11 7:15am    
Then create a ConnectionString Property or a GetConnection method, and call it each time you need it. You can always test if you need to access the settings file by assigning an initial null.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900