1

I have the correct entries in the web.config file in my ASP.NET project. How do I make sure that the connection has been established successfully? I want to pull an image form my database and then display it on my aspx page. Some things to note: I am using Visual Studio 2010, SQL server 2008, .NET 4.0
Here is the relevant part from my web.config file

<databases>    
    <add key="MyConnection" name="MyName" value="server=servername\SQL2008;uid=myuid;pwd=mypwd;database=databaseName;"/>  
    <add key="DataAccessClass" value="DataAccessSql"/>  
</databases> 

I do not have any app.config file in my project yet. Surprisingly there is not section in my web.config. Do I need to add one explicitly?

2
  • I don't understand the question. If you can connect to the database, the connection has been established. Are you encountering a specific issue? Commented Oct 23, 2010 at 21:27
  • No. I just want to know that simply adding the connectionstring/UID/password/databaseName to web.config file opens a connection in asp.net? or do I have to explicitely write a c# code behind to establish it? Commented Oct 23, 2010 at 21:32

2 Answers 2

3

Web.config file is just a store for configuration settings. In order to establish a connection to the database you need to write some code.

ASP.NET - Database Connection

Creating Connections to SQL Server

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

Comments

1

The connection parameters entered in the web.config file are just parameters. No actual connections are made. In fact, multiple connection parameters can be made in the web.config and the actual connection can be made at runtime.

To actually make a connection happen, you need to define

For instance, here the SQLDataSource is set to connect to ConnectionStrings.MyNorthwind

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
      <asp:SqlDataSource
          id="SqlDataSource1"
          runat="server"
          DataSourceMode="DataReader"
          ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
          SelectCommand="SELECT LastName FROM Employees">
      </asp:SqlDataSource>

      <asp:ListBox
          id="ListBox1"
          runat="server"
          DataTextField="LastName"
          DataSourceID="SqlDataSource1">
      </asp:ListBox>

    </form>
  </body>
</html>

or in this second example, where they explicitely create an SqlConnection. The connectionstring here would be taken from the web.config.

private static void ReadOrderData(string connectionString)
{
    string queryString = 
        "SELECT OrderID, CustomerID FROM dbo.Orders;";
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(
            queryString, connection);
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();
        try
        {
            while (reader.Read())
            {
                Console.WriteLine(String.Format("{0}, {1}",
                    reader[0], reader[1]));
            }
        }
        finally
        {
            // Always call Close when done reading.
            reader.Close();
        }
    }
}

Here is an example of a connection string to be found in your web.config

<configuration>
  <connectionStrings>
    <add name="ApplicationServices"
         connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
         providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

4 Comments

I am facing an error if I try to implement the connection in the markup: Heres my web.config line: <add key="MyConnection" name="DescriptionColumn" value="server=project\SQL2008;uid=myuid;pwd=mypwd;database=tableName;"/> The error is this: The connection name 'DescriptionColumn' was not found in the applications configuration or the connection string is empty.
Where is the application config file/ Its not in my asp.net project directory.
Doh, this comment section is not designed for code... I'll add it to the end of my post (above)
Having re-read your addition, where are you hosting your app? From msdn.microsoft.com/en-us/library/ms733932.aspx, it says "The choice of the configuration file name is determined by the hosting environment you choose for the service. If you are using IIS to host your service, use a Web.config file. If you are using any other hosting environment, use an App.config file." It is not clear where you are hosting. For IIS, use Web.Config

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.