Error when connecting to .mdf from ASP.NET (C#) - c#

When trying to connect to a .mdf databse in ASP.NET (using c#) I am given this error:
A network-related or instance-specific
error occurred while establishing a
connection to SQL Server.The server
was not found or was not accessible.
Verify that the instance name is
correct and that SQL Server is
configured to allow remote
connections. (provider: SQL Network
Interfaces, error: 26 - Error Locating
Server/Instance Specified)
Does this mean my connection string is wrong? What should it be?
Aside from that, I am very new to ASP.NET. I am trying to connect to this database in the same way I would in c# normally (using data adapters and SqlCommands). Is this the right way to go about it, or is there a different way? When I started a new website adding user accounts worked (there was a wizard or something?) but I couldn't work out how to add more user information. What is the best way to connect to an SQL database and add user accounts with login and personal details in ASP.NET?
EDIT: My Connection String is data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\Users.mdf;User Instance=true

If I read your connection string properly, you're never specifying a database to use!
data source=.\SQLEXPRESS;Integrated Security=SSPI;
AttachDBFilename=|DataDirectory|\Users.mdf;User Instance=true
Add the database to the connection string - and also, loose the extra backslash after the |DataDirectory|:
server=.\SQLEXPRESS;database=YourDatabase;Integrated Security=SSPI;
AttachDBFilename=|DataDirectory|Users.mdf;User Instance=true

OK, I've fixed it. For reference for anyone else:
The correct connection string was given in the Web.config file.
The connection string I needed was Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Users.mdf;Integrated Security=True;User Instance=True

Related

Connection Error in SQL Server Compact with ASP.Net

I am trying to access a database, using a DataSet. I am using Visual Studio for Web Express 2013, ASP Web Forms project with SQL Server Compact 4.0. I created the database from Database Explorer, added 3 tables, now my code has getData, setData methods which will use Dataset to get data.
But the SqlConnection object is throwing exception at runtime.
[SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error:
26 - Error Locating Server/Instance Specified)]
I guess this is due to connection string since error is at line:
SLQCon.Open();
I have tried more than one method but none of them has worked yet. These are few of them,
msdn link
Connection Strings
The other things work fine (web form controls), only problem is when I do something related to database, any fixes?
My code is as follows:
void setData(string tableName) {
string ConString = #"Data Source=" + "F:\\Github\\JournalClassifier\\WebApplication1\\WebApplication1\\App_Data\\KeywordsDB.sdf" + ";Connect Timeout=30";
SQLCon.ConnectionString = ConString;
SQLCon.Open(); // Exception here
// insertion code
}
Make sure your database inside App_Data folder on your root application. and change the connection string to this one
<add name="ConnectionStringName"
providerName="System.Data.SqlServerCe.4.0"
connectionString="Data Source=\KeywordsDB.sdf;Connection Timeout=30" />
things you need to concern is you are using SQL Server Compact, so the provider name need use System.Data.SqlServerCe.4.0 otherwise it will confused with SQL Server Client

Azure Cloud Service unable to connect to SQL Database on write operations only

I have developed a web application in lightswitch. There is an HTML lightswitch client that connects to a server (C#) and this server gets the data from an SQL Database.
The application is working correctly in my local machine, the problem comes when I publish the solution to a Cloud Service in Azure. Then, the server connects to the database correctly and can read all the information from it, but when I try to update the database from within my application (insert or update rows) the application freezes and it promts the following error:
Unable to connect to SQL Server database.
Inner exception message:
Unable to connect to SQL Server database.
Inner exception message:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
I have checked the connection parameters and all of them are correct, this is the connection string in my web.config:
<add name="My_Data" connectionString="data source=tcp:{MyServer}.database.windows.net,1433;initial catalog={myDatabase};user id={Myuser}#{MyServer};password={MyPassword};Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" providerName="System.Data.SqlClient" />
I have seen other questions with the same issue, but all of them were because the database or the server was not available. I have no problem connecting to the server because I can query my database with no problems.
The error comes when I try to write in my database, either because I click on the save button of a lightswitch screen or because I do it by code in the server:
serverContext.DataWorkspace.My_Data.SaveChanges();
If I comment the previous line, the error is not prompted.
I have also checked the user permissions in the database and this usser is a db_owner, so there should be no problem with that.
Anyone has ever faced a problem like this?
I found the solution!
After a deep research, I discovered that Azure server is adding a connection string to my application (I don't know how nor why). This connection string is inherited from somewhere and it is pointing to a local instance of SQL Express. Obviously, this instance does not exist, so after a timeout the application throws the error shown above.
I don't really know why this is happening nor why only happens when I try to write in my database. The solution I found is to add the following in the connection strings part of my web.config:
<clear />
With this, we delete the inherited connection string. And now, we have to set the connection string of the LocalSqlServer (which will be our connection string, the one that points to our remote SQL Server in Azure):
<add name="LocalSqlServer" connectionString="Data Source={SERVER}.database.windows.net;Initial Catalog={DATABASE};Integrated Security=False;User ID={USER}#{SERVER};Password={PASSWORD};Encrypt=True" />
Now, everything works fine. I hope this can help anyone who faces the same problem than me and I would really like to know why this happened, so if anyone knows it, please tell me :)

Connection string to SQL by EntityFramework and MVC

I write web page in MVC, which use Entity Framework as ORM mapper. I put my connection string to web.config file. Here is it:
<add name="JP_CMS" connectionString="Data Source=.;Initial Catalog=JP_CMS;
User Id=CmsWebUser;Password=abcd1234abcd1234;"
providerName="System.Data.SqlClient"/>
I try to get data by this C# code
public DataBaseContext()
: base("JP_CMS")
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DataBaseContext>());
}
//------------------------------
var db = new DataBaseContext();
var result = db.Articles.ToList(); //<- here is thrown exception
//------------------------------
My local SQL server is running, this table is exists, but I get excepion like in this:
I think it is the most important expection: "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"
Could you help my by give some idea what I make wrong?
"The system cannot find the file specified" error. Check carefully your connection string.
hi Error indicates EF tried to connect to the server but failed.
Could you Checkk,
1) SQL Server should be up and running.
Go to All Programs >> Microsoft SQL Server 2008 >> Configuration Tools >> SQL Server Configuration Manager >> SQL Server Services, and check if SQL Server service status is “Running”.
In addition, ensure that your remote server is in the same network. Run “sqlcmd -L” in your command prompt to ascertain if your server is included in your network list.
2) Enable TCP/IP in SQL Server Configuration
For more info you can go here
Instead of DataSource =. try DataSource = .\sqlInstanceName. Also DataSource=(local) might work.

