简体   繁体   中英

“Value cannot be null.\r\nParameter name: text”

I have a method (Win App C#) to fill DataGridView as below and used that in my TxB_ProitirySearch_TextChanged event:

void PrioFillGrid(bool IsSearching= false)
{
    if (IsSearching)
    {
        var ddd = from p in db.PDP_Priorities
                  where p.PriorityTitle.Contains(aski.Change(TxB_ProitirySearch.Text))
                  orderby p.ID descending
                  select new { p.ID, Title = p.PriorityTitle };

        if (ddd.Count() > 0)     // Solution1
        { 
            DG_Priority.DataSource = ddd; 
        }

        if (ddd != null)        // Solution2
        { 
            DG_Priority.DataSource = ddd; 
        }
        else
        {
            DG_Priority.DataSource = from p in db.PDP_Priorities
                                     orderby p.ID descending
                                     select new { p.ID, Title = p.PriorityTitle };
        }
    }
    else
    {
        DG_Priority.DataSource = from p in db.PDP_Priorities
                                 orderby p.ID descending
                                 select new { p.ID, Title = p.PriorityTitle };
    }
}

When I type a character, it's searching very well and updating data in gridview, but pressing backspace to clear the textbox and start a new search raises this exception:

An unhandled exception of type 'System.ArgumentNullException' occurred in System.Data.Linq.dll"

Value cannot be null. Parameter name: text

(Comment: aski.Change(TxB_ProitirySearch.Text) is a class to prevent unwanted characters from being saved in the database)

I'm wondering why both solution1 and 2 won't help.

Could anybody please help me?

Thanks in advance.

Great thanks to all of you, masters. changing "PriorityTitle.Contains to PriorityTitle?.Contains?" didn't work for me, but using others guide, I changed the class, aski.change() as below:

public string Change(string k)
    {
        if (k==string.Empty)
        {
            return null;
        }
        else
        {
            //some codes...
            return str;
        }
    }

and then changed my "PrioFillGrid()" method as below:

void PrioFillGrid(bool IsSearching= false)
    {
        if (aski.Change(TxB_ProitirySearch.Text) == null)
        {
            DG_Priority.DataSource = from p in db.PDP_Priorities
                                     orderby p.ID descending
                                     select new { p.ID, Title = p.PriorityTitle };
        }
        else if (IsSearching && aski.Change(TxB_ProitirySearch.Text) != null)
        {
            var ddd= from p in db.PDP_Priorities
                                     where p.PriorityTitle.Contains(aski.Change(TxB_ProitirySearch.Text))
                                     orderby p.ID descending
                                     select new { p.ID, Title = p.PriorityTitle };
            DG_Priority.DataSource = ddd;
        }
        else
        {
            DG_Priority.DataSource = from p in db.PDP_Priorities
                                     orderby p.ID descending
                                     select new { p.ID, Title = p.PriorityTitle };
        }
    }

and now is working well. Thanks again every body.

对我来说,问题是 DI 机制创建了UrlHelper实例,这导致该实例的所有属性都为 null,包括routeCollectionrequestContext ,通过将 UrlHelper 实例直接传递给需要它的方法来解决。

Solution 1 and 2 do not help because the exception is thrown prior.

I am guessing in the aski.Change() method.

Try

if (IsSearching && !string.IsNullOrEmpty(TxB_ProitirySearch.Text))

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