1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

ASP.net nested gridview controls

Discussion in 'C#' started by jimrthy, Dec 23, 2005.

  1. #1
    Here's the basic overview:

    I'm working on a site for people to do online restaurant ordering. It's really just a toy site as I teach myself asp.net 2.0.

    One page is the menu, where the customers can actually order. I want to have a category description followed by the items in that category.

    It would be ridiculously simple to hard-code the categories and then add menu items to the database for each category. Which is what I'll probably do, since I get to define/update the requirements by myself.

    I think it would be much nicer to have the categories also stored in the database, and easily editable.

    Right now I have the following two tables:

    categories
    • id
    • name
    • description

    menu_items
    • id
    • category_id
    • name
    • description
    • price

    Where menu_items.category_id links back to categories.id. Just about as straightforward and simple as you can get.

    To display this, I'm trying to nest a couple of gridviews (well, actually a couple of repeaters, but that doesn't seem to really be the issue).

    The outer grid view shows the category. The inner one shows the associated menu items.

    I followed the basic code template from an MSDN article about hierarchical data binding. The way it's joining the tables is making things choke.

    The way I want things to work runs like this:
    select id, name, description, price from menu_items where category_id = @category

    The way it's actually working seems to be more along the lines of:
    select * from menu_items right join categories on menu_items.category_id = categories.category_id

    This leaves NULLs in the data which leads to all sorts of havok when I try to query for them.

    I know MS recommends against doing this, and it's already been more trouble than this self-assigned homework project was worth (except for all the documentation it forced me to dig through, which was part of the point).

    I'm all googled out. Has anyone here dealt with a similar problem? I'm sure that, sooner or later, it will bite me in the real world, since it popped up so quickly on a toy project.

    TIA
     
    jimrthy, Dec 23, 2005 IP
  2. vectorgraphx

    vectorgraphx Guest

    Messages:
    545
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #2
    hmm... try: (example in asp 3.0, but the logic should work similarly in other languages)


    'note: the "order by" clause, because you want to keep your categories together in your recordset
    'also, in your example post, you connected your join as "menu_items.category_id = categories.category_id" where it needs to be "menu_items.category_id = categories.id"

    select menu_items.name name, menu_items.description description, menu_items.price price, categories.name catname, categories.description catdesctiption from menu_items right join categories on menu_items.category_id = categories.id order by categories.name, menu_items.name
    'then query db as you would normally.


    then:

    while not recordset.eof
    'define toggler1 with your category name

    toggler1 = recordset("catname")

    'check toggler1 against toggler2, the category name of the previous record. defined at the bottom of the while not... wend loop. if they match, then you are still within the same category. if not, then you are in a new category - and you can tell your code to draw the category name, description, etc...

    if toggler1 <> toggler2 then
    response.write recordset("catname")&"<br>"
    response.write recordset("catdescription")&"<br>"
    response.write "&nbsp;&nbsp;"&recordset("name")&"<br>"

    else
    response.write "&nbsp;&nbsp;"&recordset("name")&"<br>"
    end if
    'define second variable with category name - to pass to the next record
    toggler2 = recordset("catname")

    'proceed to the next record.
    recordset.movenext
    wend

    is that the sort of logic you're looking for?


    HTH :D

    VG
     
    vectorgraphx, Dec 26, 2005 IP
    jimrthy likes this.
  3. jimrthy

    jimrthy Guest

    Messages:
    283
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Good catch. That's what I get for writing pseudo-code when I'm dead tired.

    That's a good solution, but the rules change completely when you switch from legacy asp to asp.net. Well, maybe they don't, and you're forced to go back to doing things the old-fashioned way. But they should.

    Especially since you're much better off using server side controls, even if you are forced to generate them dynamically.

    I can't believe MS did such a poor job with this, since, really, it's such a basic thing.

    Thanks for trying, though.
     
    jimrthy, Dec 26, 2005 IP
  4. vectorgraphx

    vectorgraphx Guest

    Messages:
    545
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #4
    No problemo - hope you are able to sort it out man!

    VG
     
    vectorgraphx, Dec 26, 2005 IP
  5. jimrthy

    jimrthy Guest

    Messages:
    283
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks. It really is annoying me. I think maybe it's just Windows getting to me. Time to reboot to linux and do some PHP hacking. :)
     
    jimrthy, Dec 26, 2005 IP