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. problem retrieving info from one table to insert it into another

problem retrieving info from one table to insert it into another

Scheduled Pinned Locked Moved C#
helpdatabasecomdesignquestion
23 Posts 3 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.
  • A ago2486

    thank you for your answer but I wanted to point out that (num_com) is auto-incremente. I would like to know if that doesn't count?

    OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #4

    Yes, that's the whole point. That is what an IDENTITY field in SQL Server is. And when you use autoincremented fields you can't rely on the MAX value to be the one you inserted: you do your INSERT, then two dozen other users add their own before you issue your SELECT. YOur MAX then fetches the ID for a totally different row, and you start to get intermittent problems that are fiendishly difficult to duplicate, track down, fix, and prove. Using SCOPE_IDENTITY returns the last autoincremented value used by that connection, and will never return values from different users.

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    A 1 Reply Last reply
    0
    • OriginalGriffO OriginalGriff

      Yes, that's the whole point. That is what an IDENTITY field in SQL Server is. And when you use autoincremented fields you can't rely on the MAX value to be the one you inserted: you do your INSERT, then two dozen other users add their own before you issue your SELECT. YOur MAX then fetches the ID for a totally different row, and you start to get intermittent problems that are fiendishly difficult to duplicate, track down, fix, and prove. Using SCOPE_IDENTITY returns the last autoincremented value used by that connection, and will never return values from different users.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

      A Offline
      A Offline
      ago2486
      wrote on last edited by
      #5

      OK well sir and I just found her in my research, are I adapting well to my code?
        using (OleDbCommand cmd1 = sql_con.CreateCommand ())
                           {
                              
                               cmd1.CommandText = "INSERT INTO Orders (amount_com) VALUES (@montant_com)";
                               cmd1.CommandText = "SELECT @@ IDENTITY";
                               int id = Convert.ToInt32 (cmd1.ExecuteScalar ());
                               cmd1.Parameters.AddWithValue ("@ amount_com", TxtTotalCmd.Text);
                               cmd1.ExecuteNonQuery ();
                           }

      OriginalGriffO 1 Reply Last reply
      0
      • A ago2486

        OK well sir and I just found her in my research, are I adapting well to my code?
          using (OleDbCommand cmd1 = sql_con.CreateCommand ())
                             {
                                
                                 cmd1.CommandText = "INSERT INTO Orders (amount_com) VALUES (@montant_com)";
                                 cmd1.CommandText = "SELECT @@ IDENTITY";
                                 int id = Convert.ToInt32 (cmd1.ExecuteScalar ());
                                 cmd1.Parameters.AddWithValue ("@ amount_com", TxtTotalCmd.Text);
                                 cmd1.ExecuteNonQuery ();
                             }

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #6

        Stop guessing. Start thinking. Look at your code and tell me what is wrong with it. Where have we seen that problem before?

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        A 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          Stop guessing. Start thinking. Look at your code and tell me what is wrong with it. Where have we seen that problem before?

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

          A Offline
          A Offline
          ago2486
          wrote on last edited by
          #7

          Ok my apologies, this is the error I get
          System.Data.Oledb.Ole Exception (0x80040E14): Character found after the end of the Sql instruction

          using (OleDbCommand cmd1 = sql_con.CreateCommand())
          {

                              cmd1.CommandText = "INSERT INTO Commandes (montant\_com) VALUES (@montant\_com);" + "SELECT SCOPE\_IDENTITY()";
                             
                              int id;
                              cmd1.Parameters.AddWithValue("@montant\_com", TxtTotalCmd.Text);
                            
                              cmd1.ExecuteNonQuery();
                              id = (int)cmd1.ExecuteScalar();
                              TxtNunCmd.Text = id.ToString();
          
                          }
          
          OriginalGriffO 2 Replies Last reply
          0
          • A ago2486

            Ok my apologies, this is the error I get
            System.Data.Oledb.Ole Exception (0x80040E14): Character found after the end of the Sql instruction

            using (OleDbCommand cmd1 = sql_con.CreateCommand())
            {

                                cmd1.CommandText = "INSERT INTO Commandes (montant\_com) VALUES (@montant\_com);" + "SELECT SCOPE\_IDENTITY()";
                               
                                int id;
                                cmd1.Parameters.AddWithValue("@montant\_com", TxtTotalCmd.Text);
                              
                                cmd1.ExecuteNonQuery();
                                id = (int)cmd1.ExecuteScalar();
                                TxtNunCmd.Text = id.ToString();
            
                            }
            
            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #8

            That's better!

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            1 Reply Last reply
            0
            • A ago2486

              Ok my apologies, this is the error I get
              System.Data.Oledb.Ole Exception (0x80040E14): Character found after the end of the Sql instruction

              using (OleDbCommand cmd1 = sql_con.CreateCommand())
              {

                                  cmd1.CommandText = "INSERT INTO Commandes (montant\_com) VALUES (@montant\_com);" + "SELECT SCOPE\_IDENTITY()";
                                 
                                  int id;
                                  cmd1.Parameters.AddWithValue("@montant\_com", TxtTotalCmd.Text);
                                
                                  cmd1.ExecuteNonQuery();
                                  id = (int)cmd1.ExecuteScalar();
                                  TxtNunCmd.Text = id.ToString();
              
                              }
              
              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #9

              OK, the edited version makes a little more sense. You need to split your command into two: Do the INSERT, then build a new Command to do the SELECT (some DB engines don't like command chaining). As long as you use the same connection and you don't close it in between the INSERT and SELECT, you'll be fine.

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              A 2 Replies Last reply
              0
              • OriginalGriffO OriginalGriff

                OK, the edited version makes a little more sense. You need to split your command into two: Do the INSERT, then build a new Command to do the SELECT (some DB engines don't like command chaining). As long as you use the same connection and you don't close it in between the INSERT and SELECT, you'll be fine.

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                A Offline
                A Offline
                ago2486
                wrote on last edited by
                #10

                I just did it but I have an error: the function 'SCOPE_IDENTITY' not defined in the expression

                                using (OleDbCommand cmd1 = sql\_con.CreateCommand())
                                {
                                    
                                    cmd1.CommandText = "INSERT INTO Commandes (montant\_com) VALUES (@montant\_com)";
                                    cmd1.CommandText = "SELECT SCOPE\_IDENTITY()";
                                    
                                    cmd1.Parameters.AddWithValue("@montant\_com", TxtTotalCmd.Text);
                                  
                                    cmd1.ExecuteNonQuery();
                                    int id = Convert.ToInt32(cmd1.ExecuteScalar());
                                    TxtNunCmd.Text = id.ToString();
                
                                }
                
                1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  OK, the edited version makes a little more sense. You need to split your command into two: Do the INSERT, then build a new Command to do the SELECT (some DB engines don't like command chaining). As long as you use the same connection and you don't close it in between the INSERT and SELECT, you'll be fine.

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                  A Offline
                  A Offline
                  ago2486
                  wrote on last edited by
                  #11

                  and I wanted to clarify that I am on an access database

                  OriginalGriffO 1 Reply Last reply
                  0
                  • A ago2486

                    and I wanted to clarify that I am on an access database

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #12

                    Ah. That makes a difference. Access doesn't support SCOPE_IDENTITY: You need to use @@IDENTITY instead.

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    A 1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      Ah. That makes a difference. Access doesn't support SCOPE_IDENTITY: You need to use @@IDENTITY instead.

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                      A Offline
                      A Offline
                      ago2486
                      wrote on last edited by
                      #13

                      it’s already done sir but what I don't understand textbox does not recover the id, it displays zero

                      using (OleDbCommand cmd1 = sql_con.CreateCommand())
                      {

                                          cmd1.CommandText = "INSERT INTO Commandes (montant\_com) VALUES (@montant\_com)";
                                          cmd1.CommandText = "SELECT @@IDENTITY";
                                          
                                          cmd1.Parameters.AddWithValue("@montant\_com", TxtTotalCmd.Text);
                                        
                                          //sql\_cmd = new OleDbCommand(CommandText, sql\_con);
                                          cmd1.ExecuteNonQuery();
                                           id = (int)cmd1.ExecuteScalar();
                                          TxtNunCmd.Text = id.ToString();
                                      }
                      
                      OriginalGriffO 1 Reply Last reply
                      0
                      • A ago2486

                        it’s already done sir but what I don't understand textbox does not recover the id, it displays zero

                        using (OleDbCommand cmd1 = sql_con.CreateCommand())
                        {

                                            cmd1.CommandText = "INSERT INTO Commandes (montant\_com) VALUES (@montant\_com)";
                                            cmd1.CommandText = "SELECT @@IDENTITY";
                                            
                                            cmd1.Parameters.AddWithValue("@montant\_com", TxtTotalCmd.Text);
                                          
                                            //sql\_cmd = new OleDbCommand(CommandText, sql\_con);
                                            cmd1.ExecuteNonQuery();
                                             id = (int)cmd1.ExecuteScalar();
                                            TxtNunCmd.Text = id.ToString();
                                        }
                        
                        OriginalGriffO Offline
                        OriginalGriffO Offline
                        OriginalGriff
                        wrote on last edited by
                        #14

                        Do you know what the wonderful thing about banging your head on the desk is? Desk *BANG* Desk *BANG* Desk *BANG* Desk *BANG* Desk *BANG* Desk *BANG* It feels wonderful when you stop ... We've been here before. Yesterday in fact. Look at your code. What can you see that is wrong?

                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                        A L 2 Replies Last reply
                        0
                        • OriginalGriffO OriginalGriff

                          Do you know what the wonderful thing about banging your head on the desk is? Desk *BANG* Desk *BANG* Desk *BANG* Desk *BANG* Desk *BANG* Desk *BANG* It feels wonderful when you stop ... We've been here before. Yesterday in fact. Look at your code. What can you see that is wrong?

                          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                          A Offline
                          A Offline
                          ago2486
                          wrote on last edited by
                          #15

                          :doh: :doh: :doh: :doh: you are right sir and I promise you find the solution and you ride it

                          OriginalGriffO 1 Reply Last reply
                          0
                          • A ago2486

                            :doh: :doh: :doh: :doh: you are right sir and I promise you find the solution and you ride it

                            OriginalGriffO Offline
                            OriginalGriffO Offline
                            OriginalGriff
                            wrote on last edited by
                            #16

                            :laugh:

                            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                            1 Reply Last reply
                            0
                            • OriginalGriffO OriginalGriff

                              Do you know what the wonderful thing about banging your head on the desk is? Desk *BANG* Desk *BANG* Desk *BANG* Desk *BANG* Desk *BANG* Desk *BANG* It feels wonderful when you stop ... We've been here before. Yesterday in fact. Look at your code. What can you see that is wrong?

                              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                              L Offline
                              L Offline
                              Luc Pattyn
                              wrote on last edited by
                              #17

                              it probably is too early to stop. :rolleyes:

                              Luc Pattyn [My Articles] Nil Volentibus Arduum

                              OriginalGriffO 1 Reply Last reply
                              0
                              • L Luc Pattyn

                                it probably is too early to stop. :rolleyes:

                                Luc Pattyn [My Articles] Nil Volentibus Arduum

                                OriginalGriffO Offline
                                OriginalGriffO Offline
                                OriginalGriff
                                wrote on last edited by
                                #18

                                You had to say that, didn't you? :laugh: Desk *BANG* Desk *BANG* Desk *BANG* ...

                                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                                A 2 Replies Last reply
                                0
                                • OriginalGriffO OriginalGriff

                                  You had to say that, didn't you? :laugh: Desk *BANG* Desk *BANG* Desk *BANG* ...

                                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                                  A Offline
                                  A Offline
                                  ago2486
                                  wrote on last edited by
                                  #19

                                  yes yes sir I am still looking for, I will find it, I assure you sir

                                  :doh: :doh: :doh: :doh:

                                  1 Reply Last reply
                                  0
                                  • OriginalGriffO OriginalGriff

                                    You had to say that, didn't you? :laugh: Desk *BANG* Desk *BANG* Desk *BANG* ...

                                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                                    A Offline
                                    A Offline
                                    ago2486
                                    wrote on last edited by
                                    #20

                                    using (OleDbCommand cmd1 = sql_con.CreateCommand())
                                    {

                                                        cmd1.CommandText = "INSERT INTO Commandes (montant\_com) VALUES (@montant\_com)";
                                                        OleDbCommand cmr = sql\_con.CreateCommand();
                                                        cmr.CommandText = "SELECT @@IDENTITY AS LastId";
                                                        
                                                        cmd1.Parameters.AddWithValue("@montant\_com", TxtTotalCmd.Text);
                                                        id = (int)cmr.ExecuteScalar();
                                                        TxtNunCmd.Text = id.ToString();
                                                       
                                                        cmd1.ExecuteNonQuery();
                                                      
                                                    }
                                    

                                    but i can't get the id

                                    OriginalGriffO 1 Reply Last reply
                                    0
                                    • A ago2486

                                      using (OleDbCommand cmd1 = sql_con.CreateCommand())
                                      {

                                                          cmd1.CommandText = "INSERT INTO Commandes (montant\_com) VALUES (@montant\_com)";
                                                          OleDbCommand cmr = sql\_con.CreateCommand();
                                                          cmr.CommandText = "SELECT @@IDENTITY AS LastId";
                                                          
                                                          cmd1.Parameters.AddWithValue("@montant\_com", TxtTotalCmd.Text);
                                                          id = (int)cmr.ExecuteScalar();
                                                          TxtNunCmd.Text = id.ToString();
                                                         
                                                          cmd1.ExecuteNonQuery();
                                                        
                                                      }
                                      

                                      but i can't get the id

                                      OriginalGriffO Offline
                                      OriginalGriffO Offline
                                      OriginalGriff
                                      wrote on last edited by
                                      #21

                                      Stop guessing, and start thinking. When is the ID actually created? What line of code causes that to happen?

                                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                                      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                                      A 1 Reply Last reply
                                      0
                                      • OriginalGriffO OriginalGriff

                                        Stop guessing, and start thinking. When is the ID actually created? What line of code causes that to happen?

                                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                                        A Offline
                                        A Offline
                                        ago2486
                                        wrote on last edited by
                                        #22

                                        Hello sir and all my excuses for yesterday I had a connection problem that's why I disconnected but by dint of fighting all night I could find the solution regaré and tell me if the code is clean. I must say I am glad I did ...

                                        :-D :-D :-D :-D

                                        using (OleDbCommand cmd1 = sql_con.CreateCommand())
                                        {

                                                            cmd1.CommandText = "INSERT INTO Commandes (montant\_com) VALUES (@montant\_com)";
                                                            cmd1.Parameters.AddWithValue("@montant\_com", TxtTotalCmd.Text);
                                                            cmd1.ExecuteNonQuery();
                                                            OleDbCommand cmr = sql\_con.CreateCommand();
                                                            cmr.CommandText = "SELECT @@IDENTITY AS LastId";
                                                            
                                                            
                                                            int LastId = Convert.ToInt32(cmr.ExecuteScalar());
                                                            TxtNunCmd.Text = LastId.ToString();
                                                            //sql\_cmd = new OleDbCommand(CommandText, sql\_con);
                                                            
                                                          
                                                        }
                                        
                                        OriginalGriffO 1 Reply Last reply
                                        0
                                        • A ago2486

                                          Hello sir and all my excuses for yesterday I had a connection problem that's why I disconnected but by dint of fighting all night I could find the solution regaré and tell me if the code is clean. I must say I am glad I did ...

                                          :-D :-D :-D :-D

                                          using (OleDbCommand cmd1 = sql_con.CreateCommand())
                                          {

                                                              cmd1.CommandText = "INSERT INTO Commandes (montant\_com) VALUES (@montant\_com)";
                                                              cmd1.Parameters.AddWithValue("@montant\_com", TxtTotalCmd.Text);
                                                              cmd1.ExecuteNonQuery();
                                                              OleDbCommand cmr = sql\_con.CreateCommand();
                                                              cmr.CommandText = "SELECT @@IDENTITY AS LastId";
                                                              
                                                              
                                                              int LastId = Convert.ToInt32(cmr.ExecuteScalar());
                                                              TxtNunCmd.Text = LastId.ToString();
                                                              //sql\_cmd = new OleDbCommand(CommandText, sql\_con);
                                                              
                                                            
                                                          }
                                          
                                          OriginalGriffO Offline
                                          OriginalGriffO Offline
                                          OriginalGriff
                                          wrote on last edited by
                                          #23

                                          Yes, that's fine - you don't need the "AS LastID" because ExecuteScalar doesn't return labels, but it doesn't matter if you add it.

                                          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                                          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                                          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                                          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