gridview bombs on update - converting rowversion string to byte[]?

B

BenG

Hi. I have a gridview control on a web form (asp.net 2.0) that's bound to a
objectDataSource. The objectdatasource which is bound to a class I've
written in the DAL to read and update the database. The grid contains three
columns - an ID (GUID), a name (string) and a rowversion (timestamp), and
the data is passed to/from the page in an instance of a data entity object.
When I open the page the grid loads properly, but if I attempt to edit a
row, it bombs when I click on the UPDATE link with an error message of:
Cannot convert value of parameter 'rowversion' from 'System.String' to
'System.Byte[]' . The column rowversion is stored in the database as
timestamp, and in my business entity as a byte array. My guess is that when
it's passed into the gridview it's converted to a string, but when passing
it back to the DAL, the concersion from String to byte[] isn't working. The
bug is taking place after the rowupdating event, but before the update code
in the DAL gets called.

Any idea what''s going wrong?

Ben
 
W

Walter Wang [MSFT]

Hi Ben,

Can I ask why you map the timestamp field to a byte array rather than
DateTime? Based on my test, a byte array field will not show in the
GridView, how did you declare that? I'm curious to see your complete code
listing.

When you declare the ObjectDataSource in .aspx, you need to declare all the
parameters such as:

<UpdateParameters>
<asp:parameter Name="id" Type="Int32" />
<asp:parameter Name="name" Type="String" />
<asp:parameter Name="rowversion" Type="DateTime" />
</UpdateParameters>

When updating, ObjectDataSource will use this information to pass the
values to your business object's Update method.

I would recommend you use System.DateTime as the data type of the
rowversion.

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

BenG

Hi Walter.

I've read in several places that the best way to store a timestamp column is
to use a byte[], although I'm a beginner in this, so I'm not sure if that's
true. However, storing it as DateTime doesn't work either:

DateTime rowversion;
//dr is a SqlDataReader
rowversion = dr.GetDateTime(dr.GetOrdinal("rowversion"));
//or
rowversion = dr.GetDateTime(dr.GetOrdinal("rowversion")).value;

both yield a System.InvalidCastException error

I created the ObjectDataSource visually. When I inspect the code on the page
I don't see anything about <UpdateParameters>. Here's the declaration:
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetAllInstitutionList"
TypeName="DAL.DALInstitution"
DataObjectTypeName="DataEntities.InstitutionData"
DeleteMethod="DeleteInstitution"
UpdateMethod="UpdateInstitution"></asp:ObjectDataSource>

Here are the colum definitions on the page:
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="InstitutionId" HeaderText="InstitutionId"
SortExpression="InstitutionId" />
<asp:BoundField DataField="InstitutionName" HeaderText="InstitutionName"
SortExpression="InstitutionName" />
<asp:BoundField DataField="rowversion" HeaderText="rowversion"
SortExpression="rowversion" />
</Columns>


Ben
 
W

Walter Wang [MSFT]

Hi Ben,

If you are using SQL Server timestamp field, don't include the field in
your DataSet. A timestamp field contains a unique value, generated by SQL
Server, which is updated whenever that record is updated.

And you are right, in .NET, reading timestamp field needs byte[].



Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

BenG

I figured out how to make this work. Instead of storing the rowversion
column in the gridview, I add the column to the gridviews DataKeyNames
property.

Ben
 
W

Walter Wang [MSFT]

Hi Ben,

Glad to hear that you've solved the problem. By putting the rowversion
field in the DataKeyNames collection, it will also be passed to the methods
of ObjectDataSource.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top