System.NullReferenceException: "Object reference not set to an instance of an object." clientInfo.bank_account_id было null
у меня проект на ASP Net, немогу реализовать нормальную отправку с формы на сайте в бд информацию. По одному из видео смотрел изночально проект работает у самого кодера, но стоило мне сделать БД под себя и изменить код то начало быть все плохо. Я ввожу в поле bank_account_id значение (были цифры, даже буквы пытался буквы) код выдает мне clietnInfo.bank_account_id было NULL, в бд стоит тип text, и даже что он может быть NULL. Может кто-то поймет почему так?
Код Create.cshtml.cs:
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Data.SqlClient;
namespace ClientsBank.Pages.Clients
{
public class CreateModel : PageModel
{
public ClientInfo clientInfo = new ClientInfo();
public String errorMessage = "";
public String successMessage = "";
public void OnGet()
{
}
public void OnPost()
{
clientInfo.bank_account_id = Request.Form["bank_account_id"];
clientInfo.account_money = Request.Form["account_money"];
clientInfo.name = Request.Form["name"];
clientInfo.email = Request.Form["email"];
clientInfo.phone = Request.Form["phone"];
clientInfo.address = Request.Form["address"];
if (clientInfo.bank_account_id.Length == 0 || clientInfo.account_money.Length == 0 ||
clientInfo.name.Length == 0 || clientInfo.email.Length == 0 ||
clientInfo.phone.Length == 0 || clientInfo.address.Length == 0)
{
errorMessage = "All the fields are required";
return;
}
//save new clien into database
try
{
String connectionString = "Data Source=.\\sqlexpress;Initial Catalog=mydb;Integrated Security=True;Encrypt=False";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
String sql = "INSERT INTO clients (bank_account_id,account_money,name,email,phone,address) VALUES ('@bank_account_id','@account_money','@name','@email','@phone','@address');";
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.Parameters.AddWithValue("@bank_account_id", clientInfo.bank_account_id);
command.Parameters.AddWithValue("@account_money", clientInfo.account_money);
command.Parameters.AddWithValue("@name", clientInfo.name);
command.Parameters.AddWithValue("@email", clientInfo.email);
command.Parameters.AddWithValue("@phone", clientInfo.phone);
command.Parameters.AddWithValue("@address", clientInfo.address);
command.ExecuteNonQuery();
}
connection.Close();
}
}
catch (Exception ex)
{
errorMessage = ex.Message;
return;
}
clientInfo.bank_account_id = ""; clientInfo.account_money = ""; clientInfo.name = ""; clientInfo.email = ""; clientInfo.phone = ""; clientInfo.address = "";
successMessage = "New Client Added SuccessFully";
Response.Redirect("/Clients/Index");
}
}
}
Код Create.cshtml:
@page
@model ClientsBank.Pages.Clients.CreateModel
@{
}
<br />
<h2>New Clients</h2>
@if (Model.errorMessage.Length > 0)
{
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<strong>@Model.errorMessage</strong>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
}
<form method="POST">
<div class="row mb-3">
<label class="col-sm-3 col-form-label">Bank Account</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="name" value="@Model.clientInfo.bank_account_id" />
</div>
</div>
<div class="row mb-3">
<label class="col-sm-3 col-form-label">Money</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="name" value="@Model.clientInfo.account_money" />
</div>
</div>
<div class="row mb-3">
<label class="col-sm-3 col-form-label">Name</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="name" value="@Model.clientInfo.name" />
</div>
</div>
<div class="row mb-3">
<label class="col-sm-3 col-form-label">Email</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="email" value="@Model.clientInfo.email" />
</div>
</div>
<div class="row mb-3">
<label class="col-sm-3 col-form-label">Contact</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="phone" value="@Model.clientInfo.phone" />
</div>
</div>
<div class="row mb-3">
<label class="col-sm-3 col-form-label">Address</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="address" value="@Model.clientInfo.address" />
</div>
</div>
@if (Model.successMessage.Length > 0)
{
<div class="alert alert-success alert-dismissible fade show" role="alert">
<strong>@Model.successMessage</strong>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
</button>
</div>
}
<div class="row mb-3">
<div class="col-sm-6">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
Visual Studio 2022 останавливает на:
if (clientInfo.bank_account_id.Length == 0 || clientInfo.account_money.Length == 0 ||
clientInfo.name.Length == 0 || clientInfo.email.Length == 0 ||
clientInfo.phone.Length == 0 || clientInfo.address.Length == 0)
{
errorMessage = "All the fields are required";
return;
}
Используется .NET 6.0, стоит из NuGet System.Data.SqlClient