I am attempting to run a program that gets data from a remote sql server, it works for a coworker from another remote site but when I run the program the debugger says that Sql_Reader is returning null.
I can ping and connect to the remote sql server. The instance is correct as well as the firewall settings on the server per http://blogs.msdn.com/b/sql_protocols/archive/2007/05/13/sql-network-interfaces-error-26-error-locating-server-instance-specified.aspx However when I run a program to get data from the sql server there is and error that says that remote server was not found or not accessible.
I checked the connection string per error: 26 - Error Locating Server/Instance Specified. (Can't connect to my local Db From my host server)
This is the connection string that is in the GetConnectionString method.
return @"Data Source= 2.2.2.2\SQLEXPRESS; Persist Security Info=False;User ID=myusername;Password=mypassword;Initial Catalog=SomeDataStructure;";
The following error happens on the line that has sql_Connection.Open();
SqlException was Unhandled. 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)
// Body of the method
str_Query = "SELECT " + dict_SearchData["FieldName"] + " FROM " + dict_SearchData["Table"] +
" WHERE " + dict_SearchData["IndexColumn"] + " = '" + dict_SearchData["IndexRow"] + "';";
using (SqlConnection sql_Connection = new SqlConnection(dict_SearchData["ConnectionString"]))
{
SqlCommand sql_Command = new SqlCommand(str_Query, sql_Connection);
sql_Connection.Open();
SqlDataReader sql_Reader = sql_Command.ExecuteReader();
try
{
sql_Reader.Read();
str_FieldValue = sql_Reader[0].ToString();
}
catch (System.InvalidOperationException)
{
//ignore the exception
}
finally
{
// Always call Close when done reading.
sql_Reader.Close();
}
}