列Firstname不能为null
本文关键字:null 不能 Firstname | 更新日期: 2023-09-27 18:21:38
it下面是我的代码。。我在平台上试过几个答案,但仍然出现了同样的错误。。有人请帮帮我!!!!提前感谢!
public bool saveRecord(string Firstname,string Lastname,string Username,string Password,string Emailadress)
{
using (MySqlConnection connection = new MySqlConnection("Server=localhost;Port=8889;Database=SSC;Uid=root;Password=root"))
{
try
{
string Sql_Query = "INSERT INTO administrator (Firstname,Lastname,Username,Password,EmailAdress)VALUES(@Firstname,@Lastname,@Username,@Password,@EmailAdress);";
connection.Open();
using (MySqlCommand command = new MySqlCommand(Sql_Query, connection))
{
command.Parameters.AddWithValue("@Firstname", Firstname);
command.Parameters.AddWithValue("@Lastname", Lastname);
command.Parameters.AddWithValue("@Username", Username);
command.Parameters.AddWithValue("@Password", Password);
command.Parameters.AddWithValue("@EmailAdress", Emailadress);
command.ExecuteNonQuery();
MessageBox.Show("Inserted");
}
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
}
测试等级。。。。。
Models model = new Models();
if (model.saveRecord(TbFisrtname.Text, TbLastname.Text,TbUsername.Text,TbPassword.Text,TbAddress.Text) == true)
{
MessageBox.Show("Inserted");
}
这表示FirstName
参数变量在传递到方法时为null。我建议验证你的论点:
public bool saveRecord(string Firstname,string Lastname,string Username,string Password,string Emailadress)
{
if (string.IsNullOrWhiteSpace(FirstName))
throw new ArgumentException(FirstName, "FirstName cannot be null");
// Do the same for the other args...
using (MySqlConnection connection = new MySqlConnection("Server=localhost;Port=8889;Database=SSC;Uid=root;Password=root"))
{
try
{
string Sql_Query = "INSERT INTO administrator (Firstname,Lastname,Username,Password,EmailAdress)VALUES(@Firstname,@Lastname,@Username,@Password,@EmailAdress);";
connection.Open();
using (MySqlCommand command = new MySqlCommand(Sql_Query, connection))
{
command.Parameters.AddWithValue("@Firstname", Firstname);
command.Parameters.AddWithValue("@Lastname", Lastname);
command.Parameters.AddWithValue("@Username", Username);
command.Parameters.AddWithValue("@Password", Password);
command.Parameters.AddWithValue("@EmailAdress", Emailadress);
command.ExecuteNonQuery();
MessageBox.Show("Inserted");
}
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
}