Saizaku Saizaku - 1 month ago 13
C# Question

ASP.NET WebAPI error:An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code

An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code

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: 25 - Connection string is not valid)

I get this error whenever i call any of the functions i've written int the controller.

The Controller(WEB API 2 Controller Empty):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Test.DBA;
using Test.Models;

namespace Test.Controllers
{
public class UserAPIController : ApiController
{
ApiDbContext dbContext = null;
public UserAPIController()
{
dbContext = new ApiDbContext();
}
[HttpPost]
public IHttpActionResult InsertUser(User user)
{
dbContext.Users.Add(user);
dbContext.SaveChangesAsync();

return Ok(user.Id);
}

public IEnumerable<User> GetAllUser()
{
var list = dbContext.Users.ToList();
return list;
}

[HttpPost]
public IHttpActionResult DeleteUser(User user)
{
dbContext.Users.Remove(user);
dbContext.SaveChangesAsync();

return Ok(user.Id);
}

[HttpGet]
public IHttpActionResult ViewUser(int id)
{
var student = dbContext.Users.Find(id);
return Ok(student);
}

[HttpGet]
public IHttpActionResult UpdateUser(User user)
{
var std = dbContext.Users.Find(user.Id);

std.Name = user.Name;
//std.startTime = user.startTime;
//std.endTime = user.endTime;
//std.Gender = user.Gender;
//std.Adress = user.Adress;

dbContext.Entry(std).State = System.Data.Entity.EntityState.Modified;
dbContext.SaveChangesAsync();

return Ok();
}
}
}


The Models:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Test.Models
{
public class User
{
public int Id { get; set; }
public string Name { get; set; }
//public DateTime startTime { get; set; }
//public DateTime endTime { get; set; }
//public string Gender { get; set; }
//public string Adress { get; set; }
}
}


EDIT:Connection string code added

<connectionStrings>
<add name ="Connection" connectionString="Data Source=\.SQLEXPRESS;Initial Catalog=PonesiLoptu;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>*


EDIT 2: ApiDbContex Code added.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using Test.Models;

namespace Test.DBA
{
public class ApiDbContext :DbContext
{
public ApiDbContext() : base("Connection")
{

}
public DbSet<User> Users { get; set; }
}
}

Answer

There is a small type, the hostname (. this case) should be before the \ seperator, change:

<connectionStrings>
   <add name ="Connection" connectionString="Data Source=\.SQLEXPRESS;Initial Catalog=PonesiLoptu;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

to:

<connectionStrings>
   <add name ="Connection" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=PonesiLoptu;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
Comments