lundi 29 juin 2015

Error on Calling Connection String Define on Web.config [duplicate]

This question already has an answer here:

I have been scrolling on the answers related to my problem. I also tried the upvoted answers but still Im having the same error. I already installed the mysql connector and added in the reference. And I could not figure out why is this happening. Please help.

My Web.config file connection string look like:

<configuration>

  <appSettings>
    <add key="webpages:Enabled" value="false" />
  </appSettings>

  <connectionStrings>
    <add name="Dbconnection"
         connectionString="Server=localhost; Database=dbtest;Username=root;Password=password;Integrated Security=True"
         providerName="Mysql.Data.MySqlClient" />
  </connectionStrings>

</configuration>

And I am trying to call to in my Controller(just to check) this way:

using System;
using System.Collections.Generic;

using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using MySql.Data.MySqlClient;

public ContentResult checkConn()
{
    MySqlConnection cnn = new MySqlConnection();
    cnn.ConnectionString = ConfigurationManager.ConnectionStrings["Dbconnection"].ConnectionString; 
    return Content(cnn.ConnectionString);
}

I am getting an error prompting: "Object reference not set to an instance of an object."

Aucun commentaire:

Enregistrer un commentaire