error: 26 - Error Locating Server/Instance Specified. (Can't connect to my local Db From my host server)

I already asked this question before, but without any good response,
my problem is that I have a local database (aspnetdb.mdf) in my app_data folder, here is the connection string in the web.config :
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
so my site works fine in my local iis server, but when i put the site in my host server, I get this error message :
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified).
In the other server I have sql server 2008 (the remote connection is accepted and i have the same instance name SQLEXPRESS). I really tried everything that I can but without any good results. I will really appreciate your help, thx.
This format indicates a local connection:
.\SQLEXPRESS
From another server, you'll need to specify a server name or IP address, e.g.:
192.168.1.205\SQLEXPRESS
YOURMACHINE\SQLEXPRESS
You'll also need to validate that the instance is SQL Server Express. From your other nearly identical question it looks like a default instance.
You should also stop using this silly user instance / attachDbFilename technique. Attach your database to the instance properly, then reference the logical database name using Initial Catalog=aspnetdb; in your connection string. This user instance feature was deprecated for multiple reasons, and this is certainly one of them.
Of course there may be other network issues that will prevent this, e.g. if the server is in a different domain, there are firewall settings interfering, etc. But your other server is never going to be able to access your machine using a dot for the server name.
EDIT providing baby steps because obviously my advice is not being followed. Assuming you have actually attached the aspnetdb database on the new server, try this please:
connectionString="data source=192.168.85.124\SQLEXPRESS;
Integrated Security=SSPI;
Initial Catalog=aspnetdb;"
If you aren't using SQL Express named instance after all, try:
connectionString="data source=192.168.85.124;
Integrated Security=SSPI;
Initial Catalog=aspnetdb;"
For SQL authentication use the same username/password you use in Management Studio:
connectionString="data source=192.168.85.124;
User ID=username; Password=password;
Initial Catalog=aspnetdb;"
This is troubleshooting 101 stuff. If you still can't get this to work, you may have to hire a consultant for 10 minutes to fix it for you since it's impossible to figure out what you've done and why you can't get it to work.

Error connecting SQL server to MVC3 application - A network-related or instance-specific error

I'm getting this error since I've moved my mvc3/entity framework site onto the live server from localhost. Normally when I would get this error, I'd check the database name, password and server are all correct in the connection string. I have checked this - they all seem fine.
I have aspnet Membership provider on the site within the database, and it allows me to login, verifies me, then tries to redirect me to another page, and then that's where the error happens - i.e., as soon as I connect to the database outwith the membership provider.
The database is on the same server as the site, and when I connect from localhost to the remote server, it works perfectly.
Here is the full error message:
Exception message: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
Here is the connection string:
<add name="ApplicationServices" connectionString="Data Source=192.168.1.43;Initial Catalog=BlueLadder;User Id=BlueLadderAdmin;Password=Auth1991;timeout=30" />
Anyone anything I could try?
Ok, despite my connection string in the web config, it was actually being ignored and the application was still trying to connect to a local version. Apparently you need to pass the connection string through the dbContext constructor, like so.
public Context()
: base("ConnectionString")
{
}
Question was answered here
Thanks for your help anyway.
Try removing the Data Source and Initial Catalog, and replace them with something like this:
Server=.\SQLEXPRESS;Database=BlueLadder;
I don't know if you're using SQLEXPRESS, so just modify accordingly if you're not.
Check if your server is accepting remote connections and has TCP/IP provider enabled. You can configure this using "Sql Server Configuration Manager" on your server
You could try specifying the named instance of your server. "Sql Server Configuration Manager" can tell you what instances are installed on your server.
Server = 192.168.1.43/MSSQLSERVER or
Server = 192.168.1.43/SQLEXPRESS

Categories

Resources