C# – Failure to connect the web page to a database

cconnection-stringsql

UPDATE: I missed that I should use using MySql.Data.MySqlClient reference and MySqlConnection since I have been trying to connect a mysql server. Java mislead me I could connect easily with two lines. sorry for taking time**


I'm trying to connect my web page to a database. I can connect easily using java in order to generate tables but I cannot connect with C# / Asp.

The error on the output screen is:

A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Code:

try {
    con = new SqlConnection("Data Source = urlhere ;" +
                            "uid = u_yucel;" +
                           "pwd = *****; " +
                            "database = u_yucel; " +
                            "connection timeout = 2");
    cmd.CommandText = "select * from person";
    con.Open();
    cmd.Connection = con;

    DropDownList1.DataSource = cmd.ExecuteReader();
    DropDownList1.DataTextField = "name";
    DropDownList1.DataBind();
}
catch (Exception ex)
{
    Response.Write(ex.Message);
    Console.WriteLine("123");
    Response.Write(ex.StackTrace);
}

Error Message:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 – Could not open a connection to SQL Server) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity) at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject) at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout) at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) at System.Data.SqlClient.SqlConnection.Open() at DataDeneme1.carSelect.rebind() in E:\VisualStudioProjects\DataDeneme1\DataDeneme1\carSelect.aspx.cs:line 38

Best Answer

Your connection string isn't valid. Either use a standard .net provider connection string. Or specify the provider. See here for examples

http://www.connectionstrings.com/sql-server-2008

http://www.connectionstrings.com/sql-server-2005

Related Topic