Reader Level:
Articles

Working with the ObjectDataSource and DateTime Type

By Michael Livshitz on April 07, 2008
This tip will help you to work with the ObjectDataSource, when at least one of the properties of your business object (BO) is type of the DateTime .
  • 0
  • 0
  • 9666

I think all of us have had a few minutes of "respite" between projects, when you can look back and ask yourself: "What a line of the code I have written, which I have searched so for a lot of time? May be I will tell my friend about it !". This is some kind of  "tips", which help us to save time and/or choose more "suitable" decisions for our projects.

 

Let's suppose the following situation: on some form (or user control) you use some GridView control to display and edit some data; you use some ObjectDataSource as DataSourse for your GridView (see fig. 1); your DateUpdate is type of DateTime and you have to perform it as "Day/Month/Year" (for example, "January, twenty third, 2008"will look like this : "23/01/2008", but not "01/23/2008").

 

01.GIF

 

Figure 1.       

 

Now, your ObjectDataSource is configured so, that a method for "update" accept some BO (business object), but not each property of your BO (fig. 2)

 

02.GIF

 

Figure 2.

 

In this case you will get the following error: "Cannot convert value of  parameter …" (even if every your "Culture" is set to "right" region):

 

03.GIF

 

Figure 3.

 

          This is the bug (nothing doing!). There are some ways to bypass this point. One of them is a little "to deceive" our DataSourceObject and allow to work not with DateTime, but just to parse your DateTime type (in itself!):   

 

protected void GridViewGeneral_RowUpdating

    (object sender, GridViewUpdateEventArgs e)

{

    e.OldValues["DateUpdate"] =

        DateTime.Parse(e.OldValues["DateUpdate"].ToString());

}

 

Don't ask me why, but it works (It is very similar to old joke. A little boy come to his father, programmer, and ask: "Why in the morning the Sun is in the East, and in the evening -  in the West". The father, programmer!, answer : "I do not know, but it works! Do not touch it!!! ")


Good luck in programming ! 

 

Michael Livshitz

Michael is a lead programmer for a large technological firm. He has experience working with .NET projects (ASP.NET and Windows) since 2002. Currently used languages are C#, VB.NET, T-SQL and JavaScript. He also has exper... Read more

COMMENT USING

Trending up