Insert DateTime to Sql Database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kosal
    New Member
    • Feb 2007
    • 68

    Insert DateTime to Sql Database

    Dear All

    I would like you to help to insert and update datetime to sql database.

    Please kindly help.

    thanks
  • nmsreddi
    Contributor
    • Jul 2006
    • 366

    #2
    Hello

    you can use normal sql query as usual like other fields how you are inserting ,

    have you tried it ,did you get any error,

    mostly you will get format conversion errors ,by default sqlserver will take MM/dd/yyyy format ,make sure that your input is in that format other you can conver that to your custom format with the existing convert functions.

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Do you know how to insert/update things other than datetimes? What I'm trying to get at here is: do you have a problem specifically with datetimes, or do you just not know how to use a DB in your program? We have a few articles on how to use databases. I'd suggest you check them out in the Howto section. Otherwise, if it is a datetime problem, you can use a parameterized query and not have to worry about it.

      Here's a code sample on using a parameterized query:
      Code:
      //c# code.
      //dt is the datetime field
      string update = "update test set dt=@dt where id=@id";
      OleDbCommand cmd = new OleDbCommand(update, conn);
      DateTime dtme = DateTime.Now;
      cmd.Parameters.AddWithValue("@dt", dtme.ToString());
      cmd.Parameters.AddWithValue("@id", Convert.ToInt32(TextBox2.Text));
      Then you just execute the query as you normally would.

      Comment

      • Kosal
        New Member
        • Feb 2007
        • 68

        #4
        Dear All

        thank you for your help, but I need as bellow:
        I add DateTimePicker and i want to insert like
        strSQL = "INSERT INTO tblBooking(bPNR , bPaxName, bIssureDate, bTravelDate, bDeparting, bArriving, bFlyType, bStatus, bBy, bNetPrice, bSalePrice)" & "VALUES('" & txtPNR.Text & "', '" & txtPaxName.Text & "','" & DateTimePicker1 .Text & "','" & DateTimePicker2 .Text & "','" & txtDeparting.Te xt & "','" & txtArriving.Tex t & "','" & cboFlyType.Text & "','" & cboStatus.Text & "','" & cboBy.Text & "'," & txtNetPrice.Tex t & "," & txtSalePrice.Te xt & ")"

        please advise

        thanks

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          Well, if you'll read my reply, I show you how to use a parameterized query. That's the easiest way to do this. Change your values clause to "VALUES (@bPNR, @bPaxName, @bIssureDate ... " and so forth. Then add the parameter values. My example was in C#, but it shouldn't be that hard to translate to vb.

          Comment

          • Kosal
            New Member
            • Feb 2007
            • 68

            #6
            ok thank you for your advise i will try to do it.
            thanks

            Comment

            • Kosal
              New Member
              • Feb 2007
              • 68

              #7
              Dear all

              I try to do it but still error can you give me the full code for this problem in vb.net

              I am waiting for your advise

              thanks
              Best Regard

              Comment

              • Curtis Rutland
                Recognized Expert Specialist
                • Apr 2008
                • 3264

                #8
                We don't do your assignment for you here. Please don't ask for a full code solution.

                MODERATOR

                Now, if you post what you've tried and the error message, we can get down to business trying to help you correct your code.

                Comment

                • Kosal
                  New Member
                  • Feb 2007
                  • 68

                  #9
                  ok thanks you for your help, but i still cannot do anything yet with
                  insert datetimepicker to database.

                  thanks

                  Comment

                  • Curtis Rutland
                    Recognized Expert Specialist
                    • Apr 2008
                    • 3264

                    #10
                    If you tell us what error message you get, we might actually be able to help you.

                    Comment

                    • ganeshvkl
                      New Member
                      • Jul 2008
                      • 50

                      #11
                      hi Kosal,
                      datepicker1.val ue instead of datepicker1.tex t , it is working in VB.net 2003
                      with sql server
                      Try it out...

                      ganesh

                      Comment

                      • Kosal
                        New Member
                        • Feb 2007
                        • 68

                        #12
                        Hi All
                        thank you for your help
                        now I resolve this problem already.

                        thanks

                        Comment

                        • karthik kaliyaperumal
                          New Member
                          • Feb 2008
                          • 4

                          #13
                          HI,

                          You can store in one variable like = datatime.now (This is vill give current datatime.then you can pass to database.

                          Otherwise change the format like "DD/MM/YYYY"

                          Comment

                          • Kosal
                            New Member
                            • Feb 2007
                            • 68

                            #14
                            Ok Sir

                            Now it is ok
                            Thank you for you advice

                            thank
                            Best Regard

                            Comment

                            Working...