0

I'm trying to access a mysql database hosted online, but every time getting the same error. Here is the error message, code and web.config connectionStrings I used:

"An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code"

"Additional information: 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)"

            <div class="col-md-12">
                @{
                    var db = Database.Open("SQLServerConnectionString");
                    var selectQueryString = "SELECT * FROM `emp`";
                    var data = db.Query(selectQueryString);
                    var grid = new WebGrid(data);
                }
                @grid.GetHtml()
            </div>

  <connectionStrings>
    <add
      name="SQLServerConnectionString"
      connectionString= "server=**.***.***.*;database=Zain;uid=Zain;pwd=Zain123"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

0

1 Answer 1

1

You're passing in the string "SQLServerConnectionString" as a connection string, you should be passing a variable containing your connection string.

var db = Database.Open("SQLServerConnectionString");

should be something like:

var connectionString = ConfigurationManager.AppSettings["SQLServerConnectionString"]
var db = Database.Open(connectionString);
Sign up to request clarification or add additional context in comments.

2 Comments

im getting this error : The name 'ConfigurationManager' does not exist in the current context
i used System.Web.Configuration.WebConfigurationManager.AppSettings["ServerConnectionString"] instead of ConfigurationManager.AppSettings["SQLServerConnectionString"] but now i'm getting this error : An exception of type 'System.ArgumentException' occurred in WebMatrix.Data.dll but was not handled in user code Additional information: Value cannot be null or an empty string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.