Click here to Skip to main content
15,914,905 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)


C#
public class commonfunction
    {
        #region Variable Declaration

        SqlConnection _connection;
        SqlCommand _command;
        SqlDataAdapter _Adapter;
        DataSet _DS;
        int _count;

        #endregion

        #region Get DataSet

        public DataSet GetDataSet(string _query)
        {

            try
            {
                _DS = new DataSet();
                _connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString);
                _command = new SqlCommand(_query, _connection);
                _Adapter = new SqlDataAdapter(_command);
                _Adapter.Fill(_DS);
                return _DS;

            }
            catch (Exception)
            {

                throw;

            }
            finally
            {
                _DS.Dispose();
            }

        }
        #endregion

        #region Execute Query

        public int ExecuteQuery(string _query)
        {
            try
            {
                _connection =  new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString);
                _connection.Open();
                _command = new SqlCommand(_query, _connection);
                _count = _command.ExecuteNonQuery();
                _connection.Close();
                return _count;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                _connection.Close();
            }

        }

        #endregion
    }
Posted
Updated 11-Aug-13 2:09am
v3
Comments
[no name] 11-Aug-13 8:10am    
Your error message means exactly what it says. You need to check your connection string.

1 solution

Could be one of the following:

- Wrong server name
- Wrong credentials
- Disabled remote connection
- Firewall blocking

Check one by one to validate those are in order

Cheers,
Edo
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900