Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old October 26th, 2008, 08:25 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default Problem data binding DateTimePicker to object

Hello,

I'm trying to data bind a DateTimePicker to a property of a custom object, like:

Code:
this.dtpDateTimeField.DataBindings.Add("Value", this._typefield, "DateTimeField");
TypeField is the name of the object. "DateTimeField" is the field I'm binding to.

Then I input some data and do an insert into a Jet database.

I have the default value of the date time field in the object set like:


Code:
private System.DateTime _dateTimeField = DateTime.Now;
so the DateTimePicker has a defult value of todays date when the form loads.
Here's the initializing code from the designer:

[code]
this.dtpDateTimeField.Value = new System.DateTime(2008, 10, 26, 0, 0, 0, 0);


If I actually SELECT a date in the DateTimePicker after the form loads, and then
click a Save button that does the insert, all goes well.

However, if I don't actually click on the DateTimePicker and SELECT a date (but just try
and insert the default value), all goes bad. I get:

OleDbException was unhandled, data type mismatch in criteria expression.

The value isn't null anywhere I can see. There seems to be a genuine DateTime value getting
passed around either way. I've even tried adding my own binding and played with formatting and parsing
events:

Code:
Binding b = new Binding("Value", this._typefield, "DateTimeField", true);
this.dtpDateTimeField.DataBindings.Add(b);
b.Format += new ConvertEventHandler(dtpDateTimeField_Format);
b.Parse += new ConvertEventHandler(dtpDateTimeField_Parse);
but no luck.

Any thoughts.

Bob

  #2 (permalink)  
Old October 27th, 2008, 07:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

I believe I've figured it out.

"OleDbException was unhandled, data type mismatch in criteria expression" is thrown for the following reason.

1. DateTime.Now returns a TimeOfDay value of something like 19:18:17.4236421 (note the milliseconds).

2. I'm trying to store a System.DateTime in a Jet database, so I'm using an OleDbCommand object.

3. command.Parameters.AddWithValue("@dateTimeField", typeField.DateTimeField) creates an OleDbParameter object of OleDbType DBTimeStamp (my instance field's default value is DateTime.Now).

4. The Problem: DBTimeStamp has no conception of milliseconds. Instead it has a fraction property that represents billionths of a second ranging from 0 to 999,999,999.

5. The Solution: Change DateTime.Now to DateTime.Today. That lops off the time value, and hence the problematic millisecond value.

After changing the default value of my DateTime field to DateTime.Today, I can databind to the DateTimePicker and insert the default value fine.


 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Data binding to an object in DataGridView control? Spartacus Visual C++ 2005 0 July 12th, 2007 06:10 PM
Problem with binding data in Datagrid Samatha ASP.NET 1.0 and 1.1 Professional 6 December 6th, 2006 09:13 AM
problem in binding data vijjum3 General .NET 1 December 29th, 2004 08:41 AM
Problems with binding the DateTimePicker controls. mmwaikar ADO.NET 2 November 28th, 2004 03:09 AM
Data Binding Problem jbenson001 ASP.NET 1.x and 2.0 Application Design 3 December 18th, 2003 01:03 PM



All times are GMT -4. The time now is 07:47 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.