0
votes

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

3
Can you please post the connection string you are trying to use and some code maybe? This is pretty vague. ThanksPepe
What is the message, and stack trace, for the exception? And, post any of your code referenced in that trace.Michael Petrotta
And the actual exception details (at least the message, maybe the stack trace)jeroenh
The exception you're getting amounts to "Something went wrong while retrieving the data." You need to be more specific.Yes - that Jake.
The Message property in the exception object will provide more information as to what's gone wrong here, utkucan.Will A

3 Answers

5
votes

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

4
votes

The server was not found or was not accessible.

This could be caused by a number of different reasons, all of which cannot be diagnosed from our end.

You need to spin up Profiler (you may have to grab the express version if you don't have the full version of Sql Server) and watch the connection.

You will find that

a) No connection attempt is visible
Which means your connection string is invalid (see Vadim's answer for good links re connex strings), or that the sql server is not watching for connections over TCP/IP.

or, it may mean that

b) The connection was rejected for security reasons
which means you have to configure security on the server.

1
votes

You cannot get a connection to your database. You need to look at how your connection string is defined (or post it if you are lost)

Also if you are using multiple Web.config files, make sure that all connection strings are correct, or that your running the Web.config file with the right connection string.