The ConnectionString property has not been initialized

Hello good day I'm programming in C # I have a method that I show below, but it throws me an error that says

An unhandled exception of type 'System.InvalidOperationException ' occurred in System.Data.dll Additional information: ConnectionString{[2] property has not been initialized]}

I really can't find the error if anyone can help me I would really appreciate it.

public static bool Conectar()
    {
        bool conectado = false;
        conexion = new SqlConnection(cadenaConexion);
        try
        {            
            conexion.Open();
            conectado = true;
        }
        catch (SqlException ex)
        {
            errorconexion = ex.Message;
        }
        return conectado;
    }
 2
Author: Dev. Joel, 2016-10-24

3 answers

I would recommend that you do the following:

In your web config

<connectionStrings>
    <add name="DBConnStr" connectionString="server=127.0.10.31;database=Example;UID=admin;PWD=123456" providerName="System.Data.SqlClient"/>
</connectionStrings>

And I would also recommend that you use so your variable, more than a method as such, define a variable within your class, service or whatever, I will assume that it is a service

namespace MiNameSpace
{
    public class IServiceTopStore : IIServiceTopStore
    {
        public static string Db = (ConfigurationManager.ConnectionStrings["DBConnStr"].ConnectionString);
        public static bool Conectar()
        {
            bool conectado = false;
            SqlConnection conexion = new SqlConnection(Db);
            try
            {            
               conexion.Open();
               conectado = true;
            }
            catch (SqlException ex)
            {
               errorconexion = ex.Message;
            }
            return conectado;
         }
    }
}

Of course, all this thinking that you have some wcf web application, or a ws as such, but if it is not the case it is not easier than:

public class MiClase
{
    public static string Db = "server=127.0.10.31;database=Example;UID=admin;PWD=123456"
    public static bool Conectar()
    {
        bool conectado = false;
        SqlConnection conexion = new SqlConnection(Db);
        try
        {            
           conexion.Open();
           conectado = true;
        }
        catch (SqlException ex)
        {
           errorconexion = ex.Message;
        }
        return conectado;
     }
}
 1
Author: sioesi, 2016-10-24 19:30:20

If you have a method that inicializa your string variable of Conexion , you should call it before performing SqlConnection otherwise, that variable will have no value

 public static void inicializar() {
  cadenaConexion = "SERVER=127.0.10.31;DATABASE=Example;User id=admin; Password=123456;" 
  } 

 public static  bool Conectar()
    {
        bool conectado = false;
        inicializar(); /*Dar valores a tu cadena de conexion*/
        conexion = new SqlConnection(cadenaConexion);
        try
        {
            conexion.Open();
            conectado = true;
        }
        catch (SqlException ex)
        {
            errorconexion = ex.Message;
        }
        return conectado;
    }
 3
Author: Dev. Joel, 2016-10-24 18:20:34

I recommend you use a SqlConnectionStringBuilder:

SqlConnectionStringBuilder builder =
        new SqlConnectionStringBuilder(connectString);

    // Supply the additional values.
    builder.DataSource = dataSource;
    builder.UserID = userName;
    builder.Password = userPassword;
    Console.WriteLine(builder.ConnectionString);

Use the string I give you with the correct data because you have a formatting error, I think it's in User id.

You can view the documentation you need on the developer network site.

 2
Author: Awes0meM4n, 2016-10-24 18:19:26