Trying to filter data in grid, receiving "Cannot create an instance of the static class" error

StackOverflow https://stackoverflow.com/questions/23225529

  •  07-07-2023
  •  | 
  •  

سؤال

I am attempting to filter the items populated in a grid in the code behind. When I try to call my adapter from the data access layer, I am receiving the following error:

Cannot create an instance of the static class 'SFTIP.DataAccessLayer.InventoryAdapter'

The filter is meant to only display items in the grid related to the user role (AssetOwnershipProgramIds).

The error is in this segment new InventoryAdapter() of this line:

filteredList = new InventoryAdapter().GetAllByFilter(inventoryFilter);

Here is the code for the filter I am trying to build:

public List<Inventory> BindGrid()
{
    List<Inventory> filteredList = new List<Inventory>();
    SearchFilterInventory inventoryFilter = new SearchFilterInventory();
    User currentUser;

    currentUser = (Session["CurrentUser"] == null) ? (User)Session["CurrentUser"] : new User();
    if (currentUser.AdminPrograms.Count > 0)
    {
        inventoryFilter.AssetOwnershipProgramIds.Add(currentUser.AdminPrograms[0].ReferenceId);
        filteredList = new InventoryAdapter().GetAllByFilter(inventoryFilter);
    }

    return filteredList;
}

Can anyone provide some guidance on to where I am going wrong? I know that this is something fairly simple - this is an inherited project and I'm still trying to connect all the dots. Thank you for taking a look.

هل كانت مفيدة؟

المحلول 4

Sorry about the delay in coming back back to this - been out ill & other newer work priorities.

So, - something so very simple. Made the mistake of assuming that "no one would ever do (or NOT do) that" and as programmers we ought to know better than to make that assumption, right? Anyhow, I had to add the <SelectParameters> back into ObjectDataSource in the aspx page:

    <asp:ObjectDataSource ID="odsItInventory" runat="server" SelectMethod="BindGrid" 
TypeName="ADRUO.GUI.UserControls.ExtendedInventoryGridUserControl">
    <SelectParameters>
    <asp:SessionParameter Name="User" Type="Object" SessionField="CurrentUser" />
    </SelectParameters>
    </asp:ObjectDataSource>

Thank you for the assistance - all of your comments were helpful, I believe each answer would have worked, had I had those parameters in the page. As it was, the param add was all that was required to resolve.

نصائح أخرى

The error says it all.

You cannot create an instance of a static class. If you wanted to make that, remove static keyword from your class declaration.

MSDN says:

A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.

Static class aren't meant to be instantiated:

A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.

(Source: MSDN)

Likely, the GetAllByFilter method is static as well. If that's the case, your problem will be solved by changing the faulting line to this:

var filtereditems = InventoryAdapter.GetAllByFilter(inventoryFilter);

You would need a class declaration such as this, to do in the way your code is implemented.

public class InventoryAdapter
{
  public InventoryAdapter() { }

  public object GetAllByFilter() { }
}

or else call your method like this, if it's meant to be static / you don't own or control it:

var filtereditems = InventoryAdapter.GetAllByFilter(inventoryFilter);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top