I don't know why my code don't work(it's for shopping cart)

I wrote this code for shopping cart.I used c lass for it and i want to store orders in cache.the customer username is in a session.customer select the product from a datalist.but and I want to show the orders in gridview .but when I select some product and go to the page that gridview is in it (shopping cart page)I could,t see any thing.please look at my code and see if I wrote my code incorrect.thanks for your helping me.

-------------------------------------------------------------------------

 //the code for add product to shopping cart(products are in data list)
protected void order(object source, DataListCommandEventArgs e)
{

if (Session["user"] != null)
{
string name=Session["user"].ToString();
             if (e.CommandName.CompareTo("Porder")==0)
            {
                string pid = DataList2.DataKeys[e.Item.ItemIndex].ToString();
                string degree = ((Label)e.Item.FindControl("degreeLabel")).Text;
                string available = ((Label)e.Item.FindControl("available_nowLabel")).Text;
                string package_type = ((Label)e.Item.FindControl("package_typeLabel")).Text;
                string price = ((Label)e.Item.FindControl("priceLabel")).Text;
                string quantity = ((TextBox)e.Item.FindControl("TextBox1")).Text;
                string type=((Label)e.Item.FindControl("Label12")).Text;
                string category_name=((Label)e.Item.FindControl("Label13")).Text;
                shopping sh = new shopping();
                sh.add_order(pid, type, quantity, category_name, price, available, package_type);
                if (Cache["name"] == null)
                {
                                       Cache.Insert("name",sh,null,DateTime.Now.AddMinutes(60),TimeSpan.Zero);
                    }

                    
                    
                
            
  
            }
 
--------------------------------------
 //page loade of the page that gridview is in it(shoppinf cart page)
protected void Page_Load(object sender, EventArgs e)
    {


        if (!Page.IsPostBack)
        {
            if (Session["user"] == null)
            {
                Response.Redirect("home.aspx");
            }
            if (Cache["name"] != null)
            {
                List<shopping> myitem = (List<shopping>)Cache["name"];
                foreach (shopping item in myitem)
                {
                    GridView1.DataSource = myitem;
                    GridView1.DataBind();

                }
            }
        }
            
            
        
    }
----------------------------------
 //class 
public class basketitem
{
	private string _pid;
    private string _quantity;
    private string _category_name;
    private string _type;
    private string _price;
    private string _available;
    private string _package_type;

    public string pid
    {
        get { return _pid; }
        set { _pid = value; }
    }
    public string quantity
    {
        get { return _quantity; }
        set { _quantity = value; }
    }
    public string type
    {
        get { return _type; }
        set { _type = value; }
    }
    public string price
    {
        get { return _price; }
        set { _price = value; }
    }
    public string package_type
    {
        get { return _package_type; }
        set { _package_type = value; }
    }
    public string available
    {
        get{return _available;}
        set { _available = value; }
    }
    public string category_name
    {
        get { return _category_name; }
        set { _category_name = value; }
    }

        }
public class shopping
{
    private List _items;
    public shopping()
    {
        if (this._items == null)
        {
            this._items = new List();
        }
    }
    public List items
    {
        get { return _items; }
        set { _items = value; }
    }
    public void add_order(string pid,string type,string quantity,string category_name,string price,string available,string package_type)
    {
 //addind products to shoppinfg cart        
        basketitem newitem=new basketitem();
        newitem.package_type=package_type;
        newitem.pid=pid;
        newitem.price=price;
        newitem.quantity=quantity;
        newitem.category_name=category_name;
        newitem.available=available;
        newitem.quantity=quantity;
        _items.Add(newitem);
    }
    public void update(int rowid,string pid,string quantity)
    {
        basketitem item=_items[rowid];
        item.pid=pid;
        item.quantity=quantity;
        
    }
    public void delete(int rowid)
    {
        _items.RemoveAt(rowid);
    }
    }
  
  
 
0
bbc_sh
9/21/2008 2:15:25 PM
asp.net.getting-started 91979 articles. 4 followers. Follow

20 Replies
1432 Views

Similar Articles

[PageSpeed] 8

Hi bbc_sh,

It will be better if you give the exact problem area or you can tell what exactly happening instead of what. This way we can help you. The code you have given is C#, we cannt test it without the proper WebControls (gridview / datalist) etc. We also need to have data for proper testing.

So it will be better if you could tell the problem area.

Thanks a lot

0
abhishek_damle
9/21/2008 3:33:06 PM

 here is datalist and gridview.the product are in datalist and I want to see the orders in gridview.I don't know that I use my class correct ,and the code of cache is right or wrong.

<asp:DataList ID="DataList2" runat="server" DataKeyField="product_id" DataSourceID="SqlDataSource2" CellPadding="5" CellSpacing="10" RepeatColumns="2">
                    <ItemTemplate>
                       <table bgcolor="#ffffff" cellspacing="0">
                            <tr>
                                <td align="right" colspan="3" style="background-position-x: right; background-image: url(picture/sboxb2.jpg); background-repeat: no-repeat; height: 24px;">
                                    </td>
                            </tr>
                           <tr>
                               <td style="width: 100px">
                                   <asp:Label ID="Label10" runat="server" CssClass="titr2" Text="product :"></asp:Label></td>
                               <td align="right" style="width: 100px">
                                   <asp:Label ID="Label12" runat="server" Text='<%# Eval("category_name") %>'></asp:Label></td>
                               <td style="width: 100px">
                               </td>
                           </tr>
                           <tr>
                               <td style="width: 100px">
                                   <asp:Label ID="Label11" runat="server" CssClass="titr2" Text="type :"></asp:Label></td>
                               <td align="right" style="width: 100px">
                                   <asp:Label ID="Label13" runat="server" Text='<%# Eval("type") %>'></asp:Label></td>
                               <td style="width: 100px">
                               </td>
                           </tr>
                            <tr>
                                <td style="width: 100px">
                                    <asp:Label ID="Label2" runat="server" Text="degree :" CssClass="titr2"></asp:Label></td>
                                <td style="width: 100px" align="right">
                                    <asp:Label ID="degreeLabel" runat="server" Text='<%# Eval("degree") %>'></asp:Label></td>
                                <td style="width: 100px">
                                </td>
                            </tr>
                            <tr>
                                <td style="width: 100px">
                                    <asp:Label ID="Label3" runat="server" Text="w :" Width="76px" CssClass="titr2"></asp:Label></td>
                                <td colspan="2" align="right">
                                    <asp:Label ID="carton_weightLabel" runat="server" Text='<%# Eval("carton_weight") %>'></asp:Label></td>
                            </tr>
                            <tr>
                                <td style="width: 100px; ">
                                    <asp:Label ID="Label4" runat="server" Text="pack :" Width="83px" CssClass="titr2"></asp:Label></td>
                                <td colspan="2"  align="right">
                                    <asp:Label ID="package_typeLabel" runat="server" Height="17px" Text='<%# Eval("package_type") %>'
                                        Width="99px"></asp:Label></td>
                            </tr>
                            <tr>
                                <td style="width: 100px; height: 16px;">
                                    <asp:Label ID="Label5" runat="server" Text="temp :" Width="91px" CssClass="titr2"></asp:Label></td>
                                <td align="right" colspan="2" style="height: 16px">
                                    <asp:Label ID="temp_controlLabel" runat="server" Text='<%# Eval("temp_control") %>'></asp:Label></td>
                            </tr>
                            <tr>
                                <td style="width: 100px; height: 16px;">
                                    <asp:Label ID="Label6" runat="server" Text="av :" CssClass="titr2"></asp:Label></td>
                                <td colspan="2" align="right" style="height: 16px">
                                    <asp:Label ID="available_nowLabel" runat="server" Text='<%# Eval("available_now") %>'></asp:Label></td>
                            </tr>
                            <tr>
                                <td style="width: 100px">
                                    <asp:Label ID="Label7" runat="server" Text="price :" CssClass="titr2"></asp:Label></td>
                                <td style="width: 100px" align="right">
                                    <asp:Label ID="priceLabel" runat="server" Text='<%# Eval("price") %>'></asp:Label></td>
                                <td style="width: 100px">
                                </td>
                            </tr>
                            <tr>
                                <td style="width: 100px; height: 18px">
                                    <asp:Label ID="Label9" runat="server" CssClass="titr2" Text="qty  :"></asp:Label></td>
                                <td align="right" style="width: 100px; height: 18px">
                                    <asp:TextBox Width=50px ID="TextBox1" runat="server"></asp:TextBox></td>
                                <td style="width: 100px; height: 18px">
                                </td>
                            </tr>
                            <tr>
                                <td style="width: 100px">
                                </td>
                                <td align="right" style="width: 100px">
                                    <asp:Label ID="Label8" runat="server" ForeColor="White"></asp:Label></td>
                                <td style="width: 100px">
                                </td>
                            </tr>
                            <tr>
                                <td style="width: 100px" align="left">
                                    <asp:Button ID="Button2" runat="server" CommandName="Sorder" CssClass="button" Text="simple" Width="80px" CommandArgument='<%# Eval("product_id") %>' /></td>
                                <td style="width: 100px" align="left">
                                    <asp:Button ID="Button1" runat="server" CommandName="Porder" CssClass="button" Text="order" Width="80px" CommandArgument='<%# Eval("product_id") %>' /></td>
                                <td style="width: 100px">
                                </td>
                            </tr>
                            <tr>
                                <td style="width: 100px">
                                </td>
                                <td style="width: 100px">
                                </td>
                                <td style="width: 100px">
                                </td>
                            </tr>
                            <tr>
                                <td height=24 align="left" colspan="3" style="background-position-x: left; background-image: url(picture/sboxp2.jpg); background-repeat: no-repeat">
                                    </td>
                            </tr>
                        </table>
                    </ItemTemplate>
                </asp:DataList><asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Bahar#ConnectionString3 %>"
                    SelectCommand="SELECT * FROM [newshow] WHERE ([id_type] = @id_type)">
                    <SelectParameters>
                        <asp:QueryStringParameter Name="id_type" QueryStringField="pid" Type="String" />
                    </SelectParameters>
                </asp:SqlDataSource>

-------------------------------

<asp:GridView      ID="GridView1" runat="server" AutoGenerateColumns="False" Height=100% Width="548px"  ShowFooter="True" OnRowCommand="order_del" DataKeyNames="product_id" OnRowDeleting="GridView1_RowDeleting" CssClass="gv" OnRowDataBound="confirm" OnRowCancelingEdit="cancel" OnRowEditing="editorder" OnRowUpdating="qtyupdate"  >
    <Columns>
        <asp:TemplateField HeaderText="pid">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("product_id") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label4" runat="server" Text='<%# Bind("product_id") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="name">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("category_name") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("category_name") %>'></asp:Label>
                <asp:Label ID="Label2" runat="server" Text='<%# Eval("type") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    <asp:BoundField DataField="degree"  HeaderText="degree" ReadOnly="True" />
    <asp:BoundField DataField="package_type" HeaderText="pack" ReadOnly="True" />
    <asp:BoundField DataField="available_now" HeaderText="av" ReadOnly="True" />
        <asp:TemplateField HeaderText="price">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("price") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Bind("price") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField HeaderText="qty" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="Button3" runat="server" CommandName="delete" CssClass="button" Text="del" CommandArgument='<%# Eval("product_id") %>' OnClientClick="return confirmdelete();"  />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ButtonType="Button" CancelText="cancel" DeleteText="delete" EditText="edit"
            ShowDeleteButton="True" ShowEditButton="True" UpdateText="update">
            <ControlStyle CssClass="button" />
        </asp:CommandField>
       
    </Columns>
        <HeaderStyle CssClass="gvheader" />
        <FooterStyle CssClass="gvfooter" />
        <AlternatingRowStyle CssClass="gva" />
    </asp:GridView>
0
bbc_sh
9/21/2008 4:34:03 PM

HI bbc_sh,

After going through your code, i got the problem. Basically you were not managing generic list correctly.

Following are the modified methods :

 

Change 1:

// here I am checking the availability of cach in each postback. I have removed the condition if(!Page.IsPostback). and also I am taking the list<shopping> out of Cach["name"]. Bind the complete list. if you loop foreach to item in it, you will see only last item in gridview.

 

protected void Page_Load(object sender, EventArgs e)
{
        if (Session["user"] == null)
        {
            Response.Redirect("home.aspx");
        }
        if (Cache["name"] != null)
        {
            List<shopping> myitem = (List<shopping>)Cache["name"];
            GridView1.DataSource = myitem;
            GridView1.DataBind();
        }
}

 

Change 2:

 ///Here i am storing List<shopping> in cache. after that i am againg checking cache and updating the gridview with new item.

 

    protected void order(object source, DataListCommandEventArgs e)
    {
        if (Session["user"] != null)
        {
            string name = Session["user"].ToString();
            if (e.CommandName.CompareTo("Porder") == 0)
            {
                string pid = DataList2.DataKeys[e.Item.ItemIndex].ToString();
                string degree = ((Label)e.Item.FindControl("degreeLabel")).Text;
                string available = ((Label)e.Item.FindControl("available_nowLabel")).Text;
                string package_type = ((Label)e.Item.FindControl("package_typeLabel")).Text;
                string price = ((Label)e.Item.FindControl("priceLabel")).Text;
                string quantity = ((TextBox)e.Item.FindControl("TextBox1")).Text;
                string type = ((Label)e.Item.FindControl("Label12")).Text;
                string category_name = ((Label)e.Item.FindControl("Label13")).Text;

                shopping sh = new shopping();
                sh.add_order(pid, type, quantity, category_name, price, available, package_type);
                if (Cache["name"] == null)
                {
                    Cache.Insert("name", sh.items, null, DateTime.Now.AddMinutes(60), TimeSpan.Zero);
                }

                if (Cache["name"] != null)
                {
                    List<shopping> myitem = (List<shopping>)Cache["name"];
                    GridView1.DataSource = myitem;
                    GridView1.DataBind();
                }
            }

        }
    }

 

Change 3:

 

 //Here you didn't specify the generic List correctly. You missed the object part, thats what i have corrected it. I am doing nothing but creating a new List<shopping> and adding items in it.

 

 

public class shopping
{
    private List<basketitem> _items;
    public shopping()
    {
        if (this._items == null)
        {
            this._items = new List<basketitem>();
        }
    }
    public List<basketitem> items
    {
        get { return _items; }
        set { _items = value; }
    }
    public void add_order(string pid, string type, string quantity, string category_name, string price, string available, string package_type)
    {
        //addind products to shoppinfg cart       
        basketitem newitem = new basketitem();
        newitem.package_type = package_type;
        newitem.pid = pid;
        newitem.price = price;
        newitem.quantity = quantity;
        newitem.category_name = category_name;
        newitem.available = available;
        newitem.quantity = quantity;
        _items.Add(newitem);
    }
}

 

I think this is enough, If this solves your problem, PLEASE MARK IT AS ANSWER !!!

for more assistance you are welcome

Thank you.

0
abhishek_damle
9/21/2008 6:24:46 PM

abhishek_damle:

Change 2:

 ///Here i am storing List<shopping> in cache. after that i am againg checking cache and updating the gridview with new item.

 

    protected void order(object source, DataListCommandEventArgs e)
    {
        if (Session["user"] != null)
        {
            string name = Session["user"].ToString();
            if (e.CommandName.CompareTo("Porder") == 0)
            {
                string pid = DataList2.DataKeys[e.Item.ItemIndex].ToString();
                string degree = ((Label)e.Item.FindControl("degreeLabel")).Text;
                string available = ((Label)e.Item.FindControl("available_nowLabel")).Text;
                string package_type = ((Label)e.Item.FindControl("package_typeLabel")).Text;
                string price = ((Label)e.Item.FindControl("priceLabel")).Text;
                string quantity = ((TextBox)e.Item.FindControl("TextBox1")).Text;
                string type = ((Label)e.Item.FindControl("Label12")).Text;
                string category_name = ((Label)e.Item.FindControl("Label13")).Text;

                shopping sh = new shopping();
                sh.add_order(pid, type, quantity, category_name, price, available, package_type);
                if (Cache["name"] == null)
                {
                    Cache.Insert("name", sh.items, null, DateTime.Now.AddMinutes(60), TimeSpan.Zero);
                }

                if (Cache["name"] != null)
                {
                    List<shopping> myitem = (List<shopping>)Cache["name"];
                    GridView1.DataSource = myitem;
                    GridView1.DataBind();
                }
            }

        }
    }

----------------------------------------------

Hi abhishek

thank you very much for your help.

but in change2  that you update the gridview with new order again,my gridview isn't in that page so I can't reach it's Id.shoppingcart page and order page are seperate.

gridview  is in shopping cart page.what should i do?

0
bbc_sh
9/22/2008 6:23:42 AM

Hi bbc_sh,

I didn't know that both controls exist in different page.

Anyways, Do you want to display shopping cart page after user clicks on order? If yes do following changes:

 

protected void order(object source, DataListCommandEventArgs e)
    {
        if (Session["user"] != null)
        {
            string name = Session["user"].ToString();
            if (e.CommandName.CompareTo("Porder") == 0)
            {
                string pid = DataList2.DataKeys[e.Item.ItemIndex].ToString();
                string degree = ((Label)e.Item.FindControl("degreeLabel")).Text;
                string available = ((Label)e.Item.FindControl("available_nowLabel")).Text;
                string package_type = ((Label)e.Item.FindControl("package_typeLabel")).Text;
                string price = ((Label)e.Item.FindControl("priceLabel")).Text;
                string quantity = ((TextBox)e.Item.FindControl("TextBox1")).Text;
                string type = ((Label)e.Item.FindControl("Label12")).Text;
                string category_name = ((Label)e.Item.FindControl("Label13")).Text;

                shopping sh = new shopping();
                sh.add_order(pid, type, quantity, category_name, price, available, package_type);
                if (Cache["name"] == null)
                {
                    Cache.Insert("name", sh.items, null, DateTime.Now.AddMinutes(60), TimeSpan.Zero);
                }

                Response.Redirect("~/shoppingCart.aspx");   /// change the path to your shopping cart page.

            }

        }
    }

 

 

 

If you don't wish to redirect user to shopping cart page unless he clicks on the shopping cart page manually, do the following code changes:

 

 

 protected void order(object source, DataListCommandEventArgs e)
    {
        if (Session["user"] != null)
        {
            string name = Session["user"].ToString();
            if (e.CommandName.CompareTo("Porder") == 0)
            {
                string pid = DataList2.DataKeys[e.Item.ItemIndex].ToString();
                string degree = ((Label)e.Item.FindControl("degreeLabel")).Text;
                string available = ((Label)e.Item.FindControl("available_nowLabel")).Text;
                string package_type = ((Label)e.Item.FindControl("package_typeLabel")).Text;
                string price = ((Label)e.Item.FindControl("priceLabel")).Text;
                string quantity = ((TextBox)e.Item.FindControl("TextBox1")).Text;
                string type = ((Label)e.Item.FindControl("Label12")).Text;
                string category_name = ((Label)e.Item.FindControl("Label13")).Text;

                shopping sh = new shopping();
                sh.add_order(pid, type, quantity, category_name, price, available, package_type);
                if (Cache["name"] == null)
                {
                    Cache.Insert("name", sh.items, null, DateTime.Now.AddMinutes(60), TimeSpan.Zero);
                }

            }

        }
    }

 

 

I hope This solves your problem, If it does PLEASE MARK IT ANSWER.

More more support reply back.

Thank you

0
abhishek_damle
9/22/2008 6:40:14 AM

I correct my code.but when I select products,and then go to shopping cart page, I can't see the the order gridview and the page is empty.I checked the visibility of gridview it was true. and another question:(isn't necessary that I add namespace for cache)?

-----------------

if (Cache["name"] == null)

{ Cache.Insert("name", sh.items, null, DateTime.Now.AddMinutes(60), TimeSpan.Zero);

}

-------------

if cache ["name"]==null the order list add to cache,it means that it's the first order but for the second and ... orders the cache isn't null,then how can add the list to cache?

0
bbc_sh
9/22/2008 7:49:24 AM

Hi bbc_sh,

I will suggest to use session as follows:

 Session["name"] = sh.items;

 

to retrieve value use following:

List<shopping> myitem = (List<shopping>)Session["name"];

 

Thanks

0
abhishek_damle
9/22/2008 8:25:28 AM

Hi abhishek

thanks for getting time to help me.I have three question:

1-you suggest me to use session.some one told me cache and some one told me session. I confused Confused.for my scenario please tell me,it's better that I use session or cache,why?

2- when I write my code with cache,for the first time that customer add a product to shopping cart I wrote this code:

 

 if (Cache["name"] == null)
                {
                    Cache.Insert("name", sh.items, null, DateTime.Now.AddMinutes(60), TimeSpan.Zero);
                }
  

but in the next selection the cache isn't empty because customet selected a product before, so,how the product should be add to cache?

3-when I select product and go to shpping cart page give this error: I used session for this too.but gave this error again:

my page load code is:

 

protected void Page_Load(object sender, EventArgs e)
    {
          if (Session["user"] == null)
            {
                Response.Redirect("home.aspx");
            }
            if (Cache["name"] != null)
            {

                List<shopping> myitem = (List<shopping>)Cache["name"];
                GridView1.DataSource = myitem;
                GridView1.DataBind();
            }
           
    }
--------------------------------------------
 
 
 
0
bbc_sh
9/22/2008 2:02:41 PM

Hi abhishek

thanks for getting time to help me.I have three question:

1-you suggest me to use session.some one told me cache and some one told me session. I confused Confused.for my scenario please tell me,it's better that I use session or cache,why?

2- when I write my code with cache,for the first time that customer add a product to shopping cart I wrote this code:

 

 if (Cache["name"] == null)
                {
                    Cache.Insert("name", sh.items, null, DateTime.Now.AddMinutes(60), TimeSpan.Zero);
                }
  

but in the next selection the cache isn't empty because customet selected a product before, so,how the product should be add to cache?

3-when I select product and go to shpping cart page give this error: I used session for this too.but gave this error again:

my page load code is:

 

protected void Page_Load(object sender, EventArgs e)
    {
          if (Session["user"] == null)
            {
                Response.Redirect("home.aspx");
            }
            if (Cache["name"] != null)
            {

                List<shopping> myitem = (List<shopping>)Cache["name"];
                GridView1.DataSource = myitem;
                GridView1.DataBind();
            }
           
    }
--------------------------------------------
 
 
 
0
bbc_sh
9/22/2008 2:03:26 PM

 nobody can't help me.

0
bbc_sh
9/23/2008 5:23:29 AM

HI bbc_sh,

Hey dont worry I will solve your problem. Please find your answers below

 

Q1/Ans1   -->   There is slight difference between cache and session. Please go through the following link for more clarity.

http://www.codeproject.com/KB/aspnet/PTCacheSessionViewState.aspx

 

Q2/Ans2   -- > Please do the following code changes in your methods. I have not followed any order. Please identify the location of existing method and replace it with the follewing ones.

 

here I am doing nothing but accessing the the cache and adding the new order at the end of the existing list. I have chaged the list object from <shopping> to <basketitem> !!!!

 

public void add_order(string pid,string type,string quantity,string category_name,string price,string available,string package_type)
    {
        //addind products to shoppinfg cart       
        basketitem newitem=new basketitem();
        newitem.package_type=package_type;
        newitem.pid=pid;
        newitem.price=price;
        newitem.quantity=quantity;
        newitem.category_name=category_name;
        newitem.available=available;
        newitem.quantity=quantity;
        if(Cache["name"] != null)
        {
                   _items = (List<basketitem>)Cache["name"];
                  Cache.Remove("name");
        }
        _items.Add(newitem);
    }

 

Q3/Ans3    -> Regarding error, I have fixed it. You need to type cast as follows:

 

protected void Page_Load(object sender, EventArgs e)
{
        if (Session["user"] == null)
        {
            Response.Redirect("home.aspx");
        }
        if (Cache["name"] != null)
        {
            List<basketitem> myitem = (List<basketitem>)Cache["name"];
            GridView1.DataSource = myitem;
            GridView1.DataBind();
        }

 

 

This should solve your problem. If it does MARK IT AS ANSWER

Thank you.

0
abhishek_damle
9/23/2008 9:28:36 AM


abhishek_damle:
public void add_order(string pid,string type,string quantity,string category_name,string price,string available,string package_type)
    {
        //addind products to shoppinfg cart       
        basketitem newitem=new basketitem();
        newitem.package_type=package_type;
        newitem.pid=pid;
        newitem.price=price;
        newitem.quantity=quantity;
        newitem.category_name=category_name;
        newitem.available=available;
        newitem.quantity=quantity;
        if(Cache["name"] != null)
        {
                   _items = (List<basketitem>)Cache["name"];
                  Cache.Remove("name");
        }
        _items.Add(newitem);
    }

Hi abhishek

I decided to write my project in session.but if I want to change the code to session ,I must write the code of above?(with session)

my class code is in app code folder ,so i cant write the code of session in it.

0
bbc_sh
9/23/2008 10:13:04 AM

OK, Do the changes as follows :

 

protected void order(object source, DataListCommandEventArgs e)
    {
        if (Session["user"] != null)
        {
            string name = Session["user"].ToString();
            if (e.CommandName.CompareTo("Porder") == 0)
            {
                string pid = DataList2.DataKeys[e.Item.ItemIndex].ToString();
                string degree = ((Label)e.Item.FindControl("degreeLabel")).Text;
                string available = ((Label)e.Item.FindControl("available_nowLabel")).Text;
                string package_type = ((Label)e.Item.FindControl("package_typeLabel")).Text;
                string price = ((Label)e.Item.FindControl("priceLabel")).Text;
                string quantity = ((TextBox)e.Item.FindControl("TextBox1")).Text;
                string type = ((Label)e.Item.FindControl("Label12")).Text;
                string category_name = ((Label)e.Item.FindControl("Label13")).Text;

                shopping sh = new shopping();

                List<basketitem> oldItemList = new List<basketitem>();
                if (Cache["name"] != null)
                {   
                             oldItemList  = (List<basketitem>)Cache["name"];
                             Cache.Remove("name");
                }
   
                sh.add_order(pid, type, quantity, category_name, price, available, package_type, oldItemList);
                if (Cache["name"] == null)
                {
                    Cache.Insert("name", sh.items, null, DateTime.Now.AddMinutes(60), TimeSpan.Zero);
                }

                Response.Redirect("~/shoppingCart.aspx");   /// change the path to your shopping cart page.

            }

        }
    }

 

 


public void add_order(string pid,string type,string quantity,string category_name,string price,string available,string package_type, List<basketitem> oldItemList)
    {
        //addind products to shoppinfg cart       
        basketitem newitem=new basketitem();
        newitem.package_type=package_type;
        newitem.pid=pid;
        newitem.price=price;
        newitem.quantity=quantity;
        newitem.category_name=category_name;
        newitem.available=available;
        newitem.quantity=quantity;
        if(oldItemList.Count > 0)
        {
         _items = oldItemList;                 
        }
        _items.Add(newitem);
    }

 

 

I a doing nothing but accessing cache in your normal C# page and passing the old list to method and retaining the old values and adding the new one.

I hope this is usefull, If it is MARK IT AS ANSWER.

Thank you

0
abhishek_damle
9/23/2008 10:46:34 AM

 Hi abhishek

thanks allot for your help.I change it to session and it worked well

0
bbc_sh
9/23/2008 12:28:02 PM

 Hi abhishek

I have problem again.my problem is about deleting and updating the basket gridview.I  wrote part of this code but I don't know how to get the list and update the quantity of it.

--------------------------------------------------

 public void update(int rowid,string product_id,string quantity)
    {
        basketitem item=_items[rowid];
        item.product_id=product_id;
        item.quantity=quantity;
        
    }
    public void delete(int rowid)
    {
        _items.RemoveAt(rowid);
    }

--------------------------------

protected void qtyupdate(object sender, GridViewUpdateEventArgs e)
    {
        TextBox qtytext = (TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0];
        int qty = Convert.ToInt32(qtytext.Text);
        shopping sh = new shopping();
        
        
        

    }

-------------------------

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }

-------------------------------

0
bbc_sh
9/23/2008 5:42:51 PM

HI bbc_sh,

Find the code changes below:

 

public void update(int rowid,string product_id,string quantity)
    {

        List<basketitem> myitem = (List<basketitem>)Cache["name"];

        myitem[rowid].pid = product_id;

        myitem[rowid].quantity = quantity;


        GridView1.DataSource = myitem ;
        GridView1.DataBind();

    }

0
abhishek_damle
9/24/2008 7:02:53 AM

Hi abhishek

my class is in app code folder.and I can't reach to gridview1 id.and I used command field for my gridview and I want to write code in gridview update event and delete event,but i don't know how to get my list:

<asp:CommandField ButtonType="Button" CancelText="cancel" DeleteText="delete" EditText="edit"
            ShowDeleteButton="True" ShowEditButton="True" UpdateText="update">
            <ControlStyle CssClass="button" />
        </asp:CommandField>

------------------------------

I wrote his but it don't work

protected void qtyupdate(object sender, GridViewUpdateEventArgs e)
    {
        TextBox qtytext = (TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0];
        string qty = qtytext.Text.Trim();
        Label pidtext = (Label)GridView1.Rows[e.RowIndex].FindControl("label1");
        string pid = pidtext.Text.Trim();
        shopping sh = new shopping();
        sh.update(e.RowIndex, pid, qty);

 GridView1.DataBind();


        
        

    }

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        shopping sh = new shopping();
        sh.delete(e.RowIndex);
GridView1.DataBind();

    }
0
bbc_sh
9/24/2008 7:50:59 AM

Hi bbc_sh,

you need to call the following gridview event and do the changes in other method:

 

protected void qtyupdate(object sender, GridViewUpdateEventArgs e)
    {
        TextBox qtytext = (TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0];
        string qty = qtytext.Text.Trim();
        Label pidtext = (Label)GridView1.Rows[e.RowIndex].FindControl("label1");
        string pid = pidtext.Text.Trim();

        List<basketitem> myitem = (List<basketitem>)Cache["name"];
        List<basketitem> newMyItem = new  List<basketitem>();
        shopping sh = new shopping();
        newMyItem  = sh.update(e.RowIndex, pid, qty, myitem);

        Cache.Remove("name");
        Cache.Insert("name", newMyItem, null, DateTime.Now.AddMinutes(60), TimeSpan.Zero);

        GridView1.DataSource = newMyItem  ;
        GridView1.DataBind();


    }

 

public List<basketitem> update(int rowid,string product_id,string quantity, List<basketitem> myitem)
    {       
        myitem[rowid].pid = product_id;

        myitem[rowid].quantity = quantity;

        return myitem;
    }

 

I am sure this will solve your problem. If it does Mark as ANSWER.

Thank you.

0
abhishek_damle
9/24/2008 9:24:48 AM

Hi abhishek 

i wrote that code in session.is my code correct? is it correct that we remove session and add item to it again(for performance)

 

 
protected void qtyupdate(object sender, GridViewUpdateEventArgs e)
    {
        TextBox qtytext = (TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0];
        string qty = qtytext.Text.Trim();
        Label pidtext = (Label)GridView1.Rows[e.RowIndex].FindControl("label1");
        string pid = pidtext.Text.Trim();
        List<basketitem> myitem = (List<basketitem>)Session["basket"];
        List<basketitem> newmyitem = new List<basketitem>();
        shopping sh = new shopping();
        newmyitem = sh.update(e.RowIndex, pid, qty, myitem);
        Session.Remove("basket");
        Session["basket"] = newmyitem;
        GridView1.DataSource = newmyitem;
        GridView1.DataBind();
        
        

    }
 
  

when i press update button i see this error

 

 

0
bbc_sh
9/24/2008 11:28:50 AM

HI bbc_sh,

The code is correct. What i can suggest is to debug the code using breakpoint and tell me where exactly error is comming. I would like to know the line. I tried to find out. The reason could be following. Please visit the links so you will know whats happening.

http://niravbhattsai.blogspot.com/2007/10/invalid-postback-or-callback-argument.html

 http://forums.asp.net/t/922994.aspx    ------ IMP LINK !!! ...do the changes accordingly.

 Eventhough if it fails to work do this:

By the time you can comment last two lines (where we are choosing datasource and binding the gridview) and check the error....mostly error should go.

Thanks.

0
abhishek_damle
9/24/2008 12:04:16 PM
Reply:

Similar Artilces:

Sending email via the port 465 of Gmail don't work! I don't know if there are bugs in .NET.
I use the following code to send  email via  Gmail , it works well when I use client.Port = 587, but it doesn't work if I use client.Port = 465 !  but If I use email client software such as Outlook Express with port 465, it woks well, I don't know if there are bugs in .NET.     protected void btnSend_Click(object sender, EventArgs e)    {        //Add Async="true" to .ASPX page         SmtpClient client=new SmtpClient();     ...

I love Firefox! But I just got a new computer with Windows 7, and half the time Firefox wouldn't work for about 3 weeks. It's working now, but I don't know for how long. Please make sure you are comp
Name: Product: Firefox Summary: I love Firefox! But I just got a new computer with Windows 7, and half the time Firefox wouldn't work for about 3 weeks. It's working now, but I don't know for how long. Please make sure you are compatible with Windows 7. Thank you. Comments: See above. Browser Details: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.9) Gecko/20100824 Firefox/3.6.9 From URL: http://hendrix.mozilla.org/ Note to readers: Hendrix gives no expectation of a response to this feedback but if you wish to provide one you must BCC (not CC) the sen...

this site didn't open, don't know if it's about the browser or not
Name: FADI Email: fadiffaatyahoodotcom Product: Firefox Release Candidate Summary: this site didn't open, don't know if it's about the browser or not Comments: no details Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9) Gecko/2008051206 Firefox/3.0 From URL: http://hendrix.mozilla.org/ ...

Two things,Why do I keep getting emails that I don't want. can't get rid of them? Why can't I get my email when I am out of town? Or can I?
Name: Ed Leech Email: ELCraftatzoominternetdotnet Product: Thunderbird Summary: Two things,Why do I keep getting emails that I don't want. can't get rid of them? Why can't I get my email when I am out of town? Or can I? Comments: I am getting frusted with all the emails coming in that I do not want, I am using the tools to get thme out but they keep coming. They just use different names or whatever. I have been thinking of just switching to something else but my business intrusts know this email and it is tooo confusing to change. Every time I go out of town on bu...

superreview granted: [Bug 264442] plugin finder can't find anything for windows media. : [Attachment 162212] Don't assume we don't have a plugin before getting a responce from the network.
Boris Zbarsky <bzbarsky@mit.edu> has granted Johnny Stenback <jst@mozilla.jstenback.com>'s request for superreview: Bug 264442: plugin finder can't find anything for windows media. https://bugzilla.mozilla.org/show_bug.cgi?id=264442 Attachment 162212: Don't assume we don't have a plugin before getting a responce from the network. https://bugzilla.mozilla.org/attachment.cgi?id=162212&action=edit ------- Additional Comments from Boris Zbarsky <bzbarsky@mit.edu> r+sr=bzbarsky, I guess.. Is ther a reason mIsBrokenPlugin is write-only? ...

superreview requested: [Bug 264442] plugin finder can't find anything for windows media. : [Attachment 162212] Don't assume we don't have a plugin before getting a responce from the network.
Johnny Stenback <jst@mozilla.jstenback.com> has asked Boris Zbarsky <bzbarsky@mit.edu> for superreview: Bug 264442: plugin finder can't find anything for windows media. https://bugzilla.mozilla.org/show_bug.cgi?id=264442 Attachment 162212: Don't assume we don't have a plugin before getting a responce from the network. https://bugzilla.mozilla.org/attachment.cgi?id=162212&action=edit ...

Messages don't always get sent if computer's date isn't right
I've found that if my computer's date is off by more than some unknown amount, when I send a message it doesn't always really get sent. Yet I don't get an error indication, and the message appears in my Sent folder right along with the rest of my sent messages. Anyone know how much my computer's date can differ from true time before this problem appears? Some of my messages to newsgroups had their dates off by two years, but still got posted. A few minutes ago, I sent two eamils to myself, one with my computer set to September 28, 2009 (one month error)...

Flash don't show up/youtube don't work
Name: Alexandre Torres Product: Minefield Summary: Flash don't show up/youtube don't work Comments: I'm using the windows 64 bits version of firefox 3, and flash plugin don't seem to work...even restarting firefox. Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.2 x64; en-US; rv:1.9a1) Gecko/20061007 Minefield/3.0a1 ...

When exiting a video playing with .wmv, you click the upper right "x" and you don't get back to the web homepage, you get stuck with a white, empty page. Explorer doesn't do that. It's an irritant f
Name: David Morgan Email: Dave_at_NewsCarsonCity.com Product: Firefox Summary: When exiting a video playing with .wmv, you click the upper right "x" and you don't get back to the web homepage, you get stuck with a white, empty page. Explorer doesn't do that. It's an irritant for those of us who want to use Mozilla, period. Comments: See summary. Browser Details: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ...

superreview granted: [Bug 340083] Crash [@ nsSVGPathGeometryFrame::GetCanvasTM] with image in title in defs : [Attachment 224205] don't create frames for content we don't handle, don't invalidate nond
Robert O'Callahan (Novell) <roc@ocallahan.org> has granted superreview: Bug 340083: Crash [@ nsSVGPathGeometryFrame::GetCanvasTM] with image in title in defs https://bugzilla.mozilla.org/show_bug.cgi?id=340083 Attachment 224205: don't create frames for content we don't handle, don't invalidate nondisplay content https://bugzilla.mozilla.org/attachment.cgi?id=224205&action=edit ...

I can't get no cache, I can't get no cache. 'Cause I try and I try and I try and, I can't get no, I can't get no cache.
I have fiddled out for days tinkering with the setting in about:config trying to get FireFox 12 to use the disk cache. I have NOT found the trick. This One Trick Pony ain't doing it like it used to up until recently. Pray tell anyone, What information can I share that will point a knowledgeable person to aid me in getting FF to disk.cache? I have also tried restarting FF with add-ons disabled, (There were NOT too many to do this to, so it was quick and easy) Here is my stab at trying to convey the information that MIGHT govern matters Using about:cache Informatio...

Re: I can't get no cache, I can't get no cache. ' Cause I try and I try and I try and, I can't get no, I can't get no cache.
<div>please com e see me about htis<br /> <br /> ------- Original Message f= rom the Global Relay Archive -------<br /> From: Hp &lt;ferd@farkel.net&gt;= <br /> To: "support-firefox@lists.mozilla.org" &lt;support-firefox@lists.mo= zilla.org&gt;<br /> Sent: Sun, 27 May 2012 18:30:18 -0700<br /> Subject: I = can't get no cache, I can't get no cache. 'Cause I try and I try and I try = and, I can't get no, I can't get no cache.<br /> <br /></div> <pre class=3D"gr-maex-body-pre&qu...

TWC's online DVR Manager's record options don't work?
Hello. Does any SeaMonkeyers else have a DVR, from TWC that can be accessed remotely through https://dvr.timewarnercable.com/tvListings/? If so, then are you able to click on all record options (change to don't delete and series)? They don't seem to work in two/2 of my SeaMonkey v2.26.1 web browsers on two/2 different machines (Debian and Windows XP Pro SP3). It worked fine in Internet Explorer v8.0 and Firefox/Iceweasel v31 though. Thank you in advance. :) -- "A centipede is an ant made to Canadian/government specs." --unknown /\___/\ Ant(Du...

superreview granted: [Bug 245619] Don't bother converting result of scripts into a string when the caller doesn't care about the value. : [Attachment 150056] Don't convert the result to a string if t
Brendan Eich <brendan@mozilla.org> has granted Johnny Stenback <jst@mozilla.jstenback.com>'s request for superreview: Bug 245619: Don't bother converting result of scripts into a string when the caller doesn't care about the value. http://bugzilla.mozilla.org/show_bug.cgi?id=245619 Attachment 150056: Don't convert the result to a string if the caller doesn't care about the return value. http://bugzilla.mozilla.org/attachment.cgi?id=150056&action=edit ------- Additional Comments from Brendan Eich <brendan@mozilla.org> Duh, thanks. r+sr=me....

Web resources about - I don't know why my code don't work(it's for shopping cart) - asp.net.getting-started

Resources last updated: 1/15/2016 12:14:00 PM