I am using VS 2019, SQL Server 2016, I try to use ADO.NET to update a registry, but I am getting an error:
The ConnectionString property has not been initialized
The controller:
If I use this one:
con = new SqlConnection(ConfigurationManager.ConnectionStrings["DevConnection"].ConnectionString);
I get the error:
Object reference not set to an instance of an object.'
If I use:
con = new SqlConnection(appSettings.Value.DbConnection);
I get:
The ConnectionString property has not been initialized.
The rest of the code:
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UserId", Convert.ToInt32(user.UserId));
cmd.Parameters.AddWithValue("@DateOfBirth", user.DateOfBirth);
cmd.Parameters.AddWithValue("@UserNames", user.UserNames);
cmd.Parameters.AddWithValue("@UserLastNames", user.UserLastNames);
cmd.Parameters.AddWithValue("@UserPlatformName", user.UserPlatformName);
cmd.Parameters.AddWithValue("@UserPassword", user.UserPassword);
cmd.Parameters.AddWithValue("@UserGender", user.UserGender);
cmd.Parameters.AddWithValue("@UserDocumentNumber", Convert.ToInt32(user.UserDocumentNumber));
con.Open();
result = cmd.ExecuteScalar().ToString();
message.Data = result.ToString();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
message.Data = e.Message;
}
finally
{
con.Close();
}
Class to get the connection:
public class MySettings
{
public string DbConnection { get; set; }
}
On Startup:
services.Configure<MySettings>(Configuration.GetSection("ConnectionStrings"));
The procedure:
ALTER PROCEDURE [dbo].[SPUpdateUser]
(@UserId INTEGER,
@DateOfBirth DATE,
@UserNames NVARCHAR(30),
@UserLastNames NVARCHAR(30),
@UserPlatformName VARCHAR(30),
@UserPassword NVARCHAR(15),
@UserGender NVARCHAR(9),
@UserDocumentNumber INTEGER)
AS
BEGIN
SET NOCOUNT ON;
UPDATE Users
SET DateOfBirth = @DateOfBirth,
UserNames = @UserNames,
UserLastNames = @UserLastNames,
UserPlatformName = @UserPlatformName,
UserPassword = @UserPassword,
UserGender = @UserGender,
UserDocumentNumber = @UserDocumentNumber
WHERE
UserId = @UserId
END
In Postman, I am sending:
{
"userId": 1,
"dateOfBirth": "1980-1-1",
"userNames": "James",
"userLastNames": "Rodriguez",
"userPlatformName": "1",
"userPassword": "1234",
"userGender": "Masculino",
"userDocumentNumber": 1,
"relatives": []
}
The two integers have their casts, so... why do I get this error?
And how can I fix this?
The project compiles, I see no errors in VS
The appsettings.json
"ConnectionStrings": {
"DevConnection": "Data Source=DESKTOP-ARVMMP2\\SQLEXPRESS;Initial Catalog=PraxedesDb;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
},
CodePudding user response:
ConfigurationManager
does not work with appsettings.json
in ASP.NET Core, you can use IConfiguration
instead of ConfigurationManager
ike below:
public class HomeController : Controller
{
private readonly IConfiguration Configuration;
public HomeController(IConfiguration Configuration)
{
this.Configuration = Configuration;
}
public async Task<IActionResult> Index()
{
var con = new SqlConnection(Configuration.GetConnectionString("DevConnection"));
return View();
}
}