列Firstname不能为null

时间:2015-11-12 18:08:47

标签: c# mysql

它下面是我的代码..我在平台上尝试了几个答案,但它仍然给我同样的错误..有人请帮帮我!!!!提前谢谢!

var_dump($params);

array(3) {
  ["index"]=>
  string(5) "leads"
  ["type"]=>
  string(4) "lead"
  ["body"]=>
  array(1) {
    ["query"]=>
    array(1) {
      ["bool"]=>
      array(1) {
        ["should"]=>
        array(1) {
          ["match"]=>
          array(1) {
            ["LastName"]=>
            array(2) {
              ["query"]=>
              string(10) "Parker"
              ["boost"]=>
              string(1) "1"
        }
          }
        }
      }
    }
  }
}

测试类.....

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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这表示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;
            }
         }
     }