4 Replies - 2914 Views - Last Post: 04 May 2012 - 04:16 AM Rate Topic: -----

#1 sidyusuf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 15-September 11

combine different date and time using two datetimepickers

Posted 01 May 2012 - 04:45 AM

hi friends,
i have two datetimepickers in a window form application.One datetimepicker is used to select date and second one is for time.
Now i want to combine date from DTP1 and time from DTP2 and update the database with the selected combined datetime value using vb.net.
Thankyou
Is This A Good Question/Topic? 0
  • +

Replies To: combine different date and time using two datetimepickers

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: combine different date and time using two datetimepickers

Posted 01 May 2012 - 05:07 AM

You do know that the datetime picker can be set to allow you to display and set these in the same control right?

It also has to methods of output, one is a date type (using .Value) the other is a String type (using .Text)

Strings are easily concatenated using the ampersand (&)

This post has been edited by CharlieMay: 01 May 2012 - 05:09 AM

Was This Post Helpful? 0
  • +
  • -

#3 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 205
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: combine different date and time using two datetimepickers

Posted 01 May 2012 - 05:10 AM

EDIT
Sorry I posted sample code earlier and then saw CharlieMay has given you the instructions on how to do it so you should first give it a try rather than copying the code. As a help, check this out. A great article which shows you all the possible date and time formats you can extract from DateTimePickers.

This post has been edited by nK0de: 01 May 2012 - 05:14 AM

Was This Post Helpful? 0
  • +
  • -

#4 sidyusuf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 15-September 11

Re: combine different date and time using two datetimepickers

Posted 03 May 2012 - 11:18 PM

plz explain me...what i have done is...
Code:

Try
Dim strdatetime As String = DateTimePicker1.Text + " " + DateTimePicker2.Text
Dim date1 As Date = Date.Parse(strdatetime, System.Globalization.CultureInfo.InvariantCulture)
sqlupdate1 = "UPDATE paper SET date='" & date1.ToString & "' WHERE paperid='" + dgvPaper.CurrentRow.Cells("paperid").Value + "'"
Dim cmd3 As New SqlCommand(sqlupdate1, con)
cmd3.ExecuteNonQuery()
Catch err As System.Exception
MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try

here when i update i get error "string is not recognized as valid datetime" only in case when i select date greater than 12 from datetimpicker1 otherwise its working fine.
i have taken short format of datetimepicker1 and time format for dtp2..
Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: combine different date and time using two datetimepickers

Posted 04 May 2012 - 04:16 AM

You're probably setting your date format to MM/dd/yyyy in your DTP but the system thinks it should be dd/MM/yyyy which would cause it to throw an error above 12.

See, 10/12/2012 can be seen as Oct 12, 2012 or Dec 12, 2012 depending on the format and both are valid dates. However, 10/26/2012 can only bee seen as Oct 26, 2012 in a dd/MM/yyyy format.

You might look into DateTime.ParseExact Method (String, String, IFormatProvider)


Now that being said, I'm not sure why you're taking the string representation of each DTP and concatenating them and then converting them to a date to only convert them back to a string in your statement. The sql statement is a string that is sent to the SQL parser so there is no need to do this date conversion in the first place.
Dim strdatetime As String = DateTimePicker1.Text + " " + DateTimePicker2.Text
sqlupdate1 = "UPDATE paper SET date='" & strdatetime & "' WHERE paperid='" + dgvPaper.CurrentRow.Cells("paperid").Value + "'"

And lastly, date, as a field name, is going to cause some problems since it's a keyword. Either change it to something like dateTS or enclose it in brackets [date] to let the parser know you are talking about a field here.

Also, take the time to look into using parameters for your statements, it's very easy to accomplish and will secure your database against SQL injection. Not to mention, it will save you a lot of concatenation headaches.

This post has been edited by CharlieMay: 04 May 2012 - 04:18 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1