469,591 Members | 1,650 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,591 developers. It's quick & easy.

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

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
Aug 15 '06 #1
5 5191
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 (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Aug 16 '06 #2
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

"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:eU**************@TK2MSFTNGXA01.phx.gbl...
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 (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Aug 16 '06 #3
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 (wa****@online.microsoft.com, 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.

Aug 17 '06 #4
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

"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:Wc**************@TK2MSFTNGXA01.phx.gbl...
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 (wa****@online.microsoft.com, 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.

Aug 17 '06 #5
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 (wa****@online.microsoft.com, 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.

Aug 18 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by =?Utf-8?B?QmFidU1hbg==?= | last post: by
7 posts views Thread by =?Utf-8?B?cGF0cmlja2RyZA==?= | last post: by
4 posts views Thread by tim.cavins | last post: by
1 post views Thread by janetb | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.