Type casting error

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 anni tempo fa
Hello there----

I have added VIEW ALL Linkbutton on Manufacturer/productingrid.ascx control........to view all the products made by the same manufacturer......

The designer file is---------------

<asp:LinkButton ID="LinkButton1" runat="server" Text="View All" CssClass="Viewallhyperlink"
              OnCommand="dl_viewall" CommandArgument="dlnew" CommandName="Viewall"></asp:LinkButton>

while the code is---------------

protected void dl_viewall(object sender, CommandEventArgs e)
        {
            LinkButton lb = (LinkButton)sender;
            // DataList dl = (DataList)lb.Parent.Parent.Parent.Parent;

            if (lb.CommandName == "Viewall")
            {
                dlProducts.DataSource = ManufacturerManager.GetProductsByManufacturer(ManufacturerId);
                dlProducts.DataBind();
                productsPager.Visible = false;
            }
        }

I have made the method to get all products which is----------

public static List<ProductManufacturer> GetProductsByManufacturer(int manufacturerId)
        {
            var context = ObjectContextHelper.CurrentObjectContext;
            var query = from mid in context.ProductManufacturers
                        from pid in context.Products
                        where pid.ProductId == mid.ProductId && mid.ManufacturerId == manufacturerId && pid.Deleted == false && pid.Published == true
                        select mid;
            var queryresult = query.ToList();
            return queryresult;
        }

When i debug this project it shows me the number of products by the manufacturer but when it reaches to Productbox1.ascx control to bind data in this page's datalist it gives me an error which is-----------


Unable to cast object of type 'System.Data.Entity.DynamicProxies.ProductManufacturer_1FB1FE3FB05820267E1F4A4ACFD563A3233BDF00631C7C5E4F4848DB890FE1A4' to type 'NopSolutions.NopCommerce.BusinessLogic.Products.Product'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Data.Entity.DynamicProxies.ProductManufacturer_1FB1FE3FB05820267E1F4A4ACFD563A3233BDF00631C7C5E4F4848DB890FE1A4' to type 'NopSolutions.NopCommerce.BusinessLogic.Products.Product'.

Source Error:


Line 67:             RepeatLayout="Table" ItemStyle-CssClass="item-box">
Line 68:             <ItemTemplate>
Line 69:                 <nopCommerce:ProductBox1 ID="ctrlProductBox" Product='<%# Container.DataItem %>'
Line 70:                     runat="server" />
Line 71:             </ItemTemplate>

Source File: e:\new projects\nopcommerce 1.8\NopCommerceStore\Templates\Manufacturers\ProductsInGrid.ascx    Line: 69


I can't find the solution to this error..........Please help me by solving this problem.
Do needful on ergent basis
13 anni tempo fa
If I understand what you are trying to do, is that you are removing the pagination from the manufacturer template and displaying all the manufacturer's products on a single page when the 'View All' link is clicked.

The problem with your code is that the method you added (GetProductsByManufacturer) to the ManufacturerManager class should return List<Product> and not List<ProductManufacturer>.

You should remove the method (GetProductsByManufacturer) from ManufacturerManager and add the following method to the ProductManager class since the method returns products:
/// <summary>
/// Gets all the products for a manufacturer
/// </summary>
/// <param name="manufacturerId">manufacturer identifier</param>
/// <returns>Product collection</returns>
public static List<Product> GetProductsByManufacturer(int manufacturerId)
{
    var context = ObjectContextHelper.CurrentObjectContext;
    var query = from p in context.Products
                join pm in context.ProductManufacturers
                on p.ProductId equals pm.ProductId
                orderby pm.DisplayOrder
                where pm.ManufacturerId == manufacturerId && (p.Published) && !p.Deleted
                select p;
    var queryresult = query.ToList();
    return queryresult;
}

Note that the sort order is fixed to the Nop_Product_Manufacturer_Mapping DisplayOrder field.

You will then need to update the code in ProductsInGrid.ascx.cs, dl_viewall method to use the GetProductsByManufacturer method in the ProductManager class:
(changed code underlined)
dlProducts.DataSource = ProductManager.GetProductsByManufacturer(ManufacturerId);

.
13 anni tempo fa
Thanks a lot for solving the problem.......
Seriously thanks
13 anni tempo fa
Hello there........

I am facing some problem regarding to the dropdownlist of Productvariant.ascx control.......I m working on a project that sells business cards and business card is a product its information is on Onevariant.ascx control and product attributes are on Productattribute.ascx control...

In this control only dropdownlist of Quantity appears physically....the rest dropdowns which i have added from admin side generates in runtime which are card thickness,signature panel,magnetic strip,Encoding etc...

I have three selected values in magnetic strip drop down list which are no-strip,hi-co and lo-co....

What i need to do is when i select no-strip value of magnetic strip drop down the next arriving dropdown of Encoding has to disappear...

I m bit confused with this task because the combos are generated on runtime...

The link for my site is--

Http://duracard-com.si-cloud.com........

You can see a preferred member card over there.....

Please help me by solving this problem...

Thankin you...
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.