Reputation: 117
So I'm working on a project involving an external API and a database. This API uses an API specific project type called an Add In and is compiled as a library, then added to the API's source program. This way, I can create self-created 'actions' within in the Add in for the program and call them remotely from a MVC web project. This action contains code also related to updating a database uploaded on a server. When I tried to call this action remotely, I would get this error within the application the add in was added to:
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)
I've gotten help with my mentor and troubleshooted the connection string inside the Add Ins app.config. It's verified to be correct (I can access the database directly from the web MVC project). I tried reading the connection string using var connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
in the project, and it just can't be found. When the action is called I get a null object reference error.
Here's my app config for the Add In:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=PROD4W\PROD4W;Initial Catalog=EplanInterfaceDb;user id = SomeID; password=SomePassword;Integrated Security=False" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
Here's my code related to the database inside my add In project:
//Here is where my mentor tried to pull the connection string from the //app.config.This is where the object reference error
var connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
//Output the connection string to the screen on outside app
new Decider().Decide(EnumDecisionType.eOkDecision, "Connection: " + connectionString, "", EnumDecisionReturn.eOK, EnumDecisionReturn.eOK);
//Access the database and add a test
using (var context = new DataContext())
{
//Throws network related error mentioned above
context.Macros.Add(new Macro { Name = "test" });
context.SaveChanges();
//Output to the screen if the savechanges was successful
new Decider().Decide(EnumDecisionType.eOkDecision, "Save Changes Called ", "", EnumDecisionReturn.eOK, EnumDecisionReturn.eOK);
}
Upvotes: 3
Views: 735
Reputation: 1258
Using what Mason recommended below you can simply hard code your connection string instead of pulling it from the app.config if it is giving you issues.. Here is an example:
var connectionString = "Hard code connection string";
//Make sure your DataContext has a constructor that takes in a connection string
using (var context = new DataContext(connectionString))
{
context.Macros.Add(new Macro { Name = "test" });
context.SaveChanges();
//Output to the screen if the savechanges was successful
new Decider().Decide(EnumDecisionType.eOkDecision, "Save Changes Called ", "", EnumDecisionReturn.eOK, EnumDecisionReturn.eOK);
}
Upvotes: 2