Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 26th, 2008, 07:25 PM
Friend of Wrox
 
Join Date: Jun 2003
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

 
Old October 27th, 2008, 06:44 PM
Friend of Wrox
 
Join Date: Jun 2003
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.







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 05: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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.