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. Adding a row in a database

Adding a row in a database

Scheduled Pinned Locked Moved C#
csharpdatabase
9 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.
  • A Offline
    A Offline
    alee15 10 88
    wrote on last edited by
    #1

    Hi, I would like to add a record in the last row of a database from a c# program. I have a database with the first column called ID which is unique. I tried doing this by finding the maximum number in that column and then adding 1 to it but it doesn' work. Thanks

    J G 2 Replies Last reply
    0
    • A alee15 10 88

      Hi, I would like to add a record in the last row of a database from a c# program. I have a database with the first column called ID which is unique. I tried doing this by finding the maximum number in that column and then adding 1 to it but it doesn' work. Thanks

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      Standard questions 1) What database? 2) "doesnt work" - what doesnt work and what error if any 3) show your code.

      A 1 Reply Last reply
      0
      • A alee15 10 88

        Hi, I would like to add a record in the last row of a database from a c# program. I have a database with the first column called ID which is unique. I tried doing this by finding the maximum number in that column and then adding 1 to it but it doesn' work. Thanks

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        Set the id field to be a counter/identifier/auto_increment (depending on the flavour of database you are using). Then you just add a record without specifying the id at all, and it automatically gets a new unique id, without any risks of conflicts. --- b { font-weight: normal; }

        1 Reply Last reply
        0
        • J J4amieC

          Standard questions 1) What database? 2) "doesnt work" - what doesnt work and what error if any 3) show your code.

          A Offline
          A Offline
          alee15 10 88
          wrote on last edited by
          #4

          I have a database with some questions and each question has a unique ID. In my c# program I have a form with a datagrid showing all these questions and 2 textboxes, 1 showing the id of the question (txtQstID) and the other showing the question (txtQst). Then I have a button (btnAdd) that when I clicks it a new question is added in the datagrid. The new question is entered by the user in the txtQst textbox. But the ID of the question textbox is readonly so the user cannot enter anything in it. So I did the following in the add button: - //getting the maximum ID from the Qst_ID column System.Data.OleDb.OleDbCommand oCommandMax = new System.Data.OleDb.OleDbCommand("SELECT MAX(Qst_ID) FROM Questions", conn); object objMax = oCommandMax.ExecuteScalar(); string maxValue = objMax.ToString(); //parsing the maxValue to integer int newRecord = (int.Parse(maxValue)); //adding 1 to the maximum number newRecord++; //displaying the new ID in the txtQstID textbox this.txtQstID.Text = (newRecord.ToString()); //CreateNewQuestion is a method found in Class 1 Class1.CreateNewQuestion(txtQstID.Text, txtQst.Text); //The following is the code found in Class1: - public static void CreateNewQuestion(string ID, string Question) { DataRow drNew = DS.Tables["Questions"].NewRow(); drNew["Question_ID"] = ID; drNew["Question"] = Question; DS.Tables["Questions"].Rows.Add(drNew); }

          J A 2 Replies Last reply
          0
          • A alee15 10 88

            I have a database with some questions and each question has a unique ID. In my c# program I have a form with a datagrid showing all these questions and 2 textboxes, 1 showing the id of the question (txtQstID) and the other showing the question (txtQst). Then I have a button (btnAdd) that when I clicks it a new question is added in the datagrid. The new question is entered by the user in the txtQst textbox. But the ID of the question textbox is readonly so the user cannot enter anything in it. So I did the following in the add button: - //getting the maximum ID from the Qst_ID column System.Data.OleDb.OleDbCommand oCommandMax = new System.Data.OleDb.OleDbCommand("SELECT MAX(Qst_ID) FROM Questions", conn); object objMax = oCommandMax.ExecuteScalar(); string maxValue = objMax.ToString(); //parsing the maxValue to integer int newRecord = (int.Parse(maxValue)); //adding 1 to the maximum number newRecord++; //displaying the new ID in the txtQstID textbox this.txtQstID.Text = (newRecord.ToString()); //CreateNewQuestion is a method found in Class 1 Class1.CreateNewQuestion(txtQstID.Text, txtQst.Text); //The following is the code found in Class1: - public static void CreateNewQuestion(string ID, string Question) { DataRow drNew = DS.Tables["Questions"].NewRow(); drNew["Question_ID"] = ID; drNew["Question"] = Question; DS.Tables["Questions"].Rows.Add(drNew); }

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            See Guffa's response below...the key in the database should generate itself - generating that key by adding 1 to the highest number will be errorprone. -- modified at 4:37 Thursday 13th April, 2006

            1 Reply Last reply
            0
            • A alee15 10 88

              I have a database with some questions and each question has a unique ID. In my c# program I have a form with a datagrid showing all these questions and 2 textboxes, 1 showing the id of the question (txtQstID) and the other showing the question (txtQst). Then I have a button (btnAdd) that when I clicks it a new question is added in the datagrid. The new question is entered by the user in the txtQst textbox. But the ID of the question textbox is readonly so the user cannot enter anything in it. So I did the following in the add button: - //getting the maximum ID from the Qst_ID column System.Data.OleDb.OleDbCommand oCommandMax = new System.Data.OleDb.OleDbCommand("SELECT MAX(Qst_ID) FROM Questions", conn); object objMax = oCommandMax.ExecuteScalar(); string maxValue = objMax.ToString(); //parsing the maxValue to integer int newRecord = (int.Parse(maxValue)); //adding 1 to the maximum number newRecord++; //displaying the new ID in the txtQstID textbox this.txtQstID.Text = (newRecord.ToString()); //CreateNewQuestion is a method found in Class 1 Class1.CreateNewQuestion(txtQstID.Text, txtQst.Text); //The following is the code found in Class1: - public static void CreateNewQuestion(string ID, string Question) { DataRow drNew = DS.Tables["Questions"].NewRow(); drNew["Question_ID"] = ID; drNew["Question"] = Question; DS.Tables["Questions"].Rows.Add(drNew); }

              A Offline
              A Offline
              albCode
              wrote on last edited by
              #6

              . . . System.Data.OleDb.OleDbCommand oCommandMax = new System.Data.OleDb.OleDbCommand("SELECT top1 * FROM Questions order by Question_ID desc", conn); . . . //insetead newRecord++; increase by one int newRecord = (int.Parse(maxValue)+1);

              J 1 Reply Last reply
              0
              • A albCode

                . . . System.Data.OleDb.OleDbCommand oCommandMax = new System.Data.OleDb.OleDbCommand("SELECT top1 * FROM Questions order by Question_ID desc", conn); . . . //insetead newRecord++; increase by one int newRecord = (int.Parse(maxValue)+1);

                J Offline
                J Offline
                J4amieC
                wrote on last edited by
                #7

                And then what happens when 2 users execute this code simultaneously?

                G 1 Reply Last reply
                0
                • J J4amieC

                  And then what happens when 2 users execute this code simultaneously?

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #8

                  One of them will get an error message because they try to create a record with the same key. --- b { font-weight: normal; }

                  J 1 Reply Last reply
                  0
                  • G Guffa

                    One of them will get an error message because they try to create a record with the same key. --- b { font-weight: normal; }

                    J Offline
                    J Offline
                    J4amieC
                    wrote on last edited by
                    #9

                    Correct, next question for 10 points :-D

                    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