5

I am new to ASP.NET MVC, I am facing this exception, the connection string looks perfect but still, the exception is raised, appreciate if anyone give me why is happening.

Thank you guys

Model 1

namespace MVCTwice.Models
{
    public class StudentContext : DbContext
    {
        public DbSet<Student> studs { get; set; }
    }
}

Model 2

namespace MVCTwice.Models
{
    [Table("tblStudents")]
    public class Student
    {
        public int id { get; set; }
        public string name { get; set; }
        public string gender { get; set; }
        public string totalMarks { get; set; }
    }
}

Action method

public ActionResult Index()
        {
            StudentContext studentContext = new StudentContext();
            //Student emp = studentContext.studs.Select(emp=>emp.)
           List<Student> emp=studentContext.studs.ToList();

            return View(emp);
        }

View

@model MVCTwice.Models.Student
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div> 
        @Model.gender
        @Model.name
        @Model.id
        @Model.totalMarks

    </div>
</body>
</html>

Exception

enter image description here

ConnectionString

 <connectionStrings >
    <add
         name="myConnectionString"
         connectionString="Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=LoginInfo;Data Source=.\SQLEXPRESS"
         providerName="System.Data.SqlClient"/>
  </connectionStrings>
5
  • 1
    Can you login using those credentials from sql mgmnt studio? I would start there. Commented Nov 6, 2016 at 1:54
  • yes I can, login to sql mgmnt studio Commented Nov 6, 2016 at 3:12
  • 2
    You are not providing a password in the connection string Commented Nov 6, 2016 at 3:25
  • after providing the password exception remains the same Commented Nov 7, 2016 at 0:30
  • 2
    Show your inner exception and stack trace for additional details. Usually inner exception explains what should you do to solve the connection issue. Commented Nov 7, 2016 at 1:19

3 Answers 3

5
<connectionStrings>
  <add name ="StudentContext " 
        connectionString ="server=.; database=here your database name; integrated security=SSPI"
           providerName ="system.data.SqlClient"/>
</connectionStrings>

Here is your code but change your database name and then add it into the web.config file.

Sign up to request clarification or add additional context in comments.

Comments

0

go to iis -> application pools -> find your application pool used in application -> click it and then click 'Advance Settings' in Actions panel. Find 'Identity' property and change it to localsystem.

please run the project if it runs successfully then good if not may be your solution is in below links.

After fixing the above step Some of you will experience another error saying "SQL Server Login error: Login failed for user 'NT AUTHORITY\SYSTEM'"

So for this please follow below links: SQL Server Login error: Login failed for user 'NT AUTHORITY\SYSTEM'

https://dba.stackexchange.com/questions/47172/can-not-find-nt-authority-network-service-on-sql-server-2012

Comments

0

Here is the answer of this exception ---> Change your name of the connection string, The name must be same like your context class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.