locked
Looking for the best method - please help! RRS feed

  • Question

  • User1063851105 posted

    Hi,
    I'm building an online shop, but have run into a problem.
    When the user click that they want to add a product to their basket, i add the product ID to an array that is held on the session.
    This all works great and i display how many items they have selected etc..

    The problem comes when i get to the checkout page.
    Whats the best way of displaying the items that the user has selected?
    should i use a datagridview? if so, how do i pass the array into the SQL in order to get all the data rows that i need?

    should i use a repeater? this gives me the same problem as above though.

    I just cant seem to work out how to get the values fro the array and then run the sql for each ID to get the information to show on the screen.

    I could loop through the array using each ID as a parameter in the SQL but then where do i put the data? in a dataset? a datatable?

    Please help!

    Thanks

    Thursday, March 4, 2010 9:45 AM

Answers

  • User1063851105 posted

     Am i missing something silly here?

    i have a repeater set up with its data source id set to sqldatasource

    i have the following code

    Dim count As Integer = basket.GetLength(0)Dim i As Integer = 0Dim sql As String = "SELECT prod_title, prod_number, prod_size, prod_designer FROM scandi_product WHERE prod_id IN ("

    While i < countIf i > 0 Then sql = sql & ","
    sql = sql & basket(i)
    i = i + 1
     
    End While

    sql = sql & ") "SqlDataSource.SelectCommand = sql

     

    which passes the sql to the datasource (and i've checked it and it works), but then nothing is displayed to the page?

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 4, 2010 11:33 AM
  • User-952121411 posted

    While i can see its not the best of solutions, i think (if i can get it woking) it will be sufficient for my needs.
     

    I agree partially.  If you are truely creating an outwardly facing "Shopping Cart" style web application that accepts payments for general use, you will want to make sure that your code is solid and stable.  The last thing you will want is incorrectly applied payment processing, or errors in the shopping cart.  You could have a real mess. 

    The good news is that a shopping cart web app is probably almost as old as the web itself (or close to it; you understand the point I am making).  Therefore there are a ton of tutorials and starter kits for creating such a site.  I know at this point you do not want to start over, but since this is the 'Architecture' forum I figure I would speak more to the overall design.  For speceific code issues with a SQLDataSource you may want to re-post in the following forum:SQL Server, SQL Server Express, and SqlDataSource Control

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 4, 2010 11:47 AM

All replies

  • User-110466603 posted

    You could also try storing a small custom dataset into session instead of just the array.  You could store just the information you want to display on the checkout page (price, name, description) in addition to the product ID. I don't think that would be too much to store considering the average person is probably only going to be buying a few products at a time. 

    Otherwise, if you want to stick with the ID's you could utilize the IN keyword in SQL.  You would have to "dynamically" create this SQL statement to say something like SELECT * FROM Products WHERE ProductID IN(1,2,3,4,5);



    Thursday, March 4, 2010 9:54 AM
  • User1063851105 posted

    Thats great.

    Thanks for that.

    I can use a repeater to display the data and pass in the sql to the SqlDataSource as you have described above.

    If it works, this will be just the solution i was looking for. :)

    thanks again for the quick response. 

    Thursday, March 4, 2010 10:25 AM
  • User-952121411 posted

    I think a better solution is to create a 'Products' class, with the properties applicable like ID, Description, Quantity, Price, etc.  Then for each item added to the shopping basket, a new object of type 'Product' is created and then added to a List(of Product) Generic object collection. Iterating though a collection like this is easy, and so is using it as a DataSource for a GridView control.  It can be directly bound manually or using an ObjectDataSource.  Working with objects and collections will present a much cleaner solution, then piece parting together a lot of individual random types (i.e. Arrays, DataSets, etc.), and is more along the lines of good OOP.  You may want to read over the following information that will help:

    .NET Object Collections Using Generics 101:

    http://allen-conway-dotnet.blogspot.com/2009/11/net-object-collections-using-generics.html

    Hope this helps! Smile

    Thursday, March 4, 2010 10:45 AM
  • User1063851105 posted

    I'll give this a go if i cant get the above method working.

    While i can see its not the best of solutions, i think (if i can get it woking) it will be sufficient for my needs.

    Thanks again.

     

    Thursday, March 4, 2010 11:16 AM
  • User1063851105 posted

     Am i missing something silly here?

    i have a repeater set up with its data source id set to sqldatasource

    i have the following code

    Dim count As Integer = basket.GetLength(0)Dim i As Integer = 0Dim sql As String = "SELECT prod_title, prod_number, prod_size, prod_designer FROM scandi_product WHERE prod_id IN ("

    While i < countIf i > 0 Then sql = sql & ","
    sql = sql & basket(i)
    i = i + 1
     
    End While

    sql = sql & ") "SqlDataSource.SelectCommand = sql

     

    which passes the sql to the datasource (and i've checked it and it works), but then nothing is displayed to the page?

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 4, 2010 11:33 AM
  • User-952121411 posted

    While i can see its not the best of solutions, i think (if i can get it woking) it will be sufficient for my needs.
     

    I agree partially.  If you are truely creating an outwardly facing "Shopping Cart" style web application that accepts payments for general use, you will want to make sure that your code is solid and stable.  The last thing you will want is incorrectly applied payment processing, or errors in the shopping cart.  You could have a real mess. 

    The good news is that a shopping cart web app is probably almost as old as the web itself (or close to it; you understand the point I am making).  Therefore there are a ton of tutorials and starter kits for creating such a site.  I know at this point you do not want to start over, but since this is the 'Architecture' forum I figure I would speak more to the overall design.  For speceific code issues with a SQLDataSource you may want to re-post in the following forum:SQL Server, SQL Server Express, and SqlDataSource Control

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 4, 2010 11:47 AM
  • User1063851105 posted

     Panic over.

    I rebooted and suddenly it all started working.

    not quite sure what the problem was but its all good now.

    Thanks again for all your help.

    Smile

    Thursday, March 4, 2010 12:06 PM
  • User-967169866 posted

    Please do not by any means deploy to production anything that looks like Dim sql As String = "select..."

    That will only lead to drama that you don't wish to experience.  Make a stored procedure.  

    Using the IN keyword has it's benefits, but you're better off tuning a stored procedure to retrieve products based on the id in an exact form.

    Here's the deal.  You're going to tear up your database runtime memory by executing that query over and over again.  You're also going to tear up your web servers GC by concatenating strings like that in a loop.  This method will "work" or appear to work, but it is definitely the ghetto fabulous duct tape method.  It will fall apart.

    If you want, you can make the parameter of the stored procedure be a comma delimited string, and have the procedure parse the string, and make a table variable to fill with the appropriate data, and return that.


    Sunday, March 7, 2010 4:14 AM