No matter what application we're using, when we run into a problem, it becomes necessary to do a little fact-finding, trying to discover what is causing the problem. XMod Pro is no different in this regard. Knowing where to look can greatly speed up the amount of time it takes to resolve an issue.
I received this question from a customer and not only wanted to post the solution but also provide a couple of things to look for should you encounter similar problems:
"When I attempt to edit any record in my list, the edit form always shows the last record in the table. I followed the examples in creating the forms and templates and have checked my work many times over."
The customer then supplied me with the form and template that was being used. The first thing I did was scan the Template for the Edit button. I wanted to ensure the Edit button (in this case it was an <xmod:EditImage>
control) was properly formed:
<ItemTemplate>
...
<xmod:EditImage imageurl="~/images/edit.gif">
<Parameter Name="ContractorID" Value='[[ID]]'/>
</xmod:EditImage>
...
</ItemTemplate>
The customer was also using the Alternating template so I checked that as well:
<AlternatingItemTemplate>
...
<xmod:EditImage imageurl="~/images/edit.gif">
<Parameter Name="ContractorID" Value='[[ID]]'/>
</xmod:EditImage>
...
</AlternatingItemTemplate>
Both of the tags look fine. They're well-formed with no syntax issues. The value attribute of the <Parameter>
tag is using a Field Token and that is properly delimited by single quotes. More importantly, it provides us with vital information we can use in the rest of our debugging. The tags are passing a parameter to the EditForm called "ContractorID". The value of the parameter is set to the "ID" column of the current record.
The customer isn't reporting any errors being thrown when trying to edit the record. In fact, a record seems to be retrieved - it's just the last record each time. So, this step probably isn't required but since we're looking at the template, let's just check the <ListDataSource>
to ensure the "ID" column is being retrieved:
<ListDataSource CommandText="EXEC dbo.GetContractorList" />
Well, since the customer is calling a stored procedure, there's no immediate confirmation that the "ID" column is being retrieved. The next step would be to look at the stored procedure and verify "ID" is being returned from the procedure. Since the ID retrieval doesn't appear to be a problem in our case, we can move on and look at the EditForm.
Specifically, we need to look at the <EditForm>
's <SelectCommand>
. This is the command that is executed when the EditForm is loaded. It's job in an edit form is to retrieve a single record for editing.
<EditForm>
...
<SelectCommand CommandText="SELECT [ID], [Company], [Address1], [City], [State], [Zip],
[Phone], [Fax], [Email], [DateModified]
FROM [dbo].[Contractor]" />
...
</EditForm>
Now we see the problem. This command does not limit the records being retrieved. It simply pulls every record from the Contractor table. Instead, it should be using the information passed from the Edit button in the template. So, let's rewrite the <SelectCommand>
:
<SelectCommand CommandText="SELECT [ID], [Company], [Address1], [City], [State], [Zip],
[Phone], [Fax], [Email], [DateModified]
FROM [dbo].[Contractor]
WHERE [ID]=@ContractorID" />
The only change I made is the last line above. This uses the same name as the parameter in the <xmod:EditImage>
that we saw earlier in this article.
To sum up, here are the key items to look at:
- The Edit button (or image or link) in your template that you're clicking to call up the Edit form.
- The Edit button must contain a parameter that will be used by your
<EditForm>
's <SelectCommand>
.
- Verify the value of the Edit button is actually being retrieved in the template.
- Look at the
<EditForm>
's <SelectCommand>
tag. The CommandText should use the Edit button's parameter to limit the records being returned (usually via a WHERE clause)
- The parameter name in the
<SelectCommand>
's CommandText
must match the parameter name in the Edit button.