2
votes
protected OdbcConnection conectarBD()
{
    String StringDeConexion = "Data Source=PABLOZN\\SQLEXPRESS;Initial Catalog=Proyecto2;Integrated Security=True";

    try
    {
        OdbcConnection conexion = new OdbcConnection(StringDeConexion);
        conexion.Open();
        return conexion;
    }
    catch (Exception ex)
    {
        Label3.Text = ex.StackTrace.ToString();
        return null;
    }
}

The problem is that when I browse my website, the Label shows this exception on the line number 18:

en System.Data.Odbc.OdbcConnection.HandleError(OdbcHandle hrHandle, RetCode retcode) en System.Data.Odbc.OdbcConnectionHandle..ctor(OdbcConnection connection, OdbcConnectionString constr, OdbcEnvironmentHandle environmentHandle) en System.Data.Odbc.OdbcConnectionOpen..ctor(OdbcConnection outerConnection, OdbcConnectionString connectionOptions) en System.Data.Odbc.OdbcConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject) en System.Data.ProviderBase.DbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) en System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions) en System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) en System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) en System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) en System.Data.ProviderBase.DbConnectionInternal.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) en System.Data.Odbc.OdbcConnection.Open() en index.conectarBD() en c:..\Documents\Visual Studio 2012\WebSites\Proyecto\index.aspx.cs:línea 18

2
Why are you using ODBC to connect to a SQL server, why are you using the Native SqlClient?Erik Philips
I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not".John Saunders

2 Answers

0
votes

Verify that the user has access to the database . How do you use trusted connection , verify that the current user has access to the database . If your application is web , make sure NETWORKSERVICE can connect to the database.

You also can try to configure the connection string via DSN in windows ODBC manager.

0
votes

You can either change OdbcConnection to SqlConnection or if for some reason, you must use ODBC the connection string will look like this:

@"DRIVER=SQL Server Native Client 11.0;SERVER=PABLOZN\SQLEXPRESS;DATABASE=Proyecto2;Trusted_Connection=Yes;"