Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Working with "Date/Time" field of "Access Database" in C#

Working with "Date/Time" field of "Access Database" in C#

Scheduled Pinned Locked Moved C#
questioncsharpdatabase
18 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sourie
    wrote on last edited by
    #1

    How can I work with Date/Time field of Access Database in C#? I need to read these feilds of a row and show it in messagebox. Also I need to add new rows to a table in access and fill these fields for it.

    Sourie

    E W 2 Replies Last reply
    0
    • S Sourie

      How can I work with Date/Time field of Access Database in C#? I need to read these feilds of a row and show it in messagebox. Also I need to add new rows to a table in access and fill these fields for it.

      Sourie

      E Offline
      E Offline
      Ennis Ray Lynch Jr
      wrote on last edited by
      #2

      A connection string for you is: Data Source=the complete file path\somefile.mdb;Persist Security Info=False; Use connectionstrings.com for more. As for the rest a simple introduction into the world of ADO.NET will get you started. There are plenty of articles on CP that deal with that.

      Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
      Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
      Most of this sig is for Google, not ego.

      1 Reply Last reply
      0
      • S Sourie

        How can I work with Date/Time field of Access Database in C#? I need to read these feilds of a row and show it in messagebox. Also I need to add new rows to a table in access and fill these fields for it.

        Sourie

        W Offline
        W Offline
        Wendelius
        wrote on last edited by
        #3

        Not sure if your question was about this, but if you're having a problem with date literals, as I recall, it uses format #mm/dd/yyyy#. For example #09/23/2008#.

        The need to optimize rises from a bad design

        S 1 Reply Last reply
        0
        • W Wendelius

          Not sure if your question was about this, but if you're having a problem with date literals, as I recall, it uses format #mm/dd/yyyy#. For example #09/23/2008#.

          The need to optimize rises from a bad design

          S Offline
          S Offline
          Sourie
          wrote on last edited by
          #4

          thanks for your reply. I need to have to seperate fields : Date and Time, but when I want to define columns in Access, I only see a field : Date/Time. I do not know how to write from/read into this field in a c# program. (I want to read time and date from two different textBoxs and save them in related field in a Access table. and also I want to read Date and Time field from access and show them in a MessageBox.)

          Sourie

          W 1 Reply Last reply
          0
          • S Sourie

            thanks for your reply. I need to have to seperate fields : Date and Time, but when I want to define columns in Access, I only see a field : Date/Time. I do not know how to write from/read into this field in a c# program. (I want to read time and date from two different textBoxs and save them in related field in a Access table. and also I want to read Date and Time field from access and show them in a MessageBox.)

            Sourie

            W Offline
            W Offline
            Wendelius
            wrote on last edited by
            #5

            You don't have separate data types for date and time. When you need just time portion, use date/time field with a fixed date. For example: set date for each time for 1.1.1900. When showing a time field, show only time portion. This way having the date in the field doesn't matter and it isn't shown Mika

            The need to optimize rises from a bad design

            S 1 Reply Last reply
            0
            • W Wendelius

              You don't have separate data types for date and time. When you need just time portion, use date/time field with a fixed date. For example: set date for each time for 1.1.1900. When showing a time field, show only time portion. This way having the date in the field doesn't matter and it isn't shown Mika

              The need to optimize rises from a bad design

              S Offline
              S Offline
              Sourie
              wrote on last edited by
              #6

              Like his: //dr indicates to a row of a table in Access Database. DataRow dr = new DataRow(); // I have defined two fields of type Date/Time in a table and now I want to give them value for the current row in the table: dr["Time"] = Convert.ToDateTime(DateTime.Now.ToShortTimeString()); dr["Date"] = Convert.ToDateTime(DateTime.Now.ToShortDateString()); Is it correct?

              Sourie

              W 1 Reply Last reply
              0
              • S Sourie

                Like his: //dr indicates to a row of a table in Access Database. DataRow dr = new DataRow(); // I have defined two fields of type Date/Time in a table and now I want to give them value for the current row in the table: dr["Time"] = Convert.ToDateTime(DateTime.Now.ToShortTimeString()); dr["Date"] = Convert.ToDateTime(DateTime.Now.ToShortDateString()); Is it correct?

                Sourie

                W Offline
                W Offline
                Wendelius
                wrote on last edited by
                #7

                Something like that. Don't have a compiler near and fon't remember what is the date portion in System.DateTime if it is not defined. If the date is too far away, it can cause problems to Access. In case Access doesn't like the date portion, you can use something like this (syntax is not correct): Convert.ToDateTime("01011900" + currentTime); Mika

                The need to optimize rises from a bad design

                S 1 Reply Last reply
                0
                • W Wendelius

                  Something like that. Don't have a compiler near and fon't remember what is the date portion in System.DateTime if it is not defined. If the date is too far away, it can cause problems to Access. In case Access doesn't like the date portion, you can use something like this (syntax is not correct): Convert.ToDateTime("01011900" + currentTime); Mika

                  The need to optimize rises from a bad design

                  S Offline
                  S Offline
                  Sourie
                  wrote on last edited by
                  #8

                  This is my code: I reach an error: Syntax error in INSERT INTO statement. I know this error is because of line : dr["Date"] = Convert.ToDateTime(DateTime.Now.ToShortDateString()); But I donno how to solve it. I have a field named "Date", with datatype "DateTime", and with format "ShortDate". OleDbDataAdapter adp = new OleDbDataAdapter(); OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0;" + @"data source=f:\Wire1\WireDb.mdb"); conn.Close(); OleDbCommand cmd = new OleDbCommand("SELECT * FROM tbl1 ORDER BY Code", conn); adp.SelectCommand = cmd; OleDbCommandBuilder cb = new OleDbCommandBuilder(adp); DataSet ds = new DataSet("tbl1"); DataTable dt = new DataTable(); conn.Open(); adp.Fill(ds, "tbl1"); dt = ds.Tables[0]; DataRow dr = ds.Tables[0].NewRow(); int x = dt.Rows.Count; dr["Code"] = ++x; dr["Street"] = "Molavi"; dr["Alley"] = "Molavi 34"; dr["Status"] = true; dr["Date"] = Convert.ToDateTime(DateTime.Now.ToShortDateString()); ds.Tables[0].Rows.Add(dr); adp.Update(dt); ds.Clear(); adp.Fill(ds.Tables[0]); conn.Close();

                  Sourie

                  W 1 Reply Last reply
                  0
                  • S Sourie

                    This is my code: I reach an error: Syntax error in INSERT INTO statement. I know this error is because of line : dr["Date"] = Convert.ToDateTime(DateTime.Now.ToShortDateString()); But I donno how to solve it. I have a field named "Date", with datatype "DateTime", and with format "ShortDate". OleDbDataAdapter adp = new OleDbDataAdapter(); OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0;" + @"data source=f:\Wire1\WireDb.mdb"); conn.Close(); OleDbCommand cmd = new OleDbCommand("SELECT * FROM tbl1 ORDER BY Code", conn); adp.SelectCommand = cmd; OleDbCommandBuilder cb = new OleDbCommandBuilder(adp); DataSet ds = new DataSet("tbl1"); DataTable dt = new DataTable(); conn.Open(); adp.Fill(ds, "tbl1"); dt = ds.Tables[0]; DataRow dr = ds.Tables[0].NewRow(); int x = dt.Rows.Count; dr["Code"] = ++x; dr["Street"] = "Molavi"; dr["Alley"] = "Molavi 34"; dr["Status"] = true; dr["Date"] = Convert.ToDateTime(DateTime.Now.ToShortDateString()); ds.Tables[0].Rows.Add(dr); adp.Update(dt); ds.Clear(); adp.Fill(ds.Tables[0]); conn.Close();

                    Sourie

                    W Offline
                    W Offline
                    Wendelius
                    wrote on last edited by
                    #9

                    The INSERT statement wasn't included in your post (only SELECT), but I believe that there are two possible reasons - Access or OleDbConnection doesn't like if the field name is Date (may be a reserved word). Tru using angle brackets (for example INSERT INTO TableName (Field1, Field2, ..., [Date]) VALUES (...)) or chcange the name of the field in the db. - dr["Date"] has a bad value. Use debugger to view it's value and to see if it looks like what you would suspect Hope this helps, Mika

                    The need to optimize rises from a bad design

                    S 1 Reply Last reply
                    0
                    • W Wendelius

                      The INSERT statement wasn't included in your post (only SELECT), but I believe that there are two possible reasons - Access or OleDbConnection doesn't like if the field name is Date (may be a reserved word). Tru using angle brackets (for example INSERT INTO TableName (Field1, Field2, ..., [Date]) VALUES (...)) or chcange the name of the field in the db. - dr["Date"] has a bad value. Use debugger to view it's value and to see if it looks like what you would suspect Hope this helps, Mika

                      The need to optimize rises from a bad design

                      S Offline
                      S Offline
                      Sourie
                      wrote on last edited by
                      #10

                      Oh my God, It is working now. Thanks alot Mika, You are right "Date is a keyword for MS Access, I changed it to DateVal, and my code finally worked. :) Tnx alot

                      Sourie

                      W 1 Reply Last reply
                      0
                      • S Sourie

                        Oh my God, It is working now. Thanks alot Mika, You are right "Date is a keyword for MS Access, I changed it to DateVal, and my code finally worked. :) Tnx alot

                        Sourie

                        W Offline
                        W Offline
                        Wendelius
                        wrote on last edited by
                        #11

                        You're welcome :) Happy coding!

                        The need to optimize rises from a bad design

                        S 1 Reply Last reply
                        0
                        • W Wendelius

                          You're welcome :) Happy coding!

                          The need to optimize rises from a bad design

                          S Offline
                          S Offline
                          Sourie
                          wrote on last edited by
                          #12

                          I got another problem. I created similar table in my database and I named it as tbl2. I tryed the code for this table, but it is not working.

                          Sourie

                          W 1 Reply Last reply
                          0
                          • S Sourie

                            I got another problem. I created similar table in my database and I named it as tbl2. I tryed the code for this table, but it is not working.

                            Sourie

                            W Offline
                            W Offline
                            Wendelius
                            wrote on last edited by
                            #13

                            If it's working for the first table, double check the code for second one. If still having problems I think you should open a new thread and provide info about the problem (error message, relevant code etc..) especially if the problem is somehow different. If I'm still awake, I'll try to check it also. Also I think you would get more answers if you would post on .Net Framework forum (not sure if that's the correct one though). The problem in this question was related to a class defined in framework but also to Access database so perhaps SQL forum would do just as fine. Mika

                            The need to optimize rises from a bad design

                            S 1 Reply Last reply
                            0
                            • W Wendelius

                              If it's working for the first table, double check the code for second one. If still having problems I think you should open a new thread and provide info about the problem (error message, relevant code etc..) especially if the problem is somehow different. If I'm still awake, I'll try to check it also. Also I think you would get more answers if you would post on .Net Framework forum (not sure if that's the correct one though). The problem in this question was related to a class defined in framework but also to Access database so perhaps SQL forum would do just as fine. Mika

                              The need to optimize rises from a bad design

                              S Offline
                              S Offline
                              Sourie
                              wrote on last edited by
                              #14

                              I am sorry it was like the previous problem. Because in the second table of my database, I defined a field named: "No". It was also a keyword for MS ACCESS. Thank you so much for helping me. Kiss your child

                              Sourie

                              W 1 Reply Last reply
                              0
                              • S Sourie

                                I am sorry it was like the previous problem. Because in the second table of my database, I defined a field named: "No". It was also a keyword for MS ACCESS. Thank you so much for helping me. Kiss your child

                                Sourie

                                W Offline
                                W Offline
                                Wendelius
                                wrote on last edited by
                                #15

                                You're welcome :) There are plenty of reserved words so you'll encounter this problem once in a while. If you want, you can test using angle brackets. At least in SQL Server they make it possible to use reserved words and even spaces. When defining or using a field you can surround it with brackets, for example: - [No] - [Yes] - [Field name with space] and then for example "SELECT [No], [Field name with space] FROM [TableName] WHERE [Yes] = ..." Mika

                                The need to optimize rises from a bad design

                                S 1 Reply Last reply
                                0
                                • W Wendelius

                                  You're welcome :) There are plenty of reserved words so you'll encounter this problem once in a while. If you want, you can test using angle brackets. At least in SQL Server they make it possible to use reserved words and even spaces. When defining or using a field you can surround it with brackets, for example: - [No] - [Yes] - [Field name with space] and then for example "SELECT [No], [Field name with space] FROM [TableName] WHERE [Yes] = ..." Mika

                                  The need to optimize rises from a bad design

                                  S Offline
                                  S Offline
                                  Sourie
                                  wrote on last edited by
                                  #16

                                  Thanks for your suggestion :)

                                  Sourie

                                  M 1 Reply Last reply
                                  0
                                  • S Sourie

                                    Thanks for your suggestion :)

                                    Sourie

                                    M Offline
                                    M Offline
                                    meenana
                                    wrote on last edited by
                                    #17

                                    Dears, I'm having a serious problem making me MAD! I'm searching for days over google but no use I'm trying to get data from access where date column in access = datetime selected from textbox in C# CODE: it is one line code which is not working DateTime d = DateTime.Parse("1/1/1");//while debugging : d=1/1/2010 12:00:00 AM sql="select * from MYTABLE where COLUMNDATE = d"; COLUMNDATE is defined as general date in MYTABLE IN access. and then when I execute the sql (i.e: datareader.executeReader) command with C#, it is giving mismatch data type. PLZ PLZ PLZ PLZ try to help me!!! THANKS IN ADVANCE!!

                                    R 1 Reply Last reply
                                    0
                                    • M meenana

                                      Dears, I'm having a serious problem making me MAD! I'm searching for days over google but no use I'm trying to get data from access where date column in access = datetime selected from textbox in C# CODE: it is one line code which is not working DateTime d = DateTime.Parse("1/1/1");//while debugging : d=1/1/2010 12:00:00 AM sql="select * from MYTABLE where COLUMNDATE = d"; COLUMNDATE is defined as general date in MYTABLE IN access. and then when I execute the sql (i.e: datareader.executeReader) command with C#, it is giving mismatch data type. PLZ PLZ PLZ PLZ try to help me!!! THANKS IN ADVANCE!!

                                      R Offline
                                      R Offline
                                      RaviRanjanKr
                                      wrote on last edited by
                                      #18

                                      Hey you need to write

                                      DateTime d = DateTime.Parse("1/1/1");//while debugging : d=1/1/2001 12:00:00 AM
                                      sql="select * from MYTABLE where COLUMNDATE = '"+d.ToShortDateString()+"'";

                                      because of d.ToShortDateString() value of d will be only 1/1/2001 which is same as general date as MyTable in Access.

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups