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. Database & SysAdmin
  3. Database
  4. getting last inserted row

getting last inserted row

Scheduled Pinned Locked Moved Database
csharpdatabasemysqlquestionannouncement
5 Posts 4 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.
  • M Offline
    M Offline
    Member 10263519
    wrote on last edited by
    #1

    Hi, we r developing visitor tracking system,when avisitor come giving a card and he swips and enters inside ,when he releaving handovers the card to watchmen.after that there is a chance same card can be given to someother visitor. mysql table is not having primary key. table is like this: cardid name intime outtime -------------------------------------------- 123 raj 1/2/2014 10:12 1/2/2014 11:00 234 rajee 1/2/2014 10:34 123 vani 1/2/2014 12:10 then here how can i get the last inserted row of id(123) to update the outtime am using c# following code:

    String intime = "";
    String outtime = "";

                String indatetime=System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                String outdatetime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
    
                using (MySqlConnection con = new MySqlConnection(ConnectionString))
                {
                    String query = "select \* from visitor where card\_id=@id ORDER BY card\_id DESC LIMIT 1";
                    MySqlCommand command = new MySqlCommand(query, con);
                    command.Parameters.AddWithValue("@id", txtAddVisitorCardId.Text);
                    con.Open();
                    using (MySqlDataReader rdr = command.ExecuteReader())
                    {
                        if (rdr.Read())
                        {
                            intime = rdr\["in\_time"\].ToString();
                            outtime = rdr\["out\_time"\].ToString();
                        }
                    }
    
                }
    
                if (((intime == "") && (outtime == "")) || ((intime!="")&&(outtime !="")))
                {
                    using (MySqlConnection con = new MySqlConnection(ConnectionString))
                    {
                        String query = "insert into visitor(card\_id,name,age,address,id\_proof,contact\_person,purpose,in\_time) values(@id,@name,@age,@address,@idproof,@contact,@purpose,@in)";
                        MySqlCommand command = new MySqlCommand(query, con);
                        command.Parameters.AddWithValue("@id", txtAddVisitorCardId.Text);
                        command.Parameters.AddWithValue("@name", txtAddVisitorName.Text);
                        command.Parameters.AddWithValue("@age", txtAddVisitorAge.Text);
                        command.Parameters.AddWithValue("@address", txtAddVisitorAddress.Text);
    
    P B 2 Replies Last reply
    0
    • M Member 10263519

      Hi, we r developing visitor tracking system,when avisitor come giving a card and he swips and enters inside ,when he releaving handovers the card to watchmen.after that there is a chance same card can be given to someother visitor. mysql table is not having primary key. table is like this: cardid name intime outtime -------------------------------------------- 123 raj 1/2/2014 10:12 1/2/2014 11:00 234 rajee 1/2/2014 10:34 123 vani 1/2/2014 12:10 then here how can i get the last inserted row of id(123) to update the outtime am using c# following code:

      String intime = "";
      String outtime = "";

                  String indatetime=System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                  String outdatetime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
      
                  using (MySqlConnection con = new MySqlConnection(ConnectionString))
                  {
                      String query = "select \* from visitor where card\_id=@id ORDER BY card\_id DESC LIMIT 1";
                      MySqlCommand command = new MySqlCommand(query, con);
                      command.Parameters.AddWithValue("@id", txtAddVisitorCardId.Text);
                      con.Open();
                      using (MySqlDataReader rdr = command.ExecuteReader())
                      {
                          if (rdr.Read())
                          {
                              intime = rdr\["in\_time"\].ToString();
                              outtime = rdr\["out\_time"\].ToString();
                          }
                      }
      
                  }
      
                  if (((intime == "") && (outtime == "")) || ((intime!="")&&(outtime !="")))
                  {
                      using (MySqlConnection con = new MySqlConnection(ConnectionString))
                      {
                          String query = "insert into visitor(card\_id,name,age,address,id\_proof,contact\_person,purpose,in\_time) values(@id,@name,@age,@address,@idproof,@contact,@purpose,@in)";
                          MySqlCommand command = new MySqlCommand(query, con);
                          command.Parameters.AddWithValue("@id", txtAddVisitorCardId.Text);
                          command.Parameters.AddWithValue("@name", txtAddVisitorName.Text);
                          command.Parameters.AddWithValue("@age", txtAddVisitorAge.Text);
                          command.Parameters.AddWithValue("@address", txtAddVisitorAddress.Text);
      
      P Offline
      P Offline
      Peter Leow
      wrote on last edited by
      #2

      refer: get-the-id-of-inserted-row-using-c-sharp[^]

      M 1 Reply Last reply
      0
      • P Peter Leow

        refer: get-the-id-of-inserted-row-using-c-sharp[^]

        M Offline
        M Offline
        Member 10263519
        wrote on last edited by
        #3

        i want the query plz help

        M 1 Reply Last reply
        0
        • M Member 10263519

          Hi, we r developing visitor tracking system,when avisitor come giving a card and he swips and enters inside ,when he releaving handovers the card to watchmen.after that there is a chance same card can be given to someother visitor. mysql table is not having primary key. table is like this: cardid name intime outtime -------------------------------------------- 123 raj 1/2/2014 10:12 1/2/2014 11:00 234 rajee 1/2/2014 10:34 123 vani 1/2/2014 12:10 then here how can i get the last inserted row of id(123) to update the outtime am using c# following code:

          String intime = "";
          String outtime = "";

                      String indatetime=System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                      String outdatetime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
          
                      using (MySqlConnection con = new MySqlConnection(ConnectionString))
                      {
                          String query = "select \* from visitor where card\_id=@id ORDER BY card\_id DESC LIMIT 1";
                          MySqlCommand command = new MySqlCommand(query, con);
                          command.Parameters.AddWithValue("@id", txtAddVisitorCardId.Text);
                          con.Open();
                          using (MySqlDataReader rdr = command.ExecuteReader())
                          {
                              if (rdr.Read())
                              {
                                  intime = rdr\["in\_time"\].ToString();
                                  outtime = rdr\["out\_time"\].ToString();
                              }
                          }
          
                      }
          
                      if (((intime == "") && (outtime == "")) || ((intime!="")&&(outtime !="")))
                      {
                          using (MySqlConnection con = new MySqlConnection(ConnectionString))
                          {
                              String query = "insert into visitor(card\_id,name,age,address,id\_proof,contact\_person,purpose,in\_time) values(@id,@name,@age,@address,@idproof,@contact,@purpose,@in)";
                              MySqlCommand command = new MySqlCommand(query, con);
                              command.Parameters.AddWithValue("@id", txtAddVisitorCardId.Text);
                              command.Parameters.AddWithValue("@name", txtAddVisitorName.Text);
                              command.Parameters.AddWithValue("@age", txtAddVisitorAge.Text);
                              command.Parameters.AddWithValue("@address", txtAddVisitorAddress.Text);
          
          B Offline
          B Offline
          Bernhard Hiller
          wrote on last edited by
          #4

          Reposts are bad behavior. how get latest recordfrom mysql table[^]

          1 Reply Last reply
          0
          • M Member 10263519

            i want the query plz help

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            Write your own code!

            Never underestimate the power of human stupidity RAH

            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