3 Replies - 12334 Views - Last Post: 10 March 2009 - 05:44 AM Rate Topic: -----

#1 mjwood0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-March 09

Attempting to insert a date from a DateTimePicker to a SQL Database

Posted 09 March 2009 - 01:25 PM

I've googled, searched and am still totally confused. I've seen so many examples that claim to work, but I'm still stumped.

What I'm attempting to do is have the user select a date in a DateTimePicker object. I'd like to insert this into the created SQL Database.

My DB is really simple. It looks like this:

DateID - Integer, Primary Key
Date - DateTime Type
Hours - Integer (to be filled in later, zero for now)

Here is what I'm attempting:
try
{
	 timeKeeperDataSet.DatesRow newDateRow = timeKeeperDataSet.Dates.NewDatesRow();
				   
	 newDateRow.Date = dateTimePicker1.Value.Date;
	 newDateRow.Hours = 0;

	 int rowsAffected = datesTableAdapter.Update(newDateRow);

	 if (rowsAffected > 0)
	 {
		   MessageBox.Show("New Date Added");
	 }
	 else
	 {
		   MessageBox.Show("Problem creating new date.  Could not update database.");
	 }
}
catch (Exception ex)
{
	  MessageBox.Show("Problem creating new record: " + ex.Message);
}



What I'm seeing is that the rowsAffected is never incrementing. Further investigation shows that I'm getting an error on the line:
newDateRow.Date = dateTimePicker1.Value.Date;



The error is: newDateRow.Date threw an exception of type 'System.InvalidCastException'

I can't think that this is overly difficult as it seems like such a logical thing to do (having a user insert a date into a DB).

Any ideas?

Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Attempting to insert a date from a DateTimePicker to a SQL Database

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,437
  • Joined: 23-August 08

Re: Attempting to insert a date from a DateTimePicker to a SQL Database

Posted 09 March 2009 - 01:52 PM

Well, my completely off the cuff guess would be that it's because a Date is not really the same as a DateTime?
Was This Post Helpful? 0
  • +
  • -

#3 mjwood0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-March 09

Re: Attempting to insert a date from a DateTimePicker to a SQL Database

Posted 09 March 2009 - 01:55 PM

Thanks for the reply.

I've tried the following too with no success:

newDateRow.Date = dateTimePicker1.Value;



Perhaps I'm missing something. I'll keep digging!

This post has been edited by mjwood0: 09 March 2009 - 01:55 PM

Was This Post Helpful? 0
  • +
  • -

#4 mjwood0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-March 09

Re: Attempting to insert a date from a DateTimePicker to a SQL Database

Posted 10 March 2009 - 05:44 AM

Kept at it. What can I say, I'm stubborn.

Found that somewhere during the editing, I lost the line

timeKeeperDataSet.Dates.AddDatesRow(newDateRow);



This, coupled with using the .Value property instead of the .Value.Date property fixed the problem.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1