locked
Error ConnectionString Informix MVC ASP.NET RRS feed

  • Question

  • User602874954 posted
    Hi.
    
    I am developing a web application from the basics with C # and mvc.
    
    I want to connect my application with an Informix database.
    
    I create a class that will handle the connection.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Configuration;
    using IBM.Data.Informix;
    using ClienteConexxion.Models;
    
    namespace ClienteConexxion.Models
    {
        public class Conexion
        {
            public IfxConnection AbreConexion()
            {
                IfxConnection conexion = new IfxConnection();
                var cadenaConexion = ConfigurationManager.ConnectionStrings["001"].ConnectionString;
                
             
                try
                    {
                        conexion = new IfxConnection(cadenaConexion);
                        conexion.Open();
                    }
                    catch (Exception)
                    {
                        conexion.Close();
                    }
         
                return conexion;
            }
        }
    }
    When I perform the unit test it shows the following error: Result Message:    
    Test method ClienteConexxion.Models.Tests.ConexionTests.AbreConexionTest threw exception:
    System.NullReferenceException: Referencia a objeto no establecida como instancia de un objeto.

    <connectionStrings>
        <add name="001" providerName="IBM.Data.Informix" connectionString="Database=ejem;Host=192.168.2.1;Server=base_s;Service=1526;Protocol=onsoctcp;UID=root;Password=root"/>
    
    Can not find the web.config.
    
    Since when I put the chain directly if it works perfectly.
    
    I do not know where the fault is in my code.
    
    Thank you for your help.
    Monday, January 9, 2017 8:39 PM

All replies

  • User753101303 posted

    Hi,

    It can't use the web.config. You could perhaps use an app.config file. What do you want to test? Here :

    - you create a connection object and create then it again with a connection string (not  a real problem but useless)
    - you'll really just close the connection and return it even if an exception happens ?

    Monday, January 9, 2017 9:33 PM
  • User602874954 posted

    What I try to do is a class that I will instantiate every time I use it.

    Maybe it's not the best way I do it. I'm a newbie.

    I understand that I should use web.config for the MVC in asp.

    How could my class handle an example.

    Here my unity test.

    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using ClienteConexxion.Models;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using IBM.Data.Informix;
    
    namespace ClienteConexxion.Models.Tests
    {
        [TestClass()]
        public class ConexionTests
        {
            [TestMethod()]
            public void AbreConexionTest()
            {
                var accion = new Conexion();
    
                var conexion = accion.AbreConexion();
                var estado = conexion.State.ToString();
                conexion.Close();
    
                Assert.AreEqual("Open", estado);
                //Assert.Fail();
            }
        }
    }

     Change my code and stay that way.

    Error

    Always showing error, can not find the connection string.

    As I repeat if I put the connection string directly if it works

    Monday, January 9, 2017 9:41 PM
  • User-271186128 posted

    Hi jmanu,

    When you run ASP.NET unit tests, it is a separate project, so you could access the web.config in the Unit test project, instead of the MVC project.

    Please check the web.config file in the unit test project and make sure it contains the connection string.

    Since this issue is related to Unit Test, if you have any further questions about unit test, I suggest you could post your problem to unit test forum:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vsunittest

    Best regards,
    Dillion

    Tuesday, January 10, 2017 8:12 AM
  • User602874954 posted

    Hi Zhi Lv thanks for the help but I consider that it is not the test but the main project. It's like I could not read the web.config

    The test project already has the web.config.

    I do not know what else to do since the mission is to create a class for connection and call it whenever necessary

    Tuesday, January 10, 2017 3:40 PM