I recently uploaded my data driven ASP.NET application to a asp hosting service called SmarterAsp.com.
Everything in the website works fine, all but 1 feature, data can't be entered into the database.
However when using a gridview control to see data in the database the data is displayed perfectly and I can even insert, update and delete data through the gridview.
I uploaded my database in a separate space on the hosting service and I was provided with a connection string, I replaced all connection strings in my application with the provided connection string.
It works fine in my IDE, the problem only came once I uploaded the website to the hosting service.
Here is my C# code for inserting data into my database:
protected void Button1_Click(object sender, EventArgs e)
{
using (SqlConnection connection = new SqlConnection(#DataSource=SQL5004.Smarterasp.net;Initial Catalog=DB_9DE115_hmkdatabase;User Id=DB_9DE115_hmkdatabase_admin;Password=YOUR_DB_PASSWORD;"))
{
SqlCommand cmd = new SqlCommand(#"INSERT INTO Clients(ClientName)VALUES(#ClientName)");
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
cmd.Parameters.AddWithValue("#ClientName", TextBox1.Text.ToString());
connection.Open();
cmd.ExecuteNonQuery();
}
}
And here is the error I get when attempting to insert the data:
Server Error in '/' Application.
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)
Description:
An unhandled exception occurred during the execution of the current
web request. Please review the stack trace for more information about
the error and where it originated in the code.
Exception Details:
System.Data.SqlClient.SqlException: 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)
My first thought was that it's a problem with the connection string, but the connection string works fine with the rest of the application. I can enter data into my database through the gridview add function but I cannot enter data using the C# codebehind page. As I said it works fine in my IDE, the problem only came once I uploaded the website to the hosting service.
Thanks in advance for the help guys. You guys are real life heroes!
I sorted out the problem. My problem was where I initialized the connection string and where I opened the connection. I initialized the connection string at the top where the page is initialized. And then replaced where the connection string was with connection.open();
Thanks for all the help guys!
Related
This question already has answers here:
Cannot Connect to Server - A network-related or instance-specific error
(54 answers)
Closed 3 years ago.
I tried making a site for a school project and when I go ahead and input values in the labels in the register page and send it starts loading for some time and then opens up the code in the server side:
SqlConnection conn = ConnectToDb(fileName);
in this line -----> conn.Open();
and this error msg appears:
System.Data.SqlClient.SqlException: '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)'
You should check your connection string and that you have enough permissions to connect to that database
This is likely because (1) you have the wrong connection string (Example: Wrong server name), or (2) you're loading the connection string from a missing/invalid configuration file. For example, if you're running in debug mode and you your application is trying to load *.debug.config file.
There are other reasons, like you are using NT Authentication and the identity your application is running under does not have SQL Server access.
I've added to my .asp website a CreateUserWizard with destination page url home. After I'm launching it in Visual Studio 2013, it opens, I register, but, after a long delay after pressing sign up, it shows me 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)
Should I create a database myself or how it can't locate the server?
I think that connection string for database is not correct. Try creating an empty database and add that connection string in web config.
The error is telling you it can't find the database you have specified in your connection string (perhaps a typo or an incorrect details)
this page will help you create the connection string. You will just need your server details
I currently have a problem where I can't connect a database on my server but it works fine when running locally. I am using this connection string in the controller:
#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='|DataDirectory|\Test.mdf';";
Controller code:
namespace PathTest.Controllers
{
public class HomeController : Controller
{
string connectionString = #"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='|DataDirectory|\Test.mdf';";
public ActionResult Index()
{
DataTable daTbl = new DataTable();
using(SqlConnection sqlCon = new SqlConnection(connectionString))
{
sqlCon.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter("SELECT * FROM Numbers", sqlCon);
sqlDa.Fill(daTbl);
}
return View(daTbl);
}
}
}
Error when running on server:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: 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: 50 - Local Database Runtime error occurred. Specified LocalDB instance name is invalid.)
Any help would be appreciated
First of all, I believe that you don't want to run the localdb on production server. You should deploy, if not did yet, a SQL Server instance on your server, so the connection string should be:
"Server=[Server IP/Name];Database=MSSQLLocalDB;User Id=[Some User Created];Password=[Password of User Created];"
If I'm wrong, and you want the localdb on server, you need to check if the SQL Server Express Service is running.
If so, check if the database file exists, if not, you can create an local database for express instance with the tool sqllocaldb.
And for the last, you can keep the two connection strings, using the Configuration Manager by the .config files (you can use the local db on Debug profile and the server db on Release profile).
I hope it helps you!
I am trying to make a registration page, that stores Username, password,and email,
but
When ever i try to Connect database using this string,
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RConnectionString"].ConnectionString);
conn.Open();
Following Error appears:
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 searched everywhere, but it didn't helped me..
The issue starts from web.config and the connection string that is located at RConnectionString according to the code line you give.
Locate this string and fix it to been able to connect to your database.
And some examples here: Setting up connection string in ASP.NET to SQL SERVER
So Basically i have a POS system with its own database and my website which is hosted on a shared 1and1.com hosting company server which also contains a database with a database design needed.
My aim is to only upload those products that are active- true.
My solution to update the website database from the POS database is to have a special admin page on the website that a connection string of the POS database is entered. The admin.aspx.cs calls a stored procedure that gets all the specific data from the POS database and insert them into the website database.
This all works perfectly when testing on a colleagues machine acting as the website+ db.
But when it comes to loading on the 1and1.com host it returns an error 26.
SqlException was unhandled 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)
The connection string is the following
Server=IP_ADDRESS_OF_POS\SQLEXPRESS;Database=CentricDatabase;User ID=centric;Password=XXXXXXXXX
Does anyone have an idea why this isn't working on the host? but works on a colleagues machine?
Thanks