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 while inserting a new row in database from C# application

Problem while inserting a new row in database from C# application

Scheduled Pinned Locked Moved C#
databasehelpcsharpquestion
11 Posts 5 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 AprNgp

    I need some help in my project. I am trying to insert a row in my database table using a SQL query from my dataset. If I execute the query from query builder, it is able to insert a new row to my database table. But, as I am trying to use the same query from my code, and try to view the rows from my code (using a getdata() query), the newly inserted row is shown, but if I close the my project, I run it after some time, the recently inserted row is gone ..... I checked the database, but the new row is not there ..... :( What can be wrong ? Please provide me some guidance ...

    Apurv “Never trust a computer you can’t throw out a window.” (Steve Wozniak) “There are only two industries that refer to their customers as ‘users’.” (Edward Tufte)

    D Offline
    D Offline
    dan sh
    wrote on last edited by
    #2

    Can you the insertion code you are using?

    50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

    A 1 Reply Last reply
    0
    • D dan sh

      Can you the insertion code you are using?

      50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

      A Offline
      A Offline
      AprNgp
      wrote on last edited by
      #3

      INSERT INTO PatientsDetails
      (FirstName, LastName, DateOfBirth, BloodGroupId, Gender)
      VALUES (@FirstName,@LastName,@DateOfBirth,@BloodGroupId,@Gender);
      SELECT ReferenceId, FirstName, LastName, DateOfBirth, BloodGroupId, Gender FROM PatientsDetails WHERE (ReferenceId = SCOPE_IDENTITY())

      Apurv “Never trust a computer you can’t throw out a window.” (Steve Wozniak) “There are only two industries that refer to their customers as ‘users’.” (Edward Tufte)

      A 1 Reply Last reply
      0
      • A AprNgp

        INSERT INTO PatientsDetails
        (FirstName, LastName, DateOfBirth, BloodGroupId, Gender)
        VALUES (@FirstName,@LastName,@DateOfBirth,@BloodGroupId,@Gender);
        SELECT ReferenceId, FirstName, LastName, DateOfBirth, BloodGroupId, Gender FROM PatientsDetails WHERE (ReferenceId = SCOPE_IDENTITY())

        Apurv “Never trust a computer you can’t throw out a window.” (Steve Wozniak) “There are only two industries that refer to their customers as ‘users’.” (Edward Tufte)

        A Offline
        A Offline
        AprNgp
        wrote on last edited by
        #4

        I modified the above query as

        INSERT INTO PatientsDetails
        (FirstName, LastName, DateOfBirth, BloodGroupId, Gender)
        VALUES (@FirstName,@LastName,@DateOfBirth,@BloodGroupId,@Gender);

        Still its happening the same ...

        Apurv “Never trust a computer you can’t throw out a window.” (Steve Wozniak) “There are only two industries that refer to their customers as ‘users’.” (Edward Tufte)

        OriginalGriffO D 2 Replies Last reply
        0
        • A AprNgp

          I modified the above query as

          INSERT INTO PatientsDetails
          (FirstName, LastName, DateOfBirth, BloodGroupId, Gender)
          VALUES (@FirstName,@LastName,@DateOfBirth,@BloodGroupId,@Gender);

          Still its happening the same ...

          Apurv “Never trust a computer you can’t throw out a window.” (Steve Wozniak) “There are only two industries that refer to their customers as ‘users’.” (Edward Tufte)

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

          This may sound stupid, but are you querying the same DB programatically, and via the SQl query? Have you checked your connection string?

          No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones "Rumour has it that if you play Microsoft CDs backwards you will hear Satanic messages.Worse still, is that if you play them forwards they will install Windows"

          "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 AprNgp

            I modified the above query as

            INSERT INTO PatientsDetails
            (FirstName, LastName, DateOfBirth, BloodGroupId, Gender)
            VALUES (@FirstName,@LastName,@DateOfBirth,@BloodGroupId,@Gender);

            Still its happening the same ...

            Apurv “Never trust a computer you can’t throw out a window.” (Steve Wozniak) “There are only two industries that refer to their customers as ‘users’.” (Edward Tufte)

            D Offline
            D Offline
            dan sh
            wrote on last edited by
            #6

            I meant the C# code. If this query is working through management studio, it must be correct. There has to something wrong in your code.

            50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

            A 1 Reply Last reply
            0
            • D dan sh

              I meant the C# code. If this query is working through management studio, it must be correct. There has to something wrong in your code.

              50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

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

              private void btnSave_Click(object sender, EventArgs e)
              {
              try
              {
              if (txtFirstName.Text == "")
              MessageBox.Show("Patient's First Name can't be blank", "Incomplete data", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
              else if (txtDOB.Text == "")
              MessageBox.Show("Patient's Date of Birth can't be blank", "Incomplete data", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                          if (txtLastName.Text == "")
                              txtLastName.Text = " ";
                          bool gender = true;
                          if (!rbMale.Checked)
                              gender = false;
              
                          MRMDbDataSetTableAdapters.PatientsDetailsTableAdapter pdta = new MediRecordManager.MRMDbDataSetTableAdapters.PatientsDetailsTableAdapter();
                          if (pdta.AddNewPatient(txtFirstName.Text, txtLastName.Text, calDOB.SelectionStart, (byte)cbBloodGroup.SelectedValue, gender) > 0)
                          {
                              MessageBox.Show("Data inserted to database !");
                              btnCancel.Text = "Close";
                          }
                      }
                      catch (Exception ex)
                      {
                          MessageBox.Show(ex.Message);
                      }
                  }
              }
              

              Apurv “Never trust a computer you can’t throw out a window.” (Steve Wozniak) “There are only two industries that refer to their customers as ‘users’.” (Edward Tufte)

              D 1 Reply Last reply
              0
              • A AprNgp

                I need some help in my project. I am trying to insert a row in my database table using a SQL query from my dataset. If I execute the query from query builder, it is able to insert a new row to my database table. But, as I am trying to use the same query from my code, and try to view the rows from my code (using a getdata() query), the newly inserted row is shown, but if I close the my project, I run it after some time, the recently inserted row is gone ..... I checked the database, but the new row is not there ..... :( What can be wrong ? Please provide me some guidance ...

                Apurv “Never trust a computer you can’t throw out a window.” (Steve Wozniak) “There are only two industries that refer to their customers as ‘users’.” (Edward Tufte)

                C Offline
                C Offline
                Covean
                wrote on last edited by
                #8

                Are you using an access database? If not just ignore the rest of this posting. Some time ago, someone had the same problem with an access database. His problem was, that he added the empty .mdb file to the project, and every time he build his solution the empty database has overwritten the one with data in it.

                Greetings Covean

                A 1 Reply Last reply
                0
                • C Covean

                  Are you using an access database? If not just ignore the rest of this posting. Some time ago, someone had the same problem with an access database. His problem was, that he added the empty .mdb file to the project, and every time he build his solution the empty database has overwritten the one with data in it.

                  Greetings Covean

                  A Offline
                  A Offline
                  AprNgp
                  wrote on last edited by
                  #9

                  I am using MS SQL 2008 server database

                  Apurv “Never trust a computer you can’t throw out a window.” (Steve Wozniak) “There are only two industries that refer to their customers as ‘users’.” (Edward Tufte)

                  C 1 Reply Last reply
                  0
                  • A AprNgp

                    I am using MS SQL 2008 server database

                    Apurv “Never trust a computer you can’t throw out a window.” (Steve Wozniak) “There are only two industries that refer to their customers as ‘users’.” (Edward Tufte)

                    C Offline
                    C Offline
                    Covean
                    wrote on last edited by
                    #10

                    Do you use transactions (maybe indirect?)? Thats the last idea I have for solving your problem.

                    Greetings Covean

                    1 Reply Last reply
                    0
                    • A AprNgp

                      private void btnSave_Click(object sender, EventArgs e)
                      {
                      try
                      {
                      if (txtFirstName.Text == "")
                      MessageBox.Show("Patient's First Name can't be blank", "Incomplete data", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                      else if (txtDOB.Text == "")
                      MessageBox.Show("Patient's Date of Birth can't be blank", "Incomplete data", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                                  if (txtLastName.Text == "")
                                      txtLastName.Text = " ";
                                  bool gender = true;
                                  if (!rbMale.Checked)
                                      gender = false;
                      
                                  MRMDbDataSetTableAdapters.PatientsDetailsTableAdapter pdta = new MediRecordManager.MRMDbDataSetTableAdapters.PatientsDetailsTableAdapter();
                                  if (pdta.AddNewPatient(txtFirstName.Text, txtLastName.Text, calDOB.SelectionStart, (byte)cbBloodGroup.SelectedValue, gender) > 0)
                                  {
                                      MessageBox.Show("Data inserted to database !");
                                      btnCancel.Text = "Close";
                                  }
                              }
                              catch (Exception ex)
                              {
                                  MessageBox.Show(ex.Message);
                              }
                          }
                      }
                      

                      Apurv “Never trust a computer you can’t throw out a window.” (Steve Wozniak) “There are only two industries that refer to their customers as ‘users’.” (Edward Tufte)

                      D Offline
                      D Offline
                      dojohansen
                      wrote on last edited by
                      #11

                      You are just adding the row to a table in your in-memory dataset. At least that's all the code you've posted does. To save it to the database, call Update() on the data adapter created for your typed data set.

                      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