Error (Connected String) Asp . Net MVC - Visual Studio 2013 and SQL Server 2012

Asked

Viewed 345 times

1

I have a problem with my MVC Asp.Net. I created the Connection String inside Webconfig,

<connectionStrings>
    <add name="connSql"
    providerName="System.Data.SqlClient"
    connectionString="Data Source=localhost;
          Initial Catalog=CarnetAdresse;
          Integrated Security=True" />

  </connectionStrings>

then created the Getlist() method within the "Contact" Template to bring the database list :

 public static List<Contact> GetList()
        {
            string connexionSql = ConfigurationManager.ConnectionStrings["connSql"].ConnectionString;

            using (SqlConnection cnx = new SqlConnection(connexionSql))
            {
                string requete = "SELECT * FROM Contact";

                SqlCommand cmd = new SqlCommand(requete, cnx);
                cmd.CommandType = System.Data.CommandType.Text;
                try
                {
                    cnx.Open();
                    SqlDataReader dataReader = cmd.ExecuteReader();
                    List<Contact> contactList = new List<Contact>();
                    while (dataReader.Read())
                    {
                        Contact c = new Contact();
                        c.id = (int)dataReader["id"];
                        c.Nom = (string)dataReader["Nom"];
                        c.Telephone = (string)dataReader["Telephone"];
                        c.Courriel = (string)dataReader["Courriel"];
                        c.DateNaissance = (DateTime)dataReader["DateNaissance"];
                        c.CodePostal = (string)dataReader["CodePostal"];

                        contactList.Add(c);
                    }
                    dataReader.Close();
                    return contactList;
                }
                finally
                {
                    cnx.Close();
                }
            }
        }

and he gives me an Exception every time I try to run the program:

Une Exception de type 'System.Nullreferenceexception' s'est produite dans Carnetadresse.dll mais n'a pas été gérée dans le code utilisateur

Informations supplémentaires : Object Reference not set to an instance of an Object.

Ele diz que string connexionSql é null

  • Detail: Use Sqlserver 2012. On the teacher’s computer he used SQLEXPRESS and it worked, but in my nothing to work. I have deleted the created database other times, already modified the names of the connection string, rewrote the code... and nothing yet

  • Another thing I tried: I added a rule in Windows Firewall to allow port 1433 for SQL SERVER. Firewall allow port 1433 for SQL Server Link: https://blogs.technet.microsoft.com/danstolts/2011/06/how-to-open-firewall-port-1433-for-sql-server-database-engine-for-use-with-scom-or-thinanyg-else/

1 answer

2


There are two web.config in a web application .net. One inside the application root and one inside the views root. This code is in which web.config? It’s right to be on the web.config from the root of the application.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.