0

I create a new project and create a simple project working with CodeFirstApproach

First Create a student class and then create a context class(communicate between database and table) and write a below code in web.config

Sql Server authentication

username: sa
password: s123

web.config

<!--CodeFirstAproach-->
<connectionStrings>  //something is wrong here
    <add name="conn" connectionString="Server=.\JSdique;database=codefirst;User Id=sa;Password=s123" providerName="System.Data.SqlClient" />  
  </connectionStrings>

and then create a Home Controller and then Create a view

ContextClass 

   public class ContextClass : DbContext 
    {
        public ContextClass() : base("conn") { }

        public DbSet<Student> students { get; set; }
    }

HomeController 
public class HomeController : Controller
    {
        ContextClass db = new ContextClass();
        // GET: Home
        public ActionResult Index()
        {
            var data = db.students.ToList();
            return View(data);
        }
    }

Error:

Additional information: 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)

How to solve this error?

I Refer this Link:https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql/authentication-in-sql-server

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Did you check the things the error mentions? 1) Verify that the instance name is correct. 2) Verify that SQL Server is configured to allow remote connections . In other words, make sure you've got the right server name, and/or the right SQL instance name, and make sure that SQL Server is configured correctly. The problem is not related to authentication - the issue occurs when trying to connect to the database, before any authentication takes place. – ADyson Sep 27 '19 at 09:52
  • N.B. a side note: it's very bad security practice to let your applications log into SQL as `sa` (i.e. the system administrator). Create a separate user login which only has the permissions the application actually needs in order to work. Don't even use the `sa` account as a shortcut during development or testing, because you need to test your account permissions as well - otherwise when you go live you might have unexpected errors relating to the user account setup. – ADyson Sep 27 '19 at 09:53
  • Possible duplicate of [Why am I getting "Cannot Connect to Server - A network-related or instance-specific error"?](https://stackoverflow.com/questions/18060667/why-am-i-getting-cannot-connect-to-server-a-network-related-or-instance-speci) – ADyson Sep 27 '19 at 09:54
  • the server (.\JSdique) in your connection-string looks wrongish! – Sancho Panza Sep 27 '19 at 09:55
  • @ADyson I made some changes in web.config and then give an error:Additional information: Keyword not supported: 'userid'. ``` ``` – jishan nuran Sep 27 '19 at 09:59
  • Why did you decide to change that? There was nothing wrong with the User Id definition. https://www.connectionstrings.com/sql-server/ has examples of valid syntax. Like I said, the error is not related to the username and password. – ADyson Sep 27 '19 at 10:03
  • @ADyson I close ssms and again SQL server authentication and username:sa and password:s123please help? – jishan nuran Sep 27 '19 at 10:08
  • Use SQL Server Management Studio and verify the server/instance matches the connection string. Then see if the credentials are set to Windows. If you have a window credential then remove the user and password from the connection string. Then add Integrated Security=true; (which indicates you are using a Windows credential). – jdweng Sep 27 '19 at 10:08
  • @ADyson Server=myServerName\myInstanceName what is myinstance name?? I thing issue is here "Server=JSdique\ what is myinstance name – jishan nuran Sep 27 '19 at 10:09
  • How should I know? I can't see your server. Instances are optional though, if you just have one instance on the server you may be able to refer to it just by the server name. It depends how you set it up when you installed SQL Server. The correct syntax may also depend what Edition of SQL Server you are using. Like jdweng says you can use SSMS to help you see. And also that connectionstrings site has examples of the syntax for different situations. If you are working at a company and they have a DBA, then you could ask them to help you understand the setup. – ADyson Sep 27 '19 at 10:22
  • 1
    @ADyson I m not working for any company. I just start to learn MVC at my home When I feel difficulty then research on google and then I ask a question on StackOverflow. ok I will check my connection string again – jishan nuran Sep 27 '19 at 10:37

0 Answers0