?
Solved

Trying to retrieve a value from my datalist control to pass to a sproc

Posted on 2005-04-28
11
Medium Priority
?
274 Views
Last Modified: 2010-04-16
Here is what I have. I am already getting some info from my datalist and passing it on. I need one more bit of info. I need to get info from a label called "Size" in MyList. I need to pass the variable "sizes" to both cart.RemoveItem and cart.UpdateItem. I have tried a bunch, but I am stuck.

 void UpdateShoppingCartDatabase() {

            ASPNET.StarterKit.Commerce.ShoppingCartDB cart = new ASPNET.StarterKit.Commerce.ShoppingCartDB();

            // Obtain current user's shopping cart ID
            String cartId = cart.GetShoppingCartId();

            // Iterate through all rows within shopping cart list
            for (int i=0; i < MyList.Items.Count; i++) {

                // Obtain references to row's controls
                TextBox quantityTxt = (TextBox) MyList.Items[i].FindControl("Quantity");
                CheckBox remove = (CheckBox) MyList.Items[i].FindControl("Remove");
                        
                      

                // Wrap in try/catch block to catch errors in the event that someone types in
                // an invalid value for quantity
                int quantity;
                        
                try {
                    quantity = Int32.Parse(quantityTxt.Text);

                    // If the quantity field is changed or delete is checked
                    if (quantity != (int)MyList.DataKeys[i] || remove.Checked == true) {

                        Label lblProductID = (Label) MyList.Items[i].FindControl("ProductID");
                                                                  
                        if (quantity == 0 || remove.Checked == true) {
                            cart.RemoveItem(cartId, Int32.Parse(lblProductID.Text));
                        }
                        else {
                            cart.UpdateItem(cartId, Int32.Parse(lblProductID.Text),quantity);
                        }
                    }
                }
                catch {
                    MyError.Text = "There has been a problem with one or more of your inputs.";
                }
            }
        }
0
Comment
Question by:dbruyere
  • 7
  • 3
11 Comments
 
LVL 9

Accepted Solution

by:
cyberdevil67 earned 1500 total points
ID: 13892157
Hi dbruyere,

 if you use this

  catch (Exception xx)
  {
    MyError.Text = xx.message;
  }

  What is the exact error message, I am betting its because your got an int going into a string.

  Which means this line is erroring..

  TextBox quantityTxt = (TextBox) MyList.Items[i].FindControl("Quantity");

  and you might need to do this

  TextBox quantityTxt = (TextBox) MyList.Items[i].FindControl("Quantity").ToString();

Cheers!
0
 
LVL 1

Author Comment

by:dbruyere
ID: 13892259
Nothing in that bit of code actuall causes an error. What I need to do is add something. I need to get the value from the label "Size" in my datalist (MyList). The code I posted is working, I just don't know to get the string value of the label "Size".

So I need this bit:

 if (quantity == 0 || remove.Checked == true) {
                            cart.RemoveItem(cartId, Int32.Parse(lblProductID.Text));
                        }
                        else {
                            cart.UpdateItem(cartId, Int32.Parse(lblProductID.Text),quantity);
                        }
To look like this:

 if (quantity == 0 || remove.Checked == true) {
                            cart.RemoveItem(cartId, Int32.Parse(lblProductID.Text), sizes);
                        }
                        else {
                            cart.UpdateItem(cartId, Int32.Parse(lblProductID.Text),quantity, sizes);
                        }
I don't know how to get the size value (large, medium, etc...) from my datalist to that "sizes" variable. Let me know if I have made any sense.

I'd show you what I tried, but you'd only laugh at me.
0
 
LVL 1

Author Comment

by:dbruyere
ID: 13905941
I think I may need to explain better. Here you go. The original post contains working code, it is just missing something. Here is what I am trying to do something like this, only the code doesn't work the line with the  ** next to it is what I need to get working:

 try {
                    quantity = Int32.Parse(quantityTxt.Text);

                    // If the quantity field is changed or delete is checked
                    if (quantity != (int)MyList.DataKeys[i] || remove.Checked == true) {

                        Label lblProductID = (Label) MyList.Items[i].FindControl("ProductID");
             ***** Label lblSizes = (Label) MyList.Items[i].FindControl("Size");***** need to get this passed into the "sizes" variable below. the datatype for the db is nvarchar.
z
                                                                                                                    
                        if (quantity == 0 || remove.Checked == true) {
                            ****cart.RemoveItem(cartId, Int32.Parse(lblProductID.Text), sizes);****
                        }
                        else {
                            ****cart.UpdateItem(cartId, Int32.Parse(lblProductID.Text),quantity, sizes);****
                        }
                    }
                }
                catch(Exception xx) {
                    MyError.Text = xx.Message;

As you can see, I could really use some help. Thanks in advance.
                }
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:dbruyere
ID: 13905996
I added the following to the page above and it didn't help

string sizes = lblSizes.ToString();

Here is the page in question if you would like to have a look, just add some stuff to the cart and try to update or remove items:

http://69.230.208.193/burpshappen/ShoppingCart.aspx
0
 
LVL 1

Author Comment

by:dbruyere
ID: 13907005
I actually think the code is working but it is a problem with comparing to the database. I think I am doing it wrong. Any help would be great.

Here is what I am ultimately trying to accomplish (the stored procedure):

CREATE Procedure CMRC_ShoppingCartUpdate
(
    @CartID    nvarchar(50),
    @ProductID int,
    @Quantity  int,
    @Sizes nvarchar(10)
)
AS

UPDATE CMRC_ShoppingCart

SET
    Quantity = @Quantity

WHERE
    CartID = @CartID
  AND
    ProductID = @ProductID
  AND
    Sizes = @Sizes
GO
0
 
LVL 9

Expert Comment

by:cyberdevil67
ID: 13907021
The query looks fine.


But if the try catch is not erroring then why the question!!
0
 
LVL 1

Author Comment

by:dbruyere
ID: 13907035
Have a look at the link to the page in question. something is not working. Oh, and when I originally posted the question, I was getting an error. Got that fixed. But the problem continues.

Here is the link again. Add some stuff to the cart and change quantities  and to an update or remove. Youll see what I mean.



http://69.230.208.193/burpshappen/ShoppingCart.aspx
0
 
LVL 9

Expert Comment

by:cyberdevil67
ID: 13907081
Yeah nothing happens, it doesn't update and I don't get an error.

But considering the original question was on the error, this is now a sperate question. But as I am not getting an error then it appears that we have helped you solve your intial question.
0
 
LVL 1

Author Comment

by:dbruyere
ID: 13907128
Actually, I don't know that the question is solved. I used the following bit of code to get the errors to go away. But the objective has not been accomplished. I can close out the question and ask a new one, but I don't know what to ask. Is it a post problem, or a SQL issue? I don't know.


    Label lblSizes = (Label) MyList.Items[i].FindControl("Size");
           
    string sizes = lblSizes.ToString;
                                                                                                                    
                       
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13910809
Where are you still having a problem (I don't understand all the history from above)?

Bob
0
 
LVL 1

Author Comment

by:dbruyere
ID: 13910848
I just realized that it may just be a problem with my string comparison. I guess I have passes the value along properly, I just don't know how to make the comparison with the value in the size column of the db.

I need the cart to update where productid = x and sizes = x. I am not sure how to do it. But I think it's an entirely different problem than the original.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question