update datetimepicker

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mrijet
    New Member
    • Feb 2015
    • 64

    update datetimepicker

    Hi, can anyone help me to guide on how to update using datetimepicker?

    Here is my sql syntax :-
    Code:
    UPDATE table set Date='" + datetimepicker1.Text + "' WHERE ID='" + ID.Text + "';
    After I run the message prompat said Syntax Missing..

    ASAP me if got solution
    Last edited by zmbd; Oct 29 '15, 01:41 AM. Reason: [z{please place script within code block}]
  • Ali Saleem
    New Member
    • Oct 2015
    • 2

    #2
    1. Add a timer to your form.

    2. Set timer interval to 1000 (milliseconds)

    3. Set Enabled property to True

    4. Update the DateTimePicker value in the Tick event of the timer as below


    Code:
     private void timer1_Tick(object sender, EventArgs e)
            {
                dateTimePicker1.Value = DateTime.Now; 
            }
    Last edited by zmbd; Oct 29 '15, 01:41 AM. Reason: [z{please place script within code block}]

    Comment

    • adriancs
      New Member
      • Apr 2011
      • 122

      #3
      I assumed that the value of dateTimePicker1 .Value is something like this (MM-dd-yyyy):

      Code:
      12-23-2015
      Then you can get the datetime value like this

      Code:
      string[] sa = dateTimePicker1.Value.Split('-');
      int month = Convert.ToInt(sa[0]);
      int day = Convert.ToInt(sa[1]);
      int year = Convert.ToInt(sa[2]);
      DateTime dateTime1 = new DateTime(year, month, day);
      string datestr = dateTime1.ToString("yyyy-MM-dd HH:mm:ss");
      int id = Convert.ToInt(ID.Text);
      
      string sql = string.Format("UPDATE table set `Date`='{0}' WHERE ID={1};",
      datestr, id);

      Comment

      Working...