Hi. I'm new to ASP.net and I'm having a bit of trouble with loading information in from web.config. I've stored my database connection in the web.config and the rest of the code is in my code behind file. Here is the code behind file followed by the web.config extract. The error is caused on this line SqlConnection conn = new SqlConnection(strConnection); :
By the way, the database connection loads successfully and information is brought back when I hard code the string into the connection method.
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class testDB : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
String strConnection = ConfigurationManager.ConnectionStrings["DBConnection"].ToString();
//newLabel.Text = "Connection = " + strConnection;
SqlConnection conn = new SqlConnection(strConnection);
SqlCommand comm = new SqlCommand("select * from test_tab",conn);
conn.Open();
System.Diagnostics.Debug.WriteLine("ok");
SqlDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
newLabel.Text += reader["name"] + "<br />\n";
}
reader.Close();
conn.Close();
}
catch (InvalidOperationException eve)
{
System.Diagnostics.Debug.WriteLine("Test"+eve.Message);
newLabel.Text = "Test" + eve.Message + "<br />" + eve.ToString();
}
}
}
<connectionStrings>
<add name="DBConnection" connectionString="Server=localhost\\SQLExpress;Database=test;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
The error I'm getting from the exception is:
TestInstance failure.
System.InvalidOperationException: Instance failure. at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, ......