Click here to Skip to main content
15,914,608 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am working on web application where i have to change data source dynamically in entity framework .
error i faced is
The network path was not found


What I have tried:

SqlConnection con = null;
            var dbToUse = objser.Getip((ddcmpname.SelectedValue).ToString()).ToList();

            con = new SqlConnection(string.Format("server=.;database={0};", dbToUse));
 
            string yourConnection = ConfigurationManager.ConnectionStrings["Entities"].ConnectionString.Replace("dbname", dbToUse[0].Location);
            con = new SqlConnection(yourConnection);
con.open();
Posted
Updated 11-Dec-18 0:53am
Comments
F-ES Sitecore 11-Dec-18 6:41am    
The connection string you are creating is probably referencing a machine that can't be found. We don't know what your connection string is, and we can't access your network to tell you why that connection string is invalid.

1 solution

I don't know whether I follow you here or not, but,
// Why this? 
con = new SqlConnection(string.Format("server=.;database={0};", dbToUse));
 
string yourConnection = ConfigurationManager.ConnectionStrings["Entities"].ConnectionString.Replace("dbname", dbToUse[0].Location);

// When you have to do this! 
con = new SqlConnection(yourConnection);
Change your code to this,
SqlConnection con = null;
var dbToUse = objser.Getip((ddcmpname.SelectedValue).ToString()).ToList();
 
string yourConnection = ConfigurationManager.ConnectionStrings["Entities"].ConnectionString.Replace("dbname", dbToUse[0].Location); // Assuming dbToUser[0] has a Location property
con = new SqlConnection(yourConnection);
con.open();
The reason I remove the previous statement was, that it does not make any difference. You are not opening the connection for that object type, you are however making a connection here. With this code, you just need to make sure your path is correct.

If the path is incorrect, we are unable to help because we do not know your networks settings and configurations. In this case, SQL Server connection strings - ConnectionStrings.com[^], would be able to help you out.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900