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

    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