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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Inserting date

Inserting date

Scheduled Pinned Locked Moved ASP.NET
csharpdatabasetutorial
11 Posts 4 Posters 1 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.
  • K Kunal P

    i m developing a web based application in c#. i just wanted to ask the most basic qn.. how to insert the current date in the database... how shud i provide the option on the webpage.. and wat all is required in the database. till yet i have created a field named Dt and datatype datetime..

    Kunal Piyush

    R Offline
    R Offline
    R Palanivel
    wrote on last edited by
    #2

    Hi You can insert Data in many ways . try to use stored procedures(for good programming). here i give u some sample code SqlConnection sqlCon=new SqlConnection("connection string"); sqlCon.Open(); SqlCommand insertCmd = new SqlCommand("stored procedure name", sqlCon); insertCmd.CommandType = CommandType.StoredProcedure; //Create parameter and Set values insertCmd.Parameters.Add(, ,); insertCmd.Parameters[].Value = insertCmd.ExecuteNonQuery(); sqlCon.Close(); r_palanivel83 10:01 4 Jan '06

    K 1 Reply Last reply
    0
    • R R Palanivel

      Hi You can insert Data in many ways . try to use stored procedures(for good programming). here i give u some sample code SqlConnection sqlCon=new SqlConnection("connection string"); sqlCon.Open(); SqlCommand insertCmd = new SqlCommand("stored procedure name", sqlCon); insertCmd.CommandType = CommandType.StoredProcedure; //Create parameter and Set values insertCmd.Parameters.Add(, ,); insertCmd.Parameters[].Value = insertCmd.ExecuteNonQuery(); sqlCon.Close(); r_palanivel83 10:01 4 Jan '06

      K Offline
      K Offline
      Kunal P
      wrote on last edited by
      #3

      hey i m sorry if i cudnt clarify myself.. wanted a walthrough ragarding inserting a DATE... thx..

      Kunal Piyush

      B R 2 Replies Last reply
      0
      • K Kunal P

        hey i m sorry if i cudnt clarify myself.. wanted a walthrough ragarding inserting a DATE... thx..

        Kunal Piyush

        B Offline
        B Offline
        badgrs
        wrote on last edited by
        #4

        DateTime.Now is the .NET method of getting the current date object. or if your using SQL Server you can use the GETDATE() SQL command directly in your query

        1 Reply Last reply
        0
        • K Kunal P

          hey i m sorry if i cudnt clarify myself.. wanted a walthrough ragarding inserting a DATE... thx..

          Kunal Piyush

          R Offline
          R Offline
          R Palanivel
          wrote on last edited by
          #5

          HI you just use this code insertCmd.Parameters.Add("@DateVale", SqlDbType.DateTime,10); insertCmd.Parameters["@DateVale"].Value = DateTime.Today.ToString("MM/dd/yyyy")

          r_palanivel83 10:01 4 Jan '06

          C K 2 Replies Last reply
          0
          • R R Palanivel

            HI you just use this code insertCmd.Parameters.Add("@DateVale", SqlDbType.DateTime,10); insertCmd.Parameters["@DateVale"].Value = DateTime.Today.ToString("MM/dd/yyyy")

            r_palanivel83 10:01 4 Jan '06

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #6

            r_palanivel83 wrote:

            insertCmd.Parameters["@DateVale"].Value = DateTime.Today.ToString("MM/dd/yyyy")

            Why on Earth would you convert the date to a string?! Just pass it the DateTime object.


            Upcoming events: * Glasgow: Geek Dinner (5th March) * Edinburgh: Web Security Conference Day for Windows Developers (12th April) My: Website | Blog | Photos

            1 Reply Last reply
            0
            • R R Palanivel

              HI you just use this code insertCmd.Parameters.Add("@DateVale", SqlDbType.DateTime,10); insertCmd.Parameters["@DateVale"].Value = DateTime.Today.ToString("MM/dd/yyyy")

              r_palanivel83 10:01 4 Jan '06

              K Offline
              K Offline
              Kunal P
              wrote on last edited by
              #7

              i am now able to add the date to the database.. bu there is on problem.. when i retrive the date when the employee was added.. it will also the time along the date.. i dont want the time to at all get into the database... is it possible.

              Kunal Piyush

              B 1 Reply Last reply
              0
              • K Kunal P

                i am now able to add the date to the database.. bu there is on problem.. when i retrive the date when the employee was added.. it will also the time along the date.. i dont want the time to at all get into the database... is it possible.

                Kunal Piyush

                B Offline
                B Offline
                badgrs
                wrote on last edited by
                #8

                A date field also includes the time - I see no reason why you wouldn't want this information available even if you don't want to display it. Use DateTime.ToString(format) to format the output without time.

                K 1 Reply Last reply
                0
                • B badgrs

                  A date field also includes the time - I see no reason why you wouldn't want this information available even if you don't want to display it. Use DateTime.ToString(format) to format the output without time.

                  K Offline
                  K Offline
                  Kunal P
                  wrote on last edited by
                  #9

                  a lil bit of problem.. the date would be retrieved from the database... now how shud i fomat it without the time.. and in DD/MM/YYYY format.. to retrieve i m using this line of code Label1.Text = "" + dread["dt"].ToString(); where dt is the datetime field in the database.

                  Kunal Piyush

                  B 1 Reply Last reply
                  0
                  • K Kunal P

                    a lil bit of problem.. the date would be retrieved from the database... now how shud i fomat it without the time.. and in DD/MM/YYYY format.. to retrieve i m using this line of code Label1.Text = "" + dread["dt"].ToString(); where dt is the datetime field in the database.

                    Kunal Piyush

                    B Offline
                    B Offline
                    badgrs
                    wrote on last edited by
                    #10

                    Kunal P wrote:

                    Label1.Text = "" + dread["dt"].ToString();

                    If you look in the documentation you'll see the ToString method is overloaded (has several different methods with different parameters). You can feed is a string format like this: (and you don't need the initial blank string - that just creates another object to be cleaned up by the GC)

                    Label1.Text = dread["dt"].ToString("dd/MM/yyyy");
                    

                    You'll probably find you need to cast it to a DateTime object first:

                    Label1.Text = ((DateTime)dread["dt"]).ToString("dd/MM/yyyy");
                    

                    This documents the possible formating strings: Custom DateTime Format Strings

                    K 1 Reply Last reply
                    0
                    • B badgrs

                      Kunal P wrote:

                      Label1.Text = "" + dread["dt"].ToString();

                      If you look in the documentation you'll see the ToString method is overloaded (has several different methods with different parameters). You can feed is a string format like this: (and you don't need the initial blank string - that just creates another object to be cleaned up by the GC)

                      Label1.Text = dread["dt"].ToString("dd/MM/yyyy");
                      

                      You'll probably find you need to cast it to a DateTime object first:

                      Label1.Text = ((DateTime)dread["dt"]).ToString("dd/MM/yyyy");
                      

                      This documents the possible formating strings: Custom DateTime Format Strings

                      K Offline
                      K Offline
                      Kunal P
                      wrote on last edited by
                      #11

                      hi thanks a lot for all the help.. and i am sorry if i am asking way too basic questions.. just started programming..;P

                      Kunal Piyush

                      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