views:

44

answers:

1

I have designed a user control to present a list of questions to the user. This "question list" control just contains a repeater. There are a wide range of types of question and form fields that must be used to answer the question, so they are implemented as a range of "question input" controls which are dynamically loaded into the ItemTemplate. Each question input control contains a button to submit the answer to that question. The answer submitted often changes the list of questions, so after any answer is submitted and saved to the DB I want the question list to be rebound.

All of the above is working, except that when the question list is rebound after an answer is submitted, the new list contains duplicated rows and also a row goes missing, as follows:

Original list:

Q1
Q2 <---- Save button clicked on this question
Q3
Q4

New list (Q2 duplicated & Q4 missing):

Q1
Q2
Q2
Q3

The way I got the answer submission on the dynamically loaded question input control to rebind the question list was like this:

In the question list control's Page_Load, if Page.IsPostBack is true I call Repeater.DataBind(). This recreates the question input controls and lets their button click events fire. The first challenge was the fact that these events fired AFTER the parent control's Page_Load had completed, but I found a way to create a "ForceReload" property on the parent control that the question input control could set to True in its button click event. So, in the question list control's Page_PreRender I check if ForceReload=true and if so call Repeater.DataBind() a second time.

It is in this 2nd databind that the strangeness happens. If I step through the code I can see the DataItem corresponding to the question that was submitted coming up twice. However the SQL query being run the 2nd time is the same as was run the 1st time and the records returned are the same (i.e. there are no duplicates in the results the 2nd time).

I imagine I need to do some kind of repeater "reset" before databinding it the 2nd time, but can't see any function for doing that.

Possible cause .... the unique key for the question changes between the 1st databind and the 2nd. I know that sounds wierd, but that is how the DB works - a qiven question might have a unique ID of "-123" in its "unanswered" state and then "123" in its "answered" state. If I force the ID to staty the same the problem doesn't arise. The changing ID is to do with the fact that the datasource is a view rather than a table,and anyway I can't do anything about it. And why would the repeater care when it has no idea what the unique ID is??

UPDATE: After more testing, its definately a LINQ problem rather than a repeater or SQL problem. Here's a simple example:

Data retrieved on 1st binding:

QuestionID   QuestionNumber    Answer
----------   --------------    ------
-1           1                 null
-2           2                 null
-3           3                 null
-4           4                 null

Then question #2 is submitted, and list rebound. Data retrieved on 2nd binding:

QuestionID   QuestionNumber    Answer
----------   --------------    ------
-1           1                 null
2            2                 My answer
-3           3                 null
-4           4                 null

So the data is exactly correct. However this is transformed by LINQ into 4 objects with the following properties:

QuestionID   QuestionNumber    Answer
----------   --------------    ------
-1 (OK)      1 (OK)            null (OK)
2  (OK)      2 (OK)            My answer (OK)
-3 (OK)      2 (wrong)         My answer (wrong)
-4 (OK)      3 (wrong)         null (OK)

So, in each object the key property (QuestionID) is correct, however in the objects after the question that got updated, the other properties are pulled from the prior record!

A: 

First of all, the person that designed a db that changes the unique ID of the record when it is updated or answered belongs in another industry. I suggest they become a fantasy writer or quatumn physicist, although if they try the latter they might end up trying to save the record in three states at once. They will probably argue that -123 and 123 are different states of the same id, but they are wrong. They are different numbers and are treated as such by every computer system that I am aware of. They should have had a boolean flag called IsAnswered or something. But you say that you can't change this, so I will need more info to answer your question.

Essentially, the id is probably the root of your problem as you suggest. Could you post the code behind for your control? I would like to see how you are updating the DB.

Daniel Dyson
Its definately a LINQ problem rather than a SQL problem, so I don't think my update code is that relevant. I added more info to my original post above (see UPDATE).
Laurence
If QuestionNumber is unique and sequential, can't you treat that as your unique Id, selecting and updating based on that?
Daniel Dyson
No. I dumbed down the data to make it easier to explain - its actually not unique.
Laurence
Note: the exact same problem occurs when the Repeater is swapped for a DataList
Laurence