locked
Trying to populate a list using SQL & Razor RRS feed

  • Question

  • User-1276693286 posted

    I currently have a foreach statement that populates the form on a webpage if anything exists in its slot in the database. However, since I now seem to have 32 different ShirtTypes I need to switch it into some sort of dictionary or list format and was wondering if I could get a nudge in the right direction.

    Current Code:

    foreach (var i in ObjectResults) {
    
    if (i.ObjectResults == "Shirt1"){
    Color1 = r.ColorID; 
    Comment1 = r.CommentID;
    }
    
    if (i.ObjectResults == "Shirt2"){
    Color2 = r.ColorID; 
    Comment2 = r.CommentID;
    }

    Basically what the code does is, it fills in the appropriate text boxes with information in page load if anything exists. Any ideas how I can wrap my head around changing this into a list format?

    Thanks in advance.

    EDIT: Basically what I would like is something like this (is it even possible?):

    foreach (var i in ObjectResults) {
    Color[] = r.ColorID;
    Comment[] = r.CommentID
    }
    Friday, August 22, 2014 1:06 PM

All replies

  • User-821857111 posted

    What is ObjectResults? What does 'i' represent? What is the relationship with comments and colors? What about 'r'? Where does that come from? 

    It;s very difficult to help when the code you have posted is meaningless and without any explanation.

    Friday, August 22, 2014 2:32 PM
  • User-1276693286 posted
    
    

    The full code is as follows:

    var Objects = "select statement"

    var ObjectResults = db.Query(ObjectResults, RecordID).ToList();

    foreach (var i in ObjectResults) {
    
    if (i.ObjectResults == "Shirt1"){
    Color1 = r.ColorID; 
    Comment1 = r.CommentID;
    }
    
    if (i.ObjectResults == "Shirt2"){
    Color2 = r.ColorID; 
    Comment2 = r.CommentID;
    }
    Friday, August 22, 2014 2:34 PM
  • User-821857111 posted

    So ObjectResults is a string?

    Friday, August 22, 2014 2:34 PM
  • User-1276693286 posted

    That is correct. Sorry if my posts/messages are out of whack. I'm interning at a position and pretty nervous and all. :)

    Friday, August 22, 2014 2:37 PM
  • User-821857111 posted

    Can you post the actual select statement?

    Friday, August 22, 2014 2:45 PM
  • User-1276693286 posted

    SELECT Res.ColorID, Res.CommentID, Res.ShirtID" +
    " FROM ProductsDB.dbo.tblResultsRecord Rec INNER JOIN ProductsDB.dbo.tblFormResult Res ON Rec.RecordID = Res.RecordID" +
    " WHERE Rec.RecordID = CAST(@0 AS uniqueidentifier)

    Friday, August 22, 2014 2:50 PM
  • User-821857111 posted

    OK, So when you query the database, you end up with a collection already. It's the ObjectResult variable. It contains a collection of dynamic items with ColorID, CommentID and ShirtID properties. Why do you want to transfer it to another collection/list?

        

    Friday, August 22, 2014 2:59 PM
  • User-1276693286 posted

    Cause the way we have it setup the textboxes are filled on the if statements listed above. I'd like to dynamically do this if possible I'm just trying to understand the pseudo-code behind it.

    Thanks.

    BTW: The code above is used to first populate all the data on the form if any previous textboxes were filled in. Which is why I can't do this as a simple foreach statement because some results wouldn't be in order. (Like numbers 1,3,5 might have an answer, but, when displayed it'd fill in 1,2,3) <-- when I do the foreach loop.

    Friday, August 22, 2014 3:01 PM
  • User-821857111 posted

    Hmmm. This is a bit like the Slow Reveal....

    What textboxes? Can you provide the code for  the form?

    Friday, August 22, 2014 3:18 PM