Inserting Datetimepicker in SQL Database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cravish
    New Member
    • Aug 2007
    • 7

    Inserting Datetimepicker in SQL Database

    Hi,
    I have a problem while inserting Datetimepicker value into Sql database.The value which gets inserted in database is 01/01/1900 which is the wrong one ,as I wanted today or current date to get inserted in database.
    i am using VB.Net 2003 as my front-end and MS SQL 2000 as my back-End.
    Please help me....
  • dip_developer
    Recognized Expert Contributor
    • Aug 2006
    • 648

    #2
    Originally posted by cravish
    Hi,
    I have a problem while inserting Datetimepicker value into Sql database.The value which gets inserted in database is 01/01/1900 which is the wrong one ,as I wanted today or current date to get inserted in database.
    i am using VB.Net 2003 as my front-end and MS SQL 2000 as my back-End.
    Please help me....
    i have some question..
    Are you picking date from datetimepicker? ?
    Do you want to insert Today's date everytime???The n why are you using datetimepicker if the date is fixed???

    two possibilities.. ...
    1.If you are picking date from datetimepicker then use the following code

    Dim myDate as Date
    myDate=myDatepi cker.Value

    while inserting write code like...........
    Insert into myTable (mDate) Values (#" & myDate & "#)

    2. if you only want to insert current date then use getdate() method
    like...
    Insert into employee values (4,'John',25, getdate())

    Comment

    • cravish
      New Member
      • Aug 2007
      • 7

      #3
      Originally posted by dip_developer
      i have some question..
      Are you picking date from datetimepicker? ?
      Do you want to insert Today's date everytime???The n why are you using datetimepicker if the date is fixed???

      two possibilities.. ...
      1.If you are picking date from datetimepicker then use the following code

      Dim myDate as Date
      myDate=myDatepi cker.Value

      while inserting write code like...........
      Insert into myTable (mDate) Values (#" & myDate & "#)

      2. if you only want to insert current date then use getdate() method
      like...
      Insert into employee values (4,'John',25, getdate())

      I am picking date from datetimepicker. But after taking your suggestion i am getting this error for date 23/08/2007 "The name #23 is not permitted in this context.Only constant,expres sions,or variable are allowed here.Column names are not permitted".One thing i forgot to specify is the datatype for date in database which is smalldatetime.

      Comment

      • dip_developer
        Recognized Expert Contributor
        • Aug 2006
        • 648

        #4
        Originally posted by cravish
        I am picking date from datetimepicker. But after taking your suggestion i am getting this error for date 23/08/2007 "The name #23 is not permitted in this context.Only constant,expres sions,or variable are allowed here.Column names are not permitted".One thing i forgot to specify is the datatype for date in database which is smalldatetime.
        Please post your code......

        Comment

        • cravish
          New Member
          • Aug 2007
          • 7

          #5
          Originally posted by dip_developer
          Please post your code......
          strQry = "insert into gin_mst(GINDate ,GinNo,........ ,userId,LCNo)"
          strQry = strQry & "values(#" & Format(myDate, "dd/MM/yyyy") & "#,'" & Trim(strGINNo) & "',........ .,'" & userId & "','" & Trim(cmbLcNo.Te xt) & "')"
          mycommand = New SqlCommand(strQ ry, myConnection)
          ra = mycommand.Execu teNonQuery


          this is the Queery which i am using to insert date.

          Comment

          • dip_developer
            Recognized Expert Contributor
            • Aug 2006
            • 648

            #6
            Originally posted by cravish
            strQry = "insert into gin_mst(GINDate ,GinNo,........ ,userId,LCNo)"
            strQry = strQry & "values(#" & Format(myDate, "dd/MM/yyyy") & "#,'" & Trim(strGINNo) & "',........ .,'" & userId & "','" & Trim(cmbLcNo.Te xt) & "')"
            mycommand = New SqlCommand(strQ ry, myConnection)
            ra = mycommand.Execu teNonQuery


            this is the Queery which i am using to insert date.
            I have forgotten about sql-server.....very very sorry...
            try

            "insert into gin_mst (GINDate) values('" & Format$(dtpicke r.value,
            "dd.mm.yyyy ") & "')"

            Comment

            • cravish
              New Member
              • Aug 2007
              • 7

              #7
              Originally posted by dip_developer
              I have forgotten about sql-server.....very very sorry...
              try

              "insert into gin_mst (GINDate) values('" & Format$(dtpicke r.value,
              "dd.mm.yyyy ") & "')"
              I tried what you have suggested but still its storing 01/01/1900 in database.

              Comment

              • shiponeye1
                New Member
                • Aug 2007
                • 8

                #8
                Hi, just try with that.

                datetime dMyDate=dtpPick er.value

                cmdInsert="INSE RT INTO MyTable(MyDate) VALUES('" & dMyDate.ToStrin g("dd MMM yyyy") & "')"

                you can insert any date in this format.

                !!! SHIPON !!!

                Comment

                • cravish
                  New Member
                  • Aug 2007
                  • 7

                  #9
                  Originally posted by shiponeye1
                  Hi, just try with that.

                  datetime dMyDate=dtpPick er.value

                  cmdInsert="INSE RT INTO MyTable(MyDate) VALUES('" & dMyDate.ToStrin g("dd MMM yyyy") & "')"

                  you can insert any date in this format.

                  !!! SHIPON !!!

                  Yes i know.... but i am using smalldatetime as my data-type.
                  If i use the above query it give an error for datatype i.e
                  "The conversion of char data type to smalldatetime resulted in out-of-range smalldatetime value.The statement has been teminated."

                  Comment

                  • spidervb
                    New Member
                    • Sep 2013
                    • 3

                    #10
                    thnx

                    Originally posted by shiponeye1
                    Hi, just try with that.

                    datetime dMyDate=dtpPick er.value

                    cmdInsert="INSE RT INTO MyTable(MyDate) VALUES('" & dMyDate.ToStrin g("dd MMM yyyy") & "')"

                    you can insert any date in this format.

                    !!! SHIPON !!!
                    its work ,,,, thanks :)

                    Comment

                    Working...