none
Can not connect to database after deploy the windows forms application RRS feed

  • Question

  • Hi, I have created a windows forms application with Entity Frame Work 6 for working with database , every thing is ok in development environment but when i tried to deploy the application and install it on the client device i couldn't connect to database getting exception message said " No connection string named 'salesEntities' could be found in the application config file. " , the connection string exist in the App.config file , so what is the problem exactly and how can i solve it ??

    i googled a lot but find nothing considering my issue.

    thanks in advance...

    Thursday, September 25, 2014 9:40 PM

Answers

  • Hello Rashiya,

    >>but when i tried to deploy the application and install it on the client device i couldn't connect to database getting exception message

    Would you mind describing how you deploy the application? With your description, I make a small demo with window form project and Entity Framework 6, it is very easy and only shows data in datagridview. I publish the application by Publish command when right clicking the project. After installing the published setup, it contains below files:

    Is this similar yours?

    >> the connection string exist in the App.config file

    This confuses me because as far as I know, the error you run into is caused by the required connection string does not match the one stored in config file. I am not sure if there is something wrong occurs during the deploying process, please check if the required connection string name in context class matches the one stored in config file after installing the application.

    And you could also try to add an additional constructor which accepts a string variable representing the connection string as below:

    public partial class DFDBEntities : DbContext
    
        {
    
            public DFDBEntities()
    
                : base("name=DFDBEntities")
    
            {
    
            }
    
    
            public DFDBEntities(string connection)//this is the additional constructor
    
                : base(connection)
    
            {
    
            }
    
        
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
    
            {
    
                throw new UnintentionalCodeFirstException();
    
            }
    
        
    
            public virtual DbSet<Order> Orders { get; set; }
    
        }
    

    Calling this constructor as:

    private void button1_Click(object sender, EventArgs e)
    
            {
    
                string connection = @"metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='data source=(localdb)\Projects;initial catalog=DFDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework';";
    
                DFDBEntities db = new DFDBEntities(connection);
    
                var result = db.Orders.ToList();
    
                this.dataGridView1.DataSource = result;
    
            }
    

    Please try above to see whether it throws error anymore.

    Best Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Rashiya Friday, September 26, 2014 2:30 PM
    Friday, September 26, 2014 6:05 AM
    Moderator

All replies

  • Hello Rashiya,

    >>but when i tried to deploy the application and install it on the client device i couldn't connect to database getting exception message

    Would you mind describing how you deploy the application? With your description, I make a small demo with window form project and Entity Framework 6, it is very easy and only shows data in datagridview. I publish the application by Publish command when right clicking the project. After installing the published setup, it contains below files:

    Is this similar yours?

    >> the connection string exist in the App.config file

    This confuses me because as far as I know, the error you run into is caused by the required connection string does not match the one stored in config file. I am not sure if there is something wrong occurs during the deploying process, please check if the required connection string name in context class matches the one stored in config file after installing the application.

    And you could also try to add an additional constructor which accepts a string variable representing the connection string as below:

    public partial class DFDBEntities : DbContext
    
        {
    
            public DFDBEntities()
    
                : base("name=DFDBEntities")
    
            {
    
            }
    
    
            public DFDBEntities(string connection)//this is the additional constructor
    
                : base(connection)
    
            {
    
            }
    
        
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
    
            {
    
                throw new UnintentionalCodeFirstException();
    
            }
    
        
    
            public virtual DbSet<Order> Orders { get; set; }
    
        }
    

    Calling this constructor as:

    private void button1_Click(object sender, EventArgs e)
    
            {
    
                string connection = @"metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='data source=(localdb)\Projects;initial catalog=DFDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework';";
    
                DFDBEntities db = new DFDBEntities(connection);
    
                var result = db.Orders.ToList();
    
                this.dataGridView1.DataSource = result;
    
            }
    

    Please try above to see whether it throws error anymore.

    Best Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Rashiya Friday, September 26, 2014 2:30 PM
    Friday, September 26, 2014 6:05 AM
    Moderator
  • Thank you Fred Bao,  adding the additional constructor and a class contain a method that retrieving the connection string solve the issue , it looks like that the application can't red the application config file.


    Friday, September 26, 2014 2:30 PM
  • Hi, I have created a windows forms application with Entity Frame Work 6 for working with database , every thing is ok in development environment but when i tried to deploy the application and install it on the client device i couldn't connect to database getting exception message said " No connection string named 'salesEntities' could be found in the application config file." , the connection string exist in the App.config file , so what is the problem exactly and how can i solve it ??

    Thank you Fred Bao,  adding the additional constructor and a class contain a method that retrieving the connection string solve the issue , it looks like that the application can't red the application config file.

    I really don't know what that's all about. But if there is an app.config in a project that creates a programname.exe file, then the app.config information is copied over to a runtime config file called programname.exe.config. It is where .NET looks for runtime configuration information for a program exeutable type at runtime, upon the successful build of the exe project type.

    The programname.exe.config is going to be setting in the bin folder at the location of the programname.exe so that .NET can find the configuration information at program runtime. That's why it works when you run the solution in non-debug mode in VS.

    Therefore when you deploy the programname.exe file, the programname.exe.config file must be deployed too, and the config file must be deployed to the same location as the programname.exe file so that .NET can find the programname.exe.config and use the configuration information in the config file.

    You should go find the programname.exe.config on your development machine and use Notepad to view the context of the config file.

    Your problem seems to be that you didn't deploy the programname.exe.config with the programname.exe, and that is why .NET couldn't find the connectionstring information in a config file at runtime when you deployed the solution.

    Go check it out....

    Friday, September 26, 2014 7:35 PM
  • Right darnold i think the problem was from the application i used to deploy wasn't deploy the config file with application.exe , so when i but the connection string in a class the problem disappeared.

    I'm using advanced installer for deploying.

     
    Saturday, September 27, 2014 12:19 AM
  • Right darnold i think the problem was from the application i used to deploy wasn't deploy the config file with application.exe , so when i but the connection string in a class the problem disappeared

    You didn't have to do that. You didn't have to make up some needless class that holds a connectionstring.

    What needs to happen is to make sure the programname.exe.config is deployed with the programname.exe, which has the connectionstring in it. What happens if there were other config information in the runtime config other than a connectionstring? .NET is not going to find it, if the runtime config has not been deployed.

    What? Are you going to make a class for the other config information just because the runtime config was not deployed?

    The key here is to understand how .NET finds config files at runtime so that you don't make needless classes to do what .NET will do if things are deployed right.

    Saturday, September 27, 2014 11:14 PM