how to convert DateTime in short date and short time

Discussion in 'Microsoft C# .NET' started by Omer kamal, Sep 12, 2005.

  1. Omer kamal

    Omer kamal Guest

    Dear all,

    I want to save my data into MS Access database Table which consist of
    some fields with short date and short time formats.
    I try to save data in to the said table and I am getting error that my
    SQL statment is not correct.

    string strSQL = @"INSERT INTO Activities ( Task,Day,Start,Finish )
    VALUES ( '"+id+"', '"+date+"', '"+start+"', '"+finish+"' )" ;

    I think error is because i am not able to cast my variables correctly.

    how can i convert my DateTime to short date and short Time formats???

    regards,

    Omer kamal
     
    Omer kamal, Sep 12, 2005
    #1
    1. Advertisements

  2. Omar,

    Use parameters (the rest should go automatic)

    http://msdn.microsoft.com/library/d...dataoledboledbcommandclassparameterstopic.asp

    Be aware that you have in OleDb to add the parameters in the sequents of
    occurence of your SQL string

    I hope this helps,

    Cor

    "Omer kamal" <-kl.de> schreef in bericht
    news:dg2u7n$5c3$-kl.de...
    > Dear all,
    >
    > I want to save my data into MS Access database Table which consist of some
    > fields with short date and short time formats.
    > I try to save data in to the said table and I am getting error that my SQL
    > statment is not correct.
    >
    > string strSQL = @"INSERT INTO Activities ( Task,Day,Start,Finish ) VALUES
    > ( '"+id+"', '"+date+"', '"+start+"', '"+finish+"' )" ;
    >
    > I think error is because i am not able to cast my variables correctly.
    >
    > how can i convert my DateTime to short date and short Time formats???
    >
    > regards,
    >
    > Omer kamal
     
    Cor Ligthert [MVP], Sep 12, 2005
    #2
    1. Advertisements

  3. Omer kamal <-kl.de> wrote:
    > I want to save my data into MS Access database Table which consist of
    > some fields with short date and short time formats.
    > I try to save data in to the said table and I am getting error that my
    > SQL statment is not correct.
    >
    > string strSQL = @"INSERT INTO Activities ( Task,Day,Start,Finish )
    > VALUES ( '"+id+"', '"+date+"', '"+start+"', '"+finish+"' )" ;
    >
    > I think error is because i am not able to cast my variables correctly.
    >
    > how can i convert my DateTime to short date and short Time formats???


    The trick is not to embed the values directly in the SQL in the first
    place. Instead, use aparameterised command. See
    http://www.pobox.com/~skeet/csharp/faq/#db.parameters

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet
    If replying to the group, please do not mail me too
     
    Jon Skeet [C# MVP], Sep 12, 2005
    #3
  4. You should also be aware that date values are stored differnrtly in MS
    Access, but the libray has methoids to convert both ways.

    See:
    DateTime.ToOADate Method
    DateTime.FromOADate Method

    Paul V. Sullivan


    On Mon, 12 Sep 2005 05:55:35 +0200, Omer kamal <-kl.de>
    wrote:

    >Dear all,
    >
    >I want to save my data into MS Access database Table which consist of
    >some fields with short date and short time formats.
    >I try to save data in to the said table and I am getting error that my
    >SQL statment is not correct.
    >
    >string strSQL = @"INSERT INTO Activities ( Task,Day,Start,Finish )
    >VALUES ( '"+id+"', '"+date+"', '"+start+"', '"+finish+"' )" ;
    >
    >I think error is because i am not able to cast my variables correctly.
    >
    >how can i convert my DateTime to short date and short Time formats???
    >
    >regards,
    >
    >Omer kamal
     
    Paul Sullivan, Sep 13, 2005
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Worrall

    Re: converting Unix datetime count to .Net DateTime object

    Mark Worrall, Feb 4, 2004, in forum: Microsoft C# .NET
    Replies:
    1
    Views:
    297
    Aravind C
    Feb 4, 2004
  2. mimi
    Replies:
    1
    Views:
    1,440
    Jon Skeet [C# MVP]
    Aug 6, 2004
  3. ABC
    Replies:
    1
    Views:
    1,115
    Maqsood Ahmed
    Sep 29, 2005
  4. Narshe
    Replies:
    3
    Views:
    1,758
    Narshe
    Nov 2, 2005
  5. Guest
    Replies:
    2
    Views:
    391
    Guest
    Sep 20, 2006
Loading...

Share This Page