简体   繁体   中英

system.argumentnullexception value cannot be null

I tried to do a filter api service with this query

create proc usp_filter
@name VARCHAR(50),
@lastname VARCHAR(50),
@pass VARCHAR(50)
as
begin
SELECT * FROM tb_users
WHERE
(name = @name OR @name is null) AND
(lastname = @lastname OR @lastname is null) AND
(pass = @pass OR @pass is null)
end 
go

which filters data from a table but it can ignore some or all parameters if it is required.

so when i send the parameters in c#, it only returns the list if all parameters are entered, in case i want to send a null parameter this error is generated in the controller

An exception of type 'System.ArgumentNullException' occurred in System.Core.dll but was not handled in user code.

Additional information: The value cannot be null.

i tried to handle this error with DbNull but i get the same error

this is my method

public List<User> Filter(string name, string lastname, string pass)
        {
            List<User> users = null;
            cnx.Open();
            string sqlStatement = "usp_filter";
            SqlCommand cmd = new SqlCommand(sqlStatement, cnx);
            cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("@name", name ?? Convert.DBNull);

               cmd.Parameters.AddWithValue("@lastname", lastname ?? Convert.DBNull);

             cmd.Parameters.AddWithValue("@pass", pass ?? Convert.DBNull);

            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.HasRows)
            {
                users = new List<User>();
                while (reader.Read())
                {
                    User user = new User();
                    user.id = int.Parse(reader["id"].ToString());
                    user.name= reader["name"].ToString();
                    user.lastName= reader["lastname"].ToString();
                    user.address= reader["address"].ToString();
                    user.email = reader["email"].ToString();
                    user.pass= reader["pass"].ToString();
                    users.Add(user);
                }
            }
            cnx.Close();
            return users;
        }

and this is how i handled it in my controller.

 [HttpGet]
        public User Filter(string name,string lastname,string pass)
        {
            var list = business.Filter(name,lastname,pass);
            User user = list.Where(x => x.name == name && x.lastname == lastname && x.pass == pass).SingleOrDefault();

            return user;
        }

Edit of the exception https://i.stack.imgur.com/HKBrU.png

You input params on your proc need to be nullable. This would work.

@name VARCHAR(50)= NULL,
@lastname VARCHAR(50)= NULL,
@pass VARCHAR(50)= NULL

Also there are objects in .net for null values so you can change Convert.DBNull to System.Data.SqlTypes.SqlString.Null

string can't be null, so coalescing won't work ( ?? ). Best to write them as:

cmd.Parameters.AddWithValue("@name", string.IsNullOrEmpty(name) ?
    System.Data.SqlTypes.SqlString.Null : name);
cmd.Parameters.AddWithValue("@lastname", string.IsNullOrEmpty(lastname) ?
    System.Data.SqlTypes.SqlString.Null : lastname);
cmd.Parameters.AddWithValue("@pass", string.IsNullOrEmpty(pass) ?
    System.Data.SqlTypes.SqlString.Null : pass);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM