I think what you need is a fresh approach here. I think you're making life harder for yourself.
I'd be a little worried that your logic is tied up to 25 steps since this would prove quite inflexible should they decide to change that requirement. I know that's a not priority for you, but you could approach this problem in two ways which would give the
solution you're looking for, plus add that bit of flexibility there:
1)have a steps table on your database with an id and a StepNumber field of whatever is a relevant name for it. then just build the relationship on a separate table much like the one you have now by having a surrogate key, step id, user id and progress. On
the client code query a join on the steps lookup table and the relationship table. It should always pull all available steps and the associated data for that user and you can populate all that in one loop. this way not only your code if vastly simplified but
you also have the option of adding more steps or archiving steps by adding a column and filtering them out in a query in the future, etc.
2)If you for some reason cannot alter the database schema or prefer to constraint your solution to the backend code, then the simplest and cleanest way of tacking this would be to first set up your data structure then attempt to "render" it. I think one
of the problems you have here is that you are mixing data processing with what you ultimately want to display on the screen.
Create a Step object containg Id, StepNumber, UserId and Progress properties. initialize a collection such as List<Step>. Now loop through the database records and create a Step object with each iteration. match the step number with the loop counter by checking
for if(i == row["step"]) . if it matches then populate the object with the data from the database otherwise give it defaults (zero to progress and the set the user id to the one in question). Now create another method that takes the List<Step> as a parameter
and call it RenderSteps or something of the sort. On that method create your array and do a foreach on each Step in the collection setting the html with values extracted from the Step object.
Incidentally, if you have any control over the architecture I would actually change the data layer approach to be more OOP and only return objects and then pass the List<Step> to the web form (I'm guessing this is web forms?) and let it loop through it and
do the rendering. Also, if at all possible, I would consider fitting in Ling-to-Sql or EF which would bring massive improvements to your data layer.
Let me know if any of this makes sense or if nothing really helps I can try to help you figure something else out.
mattGuimaUK
Member
2 Points
1 Post
Re: Assign a database field value to a menu item (step)
Apr 01, 2012 02:25 PM|LINK
Hi corrado,
I think what you need is a fresh approach here. I think you're making life harder for yourself.
I'd be a little worried that your logic is tied up to 25 steps since this would prove quite inflexible should they decide to change that requirement. I know that's a not priority for you, but you could approach this problem in two ways which would give the solution you're looking for, plus add that bit of flexibility there:
1)have a steps table on your database with an id and a StepNumber field of whatever is a relevant name for it. then just build the relationship on a separate table much like the one you have now by having a surrogate key, step id, user id and progress. On the client code query a join on the steps lookup table and the relationship table. It should always pull all available steps and the associated data for that user and you can populate all that in one loop. this way not only your code if vastly simplified but you also have the option of adding more steps or archiving steps by adding a column and filtering them out in a query in the future, etc.
2)If you for some reason cannot alter the database schema or prefer to constraint your solution to the backend code, then the simplest and cleanest way of tacking this would be to first set up your data structure then attempt to "render" it. I think one of the problems you have here is that you are mixing data processing with what you ultimately want to display on the screen.
Create a Step object containg Id, StepNumber, UserId and Progress properties. initialize a collection such as List<Step>. Now loop through the database records and create a Step object with each iteration. match the step number with the loop counter by checking for if(i == row["step"]) . if it matches then populate the object with the data from the database otherwise give it defaults (zero to progress and the set the user id to the one in question). Now create another method that takes the List<Step> as a parameter and call it RenderSteps or something of the sort. On that method create your array and do a foreach on each Step in the collection setting the html with values extracted from the Step object.
Incidentally, if you have any control over the architecture I would actually change the data layer approach to be more OOP and only return objects and then pass the List<Step> to the web form (I'm guessing this is web forms?) and let it loop through it and do the rendering. Also, if at all possible, I would consider fitting in Ling-to-Sql or EF which would bring massive improvements to your data layer.
Let me know if any of this makes sense or if nothing really helps I can try to help you figure something else out.
Regards,
Matt