Click here to Skip to main content
15,886,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi all,
I am working on a school project to create a web portal that allows butchers to sign up and sell their meat online. now I have created two girdviews, one that i will use for my default catalog(displays products to allow butchers to select their products when creating their catalog)and the other one that will get items from gridview1 to gridview2. now in gridview2 i want butchers to be able to select and edit products and then save them to their own ButcherProducts table which has StoreID, ProductID, Price, Quantity. now i can get items from gridview1 to gridview2 but i am struggling to put the edit functionality so butchers can edit and save. here is my sample code.
ASP.NET
Select Category:  <asp:DropDownList runat="server" ID="category" AutoPostBack="true"
                    SelectMethod="GetCatalogCategories" AppendDataBoundItems="true"
                    DataValueField="CategoryID" DataTextField="CategoryName">
                    <asp:ListItem Value="" Text="-select-"/>
                </asp:DropDownList> </td>
            <td>  </td>
            <td> </td>
        </tr>
        <tr>
          
            <td> <asp:GridView ID="Catalog" runat="server" 
               
                AutoGenerateColumns="False" 
                ItemType="DataAccess.Product"
              
                AllowSorting="false"
                 DataKeyNames="ProductID"
                
              
                SelectMethod="GetCatalog">
   
        <Columns>
            <asp:BoundField DataField="ProductID" HeaderText="ProductID"
                SortExpression="ProductID" />
            <asp:BoundField DataField="ProductName" HeaderText="Product Name"
                SortExpression="ProductName" />
            <asp:BoundField DataField="Description" HeaderText="Description"
                SortExpression="Description" />
           
            <asp:BoundField DataField="CategoryID" HeaderText="CategoryID"
                SortExpression="CategoryID" />
         
            <asp:TemplateField HeaderText="Category">
             
             <ItemTemplate>
                    <%#:Item.Category.CategoryName %>
                </ItemTemplate>


            </asp:TemplateField>
         
               <asp:TemplateField HeaderText="Image">
                    <ItemTemplate>
                 <img src="..Checkout/images/<%#:Item.ImagePath%>"
                                                width="20" height="25" border="1" />
                         </ItemTemplate>
                  
                    </asp:TemplateField>
            <asp:TemplateField HeaderText="Select Products">
                
               <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
     
     
        </Columns>

C# code behind:
C#
List<product> tempIDList = new List<product>();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        private MeatbyMobileContext _db = new MeatbyMobileContext();
        public IQueryable<category> GetCatalogCategories()
        {

            return _db.Categories;

        }

        public IQueryable<product> GetCatalog([Control]int? category)
        {
            var query = _db.Products.Select(p => p);
            if (category.HasValue)
            {
                query = query.Where(p => p.CategoryID == category.Value);
            
            }
            return query;
        
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
           

        for (int z = 0; z < Catalog.Rows.Count; z++)
        {
           MeatbyMobileContext db = new MeatbyMobileContext();

           
            CheckBox chk =  (CheckBox)Catalog.Rows[z].FindControl("CheckBox1");
            if (chk.Checked)
            {

              int dKey = Convert.ToInt32( Catalog.DataKeys[z].Value);
              
                Product product = (from c in db.Products
                                   where c.ProductID.Equals(dKey)
                                select c).SingleOrDefault();

                 tempIDList.Add(product );
                 



        
            }
        
        }

        GridView2.DataSource = tempIDList;
        GridView2.DataBind();

now i would like help on how do i move from here after the data is in gridview2.
Posted
Updated 7-Oct-13 18:57pm
v3

1 solution

you have to do like this
now your information of tempdata that is shown in first page gridview is stored in session
session["dtlist"]=tempIDList;

now go to another page on paqe load or where ever you want then cast this session and then store table in another value for Example

i am doing on page load

datatable dt=(datatable)session["dtlist"];
now

gridview.datasource=dt;
gridview.databind

Try and let me know
 
Share this answer
 
Comments
Xavi Giqwa 8-Oct-13 3:41am    
Hi chintransh635
thank you for your reply and help.
i tried you code and so far ive getting this error:
Unable to cast object of type 'System.Collections.Generic.List`1[DataAccess.Product]' to type 'System.Data.DataTable'.

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.Collections.Generic.List`1[DataAccess.Product]' to type 'System.Data.DataTable'.

Source Error:


Line 67: }
Line 68:
Line 69: DataTable dt = (DataTable)Session["dtlist"];
Line 70:
Line 71:

Source File: c:\Users\Admin\Desktop\MeatbyMobileFinal\MeatbyMobile\MeatbyMobile\Butchers\ButcherCatalog.aspx.cs Line: 69

Stack Trace:


[InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[DataAccess.Product]' to type 'System.Data.DataTable'.]
MeatbyMobile.Butchers.ButcherCatalog.Button4_Click(Object sender, EventArgs e) in c:\Users\Admin\Desktop\MeatbyMobileFinal\MeatbyMobile\MeatbyMobile\Butchers\ButcherCatalog.aspx.cs:69
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9552602
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
also i want the items in the new gridview to be editable so butchers can save to their catalog

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900