Value Cannot Be Null. Parameter Name: Source

Value cannot be null. Parameter name: source

I had this one a while back, and the answer isn't necessarily what you'd expect. This error message often crops up when your connection string is wrong.

At a guess, you'll need something like this:

<connectionStrings>
<add name="hublisherEntities" connectionString="Data Source=localhost;Initial Catalog=hublisher;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=localhost;Initial Catalog=hublisher;Integrated Security=True" />
</parameters>
</defaultConnectionFactory>
</entityFramework>

What's happening is that it's looking for a data source in the wrong place; Entity Framework specifies it slightly differently. If you post your connection string and EF config then we can check.

How to resolve Value cannot be null. Parameter name: source in linq?

Error message clearly says that source parameter is null. Source is the enumerable you are enumerating. In your case it is ListMetadataKor object. And its definitely null at the time you are filtering it second time. Make sure you never assign null to this list. Just check all references to this list in your code and look for assignments.

Value cannot be null. Parameter name: source when trying to get data with web api

Your List<Fornecedor> dataModels; is initially null.

Either initialize it to an empty list,

List<Fornecedor> dataModels = new List<Fornecedor> ();

or wrap most of your razor code in an @if (dataModels != null) { ... }
look at the FetchData template page for an example.

It could be that the Blazorise.DataGrid does know how to handle a null collection, I would expect it to. But you also have foreach() loop in the Filter of the "Cidade" column, it looks like that is throwing the exception.

Error: Value cannot be null. Parameter name: source

Oh okay, it seems that your product list property is empty, use the code below to fill it.

public ActionResult MainAccount(int?id)
{
User us = Session["ActiveUser"] as User;

// add Prod = _context.Product.Where(p=>p.User.ID == us.ID).ToList()
var vm = new HMViewM()
{
homesec1 = _context.homesec1slider.ToList(),
userr = us,
Prod = _context.Product.Where(p=>p.User.ID == us.ID).ToList()
};

return View(vm);
}

Value cannot be null. Parameter name: source MVC & EF

Add null check

public List<Cart> GetCartItems()
{
if(storeDB.Carts != null)
{
return storeDB.Carts
.Where(cart => cart.CartId == ShoppingCartId)
.ToList();
}
return new List<Cart>();
}

Every methods from System.Linq that takes Reference types will check if they are null if(source== null) and throw a ArgumentNullException if they are.

In this case, you call Where that is an extension method for IEnumerable<TSource>. There exists 2 overloading of Where

public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> predicate);
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);

As you can see, there are first parameter with name "source", the error is:

Value cannot be null. Parameter name: source

it means that Carts is null. Add null check will solve the problem

Value Cannot be Null; Parameter Name 'Source'

I have tested the code and if the model is null you will indeed get the null exception.

Sample Image

The issue here is with the logic flow as you - first making your selection and then checking for nullability.

In the if block - check for null first and then select the results:

if (Model != null && Model.Select(p => p.Payments.Any(o => o.Expires > DateTime.Now)).Any())


Related Topics



Leave a reply



Submit