Hello,
I want to make a connection to MSSQL 2008 database, using Visual Studio 2010, ASP.NET C# web site
I wrote this connection string in web.config file:

<connectionStrings>
    <add name="konekcija"
         connectionString="data source=.\SQLEXPRESS;Initial Catalog=Movies;Integrated Security=True;" providerName="System.Data.SqlClient" />
  </connectionStrings>

Now I need to call that connection on a code behind the page... something like:
SqlConnection conn = new SqlConnection.... can anyone help?
thanks

Dani AI

Generated

Your web.config already has a connection string named "konekcija", so the single biggest pitfall is a name mismatch. used "konekcija" in web.config but later code samples show different names like "DBConnectionString" or "NorthwindConnString" — the string you pass to ConfigurationManager must exactly match the name in web.config.

A few practical points that are not explicit in the thread:

  • Add using System.Configuration; and using System.Data.SqlClient; at the top of your code-behind, and add a reference to System.Configuration.dll if the compiler cannot find ConfigurationManager.
  • Prefer to read the ConnectionStringSettings object and use its ConnectionString property (rather than calling ToString()), and always open connections inside a using block so they are closed and returned to the pool automatically.
  • If you use Integrated Security=True, make sure the app pool identity (or the account used by your development server) has a SQL Server login. If you see "login failed" errors, that is usually the cause.

Example pattern (illustrates null-checking, disposal and a parameterized query):

using System.Configuration;
using System.Data.SqlClient;

var cs = ConfigurationManager.ConnectionStrings["konekcija"];
if (cs == null) throw new InvalidOperationException("Missing connection string 'konekcija' in web.config.");

using (var conn = new SqlConnection(cs.ConnectionString))
{
    conn.Open();
    using (var cmd = conn.CreateCommand())
    {
        cmd.CommandText = "SELECT COUNT(1) FROM dbo.YourTable WHERE IsActive = @active";
        cmd.Parameters.AddWithValue("@active", true);
        int count = (int)cmd.ExecuteScalar();
        // use count...
    }
}

Troubleshooting checklist: confirm the connection string name, verify SQL instance name (.\SQLEXPRESS vs LocalDB), ensure SQL service is running, check app pool/Windows-auth permissions when using Integrated Security, and keep connections short. As showed, once the name and retrieval are correct you can bind datasets or grids — just follow the disposal and parameterization practices above.

ow I need to call that connection on a code behind the page...

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                BindEmployeesList();
            }
        }

        private void BindEmployeesList()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NorthwindConnString"].ConnectionString;
            string selectSQL = "SELECT [LastName], [FirstName], [Title], [City], [Email] FROM [Employees]";
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand(selectSQL, con);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();

            adapter.Fill(ds, "Employees");

            gvEmployeesList.DataSource = ds;
            gvEmployeesList.DataBind();
        }

Thanks Dhaker... I needed C#
I found out what to do... here is the code snippet:

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnectionString"].ToString());
                SqlCommand cmd = new SqlCommand("SELECT * FROM someTable", conn);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.