When editing database, then adding multiple lines in a datagridview, the lines duplicate











up vote
0
down vote

favorite












I have a program where the user can add profiles, and also edit them. I wanted to make the datagrid view easier to view so I added multiple lines to it, but when I go back to edit the row, make changes to the data base, then save, it adds a whole new set of lines! How can I prevent this? I've tried clearing the datagridview withdataGridView1.Rows.Clear(); but that throws this error: System.ArgumentException: 'Cannot clear this list.'
Here is the code I have written to add multiple lines:



        private void pictureBox1_Click(object sender, EventArgs e)
{
tabTasks.SelectedIndex = 1;
RefreshDBConnection();
dataGridView1.Rows[0].Cells["FIRST NAME"].Value = dataGridView1.Rows[0].Cells["FIRST NAME"].Value + " " + dataGridView1.Rows[0].Cells["LAST NAME"].Value + Environment.NewLine + dataGridView1.Rows[0].Cells["ADDRESS #1"].Value + " " + dataGridView1.Rows[0].Cells["ADDRESS #2"].Value + Environment.NewLine + dataGridView1.Rows[0].Cells["CITY"].Value + ", " + dataGridView1.Rows[0].Cells["STATE"].Value + " " + dataGridView1.Rows[0].Cells["ZIP CODE"].Value + Environment.NewLine + dataGridView1.Rows[0].Cells["COUNTRY"].Value + Environment.NewLine + dataGridView1.Rows[0].Cells["PHONE"].Value + Environment.NewLine + dataGridView1.Rows[0].Cells["EMAIL"].Value;
}


And here are tthe lines for editing the profile, if this helps:



        private void buttonSave_Click(object sender, EventArgs e)
{
ConnectToDataBase();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
//string query = "update Profiles set [PROFILE NAME]='" + textBox1.Text + "', [LOGIN EMAIL]='" + textBox2.Text + "', [PASSWORD]='" + textBox3.Text + "', [FULL NAME]='" + textBox4.Text + "', [CARD NUMBER]='" + textBox5.Text + "', [EXP MONTH]='" + comboBox1.Text + "', [EXP YEAR]='" + comboBox2.Text + "', CVV='" + textBox6.Text + "' where ID='" + Convert.ToInt32(textBox7.Text) + "'";
string query = "update Profiles set [PROFILE NAME]= @Profile, [FIRST NAME]= @FName, [LAST NAME]= @LName, [ADDRESS #1]= @Add1, [ADDRESS #2]= @Add2, [CITY]= @City, [ZIP CODE]= @Zip, [STATE]= @State, [COUNTRY]= @Country, [PHONE]= @Phone, [EMAIL]= @Email, [CARD TYPE]= @CardType, [CARD NUMBER]= @CardN, [EXP MONTH]= @EXPM, [EXP YEAR]= @EXPY, CVV= @CVV where ID = @Id";
command.Parameters.AddWithValue("@Profile", txtProfile.Text);
command.Parameters.AddWithValue("@FName", txtFirstName.Text);
command.Parameters.AddWithValue("@LName", txtLastName.Text);
command.Parameters.AddWithValue("@Add1", txtAddress1.Text);
command.Parameters.AddWithValue("@Add2", txtAddress2.Text);
command.Parameters.AddWithValue("@City", txtCity.Text);
command.Parameters.AddWithValue("@Zip", txtZip.Text);
command.Parameters.AddWithValue("@State", comState.Text);
command.Parameters.AddWithValue("@Country", comCountry.Text);
command.Parameters.AddWithValue("@Phone", txtPhone.Text);
command.Parameters.AddWithValue("@Email", txtEmail.Text);
command.Parameters.AddWithValue("@CardType", comCardType.Text);
command.Parameters.AddWithValue("@CardN", txtCard.Text);
command.Parameters.AddWithValue("@EXPM", comboEXPM.Text);
command.Parameters.AddWithValue("@EXPY", comboEXPY.Text);
command.Parameters.AddWithValue("@CVV", txtCVV.Text);
command.Parameters.AddWithValue("@ID", txtID.Text);
command.CommandText = query;
command.ExecuteNonQuery();
RefreshDBConnection();
connection.Close();
MessageBox.Show("Profile Edited");
this.Close();
}


and finally, here is a picture of what happens when I edit then save, with some info blocked out, for obvious reasons: Error with lines



Please let me know if I can give you any more info, thank you! (using c# btw)










share|improve this question




























    up vote
    0
    down vote

    favorite












    I have a program where the user can add profiles, and also edit them. I wanted to make the datagrid view easier to view so I added multiple lines to it, but when I go back to edit the row, make changes to the data base, then save, it adds a whole new set of lines! How can I prevent this? I've tried clearing the datagridview withdataGridView1.Rows.Clear(); but that throws this error: System.ArgumentException: 'Cannot clear this list.'
    Here is the code I have written to add multiple lines:



            private void pictureBox1_Click(object sender, EventArgs e)
    {
    tabTasks.SelectedIndex = 1;
    RefreshDBConnection();
    dataGridView1.Rows[0].Cells["FIRST NAME"].Value = dataGridView1.Rows[0].Cells["FIRST NAME"].Value + " " + dataGridView1.Rows[0].Cells["LAST NAME"].Value + Environment.NewLine + dataGridView1.Rows[0].Cells["ADDRESS #1"].Value + " " + dataGridView1.Rows[0].Cells["ADDRESS #2"].Value + Environment.NewLine + dataGridView1.Rows[0].Cells["CITY"].Value + ", " + dataGridView1.Rows[0].Cells["STATE"].Value + " " + dataGridView1.Rows[0].Cells["ZIP CODE"].Value + Environment.NewLine + dataGridView1.Rows[0].Cells["COUNTRY"].Value + Environment.NewLine + dataGridView1.Rows[0].Cells["PHONE"].Value + Environment.NewLine + dataGridView1.Rows[0].Cells["EMAIL"].Value;
    }


    And here are tthe lines for editing the profile, if this helps:



            private void buttonSave_Click(object sender, EventArgs e)
    {
    ConnectToDataBase();
    OleDbCommand command = new OleDbCommand();
    command.Connection = connection;
    //string query = "update Profiles set [PROFILE NAME]='" + textBox1.Text + "', [LOGIN EMAIL]='" + textBox2.Text + "', [PASSWORD]='" + textBox3.Text + "', [FULL NAME]='" + textBox4.Text + "', [CARD NUMBER]='" + textBox5.Text + "', [EXP MONTH]='" + comboBox1.Text + "', [EXP YEAR]='" + comboBox2.Text + "', CVV='" + textBox6.Text + "' where ID='" + Convert.ToInt32(textBox7.Text) + "'";
    string query = "update Profiles set [PROFILE NAME]= @Profile, [FIRST NAME]= @FName, [LAST NAME]= @LName, [ADDRESS #1]= @Add1, [ADDRESS #2]= @Add2, [CITY]= @City, [ZIP CODE]= @Zip, [STATE]= @State, [COUNTRY]= @Country, [PHONE]= @Phone, [EMAIL]= @Email, [CARD TYPE]= @CardType, [CARD NUMBER]= @CardN, [EXP MONTH]= @EXPM, [EXP YEAR]= @EXPY, CVV= @CVV where ID = @Id";
    command.Parameters.AddWithValue("@Profile", txtProfile.Text);
    command.Parameters.AddWithValue("@FName", txtFirstName.Text);
    command.Parameters.AddWithValue("@LName", txtLastName.Text);
    command.Parameters.AddWithValue("@Add1", txtAddress1.Text);
    command.Parameters.AddWithValue("@Add2", txtAddress2.Text);
    command.Parameters.AddWithValue("@City", txtCity.Text);
    command.Parameters.AddWithValue("@Zip", txtZip.Text);
    command.Parameters.AddWithValue("@State", comState.Text);
    command.Parameters.AddWithValue("@Country", comCountry.Text);
    command.Parameters.AddWithValue("@Phone", txtPhone.Text);
    command.Parameters.AddWithValue("@Email", txtEmail.Text);
    command.Parameters.AddWithValue("@CardType", comCardType.Text);
    command.Parameters.AddWithValue("@CardN", txtCard.Text);
    command.Parameters.AddWithValue("@EXPM", comboEXPM.Text);
    command.Parameters.AddWithValue("@EXPY", comboEXPY.Text);
    command.Parameters.AddWithValue("@CVV", txtCVV.Text);
    command.Parameters.AddWithValue("@ID", txtID.Text);
    command.CommandText = query;
    command.ExecuteNonQuery();
    RefreshDBConnection();
    connection.Close();
    MessageBox.Show("Profile Edited");
    this.Close();
    }


    and finally, here is a picture of what happens when I edit then save, with some info blocked out, for obvious reasons: Error with lines



    Please let me know if I can give you any more info, thank you! (using c# btw)










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a program where the user can add profiles, and also edit them. I wanted to make the datagrid view easier to view so I added multiple lines to it, but when I go back to edit the row, make changes to the data base, then save, it adds a whole new set of lines! How can I prevent this? I've tried clearing the datagridview withdataGridView1.Rows.Clear(); but that throws this error: System.ArgumentException: 'Cannot clear this list.'
      Here is the code I have written to add multiple lines:



              private void pictureBox1_Click(object sender, EventArgs e)
      {
      tabTasks.SelectedIndex = 1;
      RefreshDBConnection();
      dataGridView1.Rows[0].Cells["FIRST NAME"].Value = dataGridView1.Rows[0].Cells["FIRST NAME"].Value + " " + dataGridView1.Rows[0].Cells["LAST NAME"].Value + Environment.NewLine + dataGridView1.Rows[0].Cells["ADDRESS #1"].Value + " " + dataGridView1.Rows[0].Cells["ADDRESS #2"].Value + Environment.NewLine + dataGridView1.Rows[0].Cells["CITY"].Value + ", " + dataGridView1.Rows[0].Cells["STATE"].Value + " " + dataGridView1.Rows[0].Cells["ZIP CODE"].Value + Environment.NewLine + dataGridView1.Rows[0].Cells["COUNTRY"].Value + Environment.NewLine + dataGridView1.Rows[0].Cells["PHONE"].Value + Environment.NewLine + dataGridView1.Rows[0].Cells["EMAIL"].Value;
      }


      And here are tthe lines for editing the profile, if this helps:



              private void buttonSave_Click(object sender, EventArgs e)
      {
      ConnectToDataBase();
      OleDbCommand command = new OleDbCommand();
      command.Connection = connection;
      //string query = "update Profiles set [PROFILE NAME]='" + textBox1.Text + "', [LOGIN EMAIL]='" + textBox2.Text + "', [PASSWORD]='" + textBox3.Text + "', [FULL NAME]='" + textBox4.Text + "', [CARD NUMBER]='" + textBox5.Text + "', [EXP MONTH]='" + comboBox1.Text + "', [EXP YEAR]='" + comboBox2.Text + "', CVV='" + textBox6.Text + "' where ID='" + Convert.ToInt32(textBox7.Text) + "'";
      string query = "update Profiles set [PROFILE NAME]= @Profile, [FIRST NAME]= @FName, [LAST NAME]= @LName, [ADDRESS #1]= @Add1, [ADDRESS #2]= @Add2, [CITY]= @City, [ZIP CODE]= @Zip, [STATE]= @State, [COUNTRY]= @Country, [PHONE]= @Phone, [EMAIL]= @Email, [CARD TYPE]= @CardType, [CARD NUMBER]= @CardN, [EXP MONTH]= @EXPM, [EXP YEAR]= @EXPY, CVV= @CVV where ID = @Id";
      command.Parameters.AddWithValue("@Profile", txtProfile.Text);
      command.Parameters.AddWithValue("@FName", txtFirstName.Text);
      command.Parameters.AddWithValue("@LName", txtLastName.Text);
      command.Parameters.AddWithValue("@Add1", txtAddress1.Text);
      command.Parameters.AddWithValue("@Add2", txtAddress2.Text);
      command.Parameters.AddWithValue("@City", txtCity.Text);
      command.Parameters.AddWithValue("@Zip", txtZip.Text);
      command.Parameters.AddWithValue("@State", comState.Text);
      command.Parameters.AddWithValue("@Country", comCountry.Text);
      command.Parameters.AddWithValue("@Phone", txtPhone.Text);
      command.Parameters.AddWithValue("@Email", txtEmail.Text);
      command.Parameters.AddWithValue("@CardType", comCardType.Text);
      command.Parameters.AddWithValue("@CardN", txtCard.Text);
      command.Parameters.AddWithValue("@EXPM", comboEXPM.Text);
      command.Parameters.AddWithValue("@EXPY", comboEXPY.Text);
      command.Parameters.AddWithValue("@CVV", txtCVV.Text);
      command.Parameters.AddWithValue("@ID", txtID.Text);
      command.CommandText = query;
      command.ExecuteNonQuery();
      RefreshDBConnection();
      connection.Close();
      MessageBox.Show("Profile Edited");
      this.Close();
      }


      and finally, here is a picture of what happens when I edit then save, with some info blocked out, for obvious reasons: Error with lines



      Please let me know if I can give you any more info, thank you! (using c# btw)










      share|improve this question















      I have a program where the user can add profiles, and also edit them. I wanted to make the datagrid view easier to view so I added multiple lines to it, but when I go back to edit the row, make changes to the data base, then save, it adds a whole new set of lines! How can I prevent this? I've tried clearing the datagridview withdataGridView1.Rows.Clear(); but that throws this error: System.ArgumentException: 'Cannot clear this list.'
      Here is the code I have written to add multiple lines:



              private void pictureBox1_Click(object sender, EventArgs e)
      {
      tabTasks.SelectedIndex = 1;
      RefreshDBConnection();
      dataGridView1.Rows[0].Cells["FIRST NAME"].Value = dataGridView1.Rows[0].Cells["FIRST NAME"].Value + " " + dataGridView1.Rows[0].Cells["LAST NAME"].Value + Environment.NewLine + dataGridView1.Rows[0].Cells["ADDRESS #1"].Value + " " + dataGridView1.Rows[0].Cells["ADDRESS #2"].Value + Environment.NewLine + dataGridView1.Rows[0].Cells["CITY"].Value + ", " + dataGridView1.Rows[0].Cells["STATE"].Value + " " + dataGridView1.Rows[0].Cells["ZIP CODE"].Value + Environment.NewLine + dataGridView1.Rows[0].Cells["COUNTRY"].Value + Environment.NewLine + dataGridView1.Rows[0].Cells["PHONE"].Value + Environment.NewLine + dataGridView1.Rows[0].Cells["EMAIL"].Value;
      }


      And here are tthe lines for editing the profile, if this helps:



              private void buttonSave_Click(object sender, EventArgs e)
      {
      ConnectToDataBase();
      OleDbCommand command = new OleDbCommand();
      command.Connection = connection;
      //string query = "update Profiles set [PROFILE NAME]='" + textBox1.Text + "', [LOGIN EMAIL]='" + textBox2.Text + "', [PASSWORD]='" + textBox3.Text + "', [FULL NAME]='" + textBox4.Text + "', [CARD NUMBER]='" + textBox5.Text + "', [EXP MONTH]='" + comboBox1.Text + "', [EXP YEAR]='" + comboBox2.Text + "', CVV='" + textBox6.Text + "' where ID='" + Convert.ToInt32(textBox7.Text) + "'";
      string query = "update Profiles set [PROFILE NAME]= @Profile, [FIRST NAME]= @FName, [LAST NAME]= @LName, [ADDRESS #1]= @Add1, [ADDRESS #2]= @Add2, [CITY]= @City, [ZIP CODE]= @Zip, [STATE]= @State, [COUNTRY]= @Country, [PHONE]= @Phone, [EMAIL]= @Email, [CARD TYPE]= @CardType, [CARD NUMBER]= @CardN, [EXP MONTH]= @EXPM, [EXP YEAR]= @EXPY, CVV= @CVV where ID = @Id";
      command.Parameters.AddWithValue("@Profile", txtProfile.Text);
      command.Parameters.AddWithValue("@FName", txtFirstName.Text);
      command.Parameters.AddWithValue("@LName", txtLastName.Text);
      command.Parameters.AddWithValue("@Add1", txtAddress1.Text);
      command.Parameters.AddWithValue("@Add2", txtAddress2.Text);
      command.Parameters.AddWithValue("@City", txtCity.Text);
      command.Parameters.AddWithValue("@Zip", txtZip.Text);
      command.Parameters.AddWithValue("@State", comState.Text);
      command.Parameters.AddWithValue("@Country", comCountry.Text);
      command.Parameters.AddWithValue("@Phone", txtPhone.Text);
      command.Parameters.AddWithValue("@Email", txtEmail.Text);
      command.Parameters.AddWithValue("@CardType", comCardType.Text);
      command.Parameters.AddWithValue("@CardN", txtCard.Text);
      command.Parameters.AddWithValue("@EXPM", comboEXPM.Text);
      command.Parameters.AddWithValue("@EXPY", comboEXPY.Text);
      command.Parameters.AddWithValue("@CVV", txtCVV.Text);
      command.Parameters.AddWithValue("@ID", txtID.Text);
      command.CommandText = query;
      command.ExecuteNonQuery();
      RefreshDBConnection();
      connection.Close();
      MessageBox.Show("Profile Edited");
      this.Close();
      }


      and finally, here is a picture of what happens when I edit then save, with some info blocked out, for obvious reasons: Error with lines



      Please let me know if I can give you any more info, thank you! (using c# btw)







      c# ms-access datagridview






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 18:22

























      asked Nov 19 at 5:37









      Caden Buckelew

      237




      237





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53368877%2fwhen-editing-database-then-adding-multiple-lines-in-a-datagridview-the-lines-d%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53368877%2fwhen-editing-database-then-adding-multiple-lines-in-a-datagridview-the-lines-d%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          If I really need a card on my start hand, how many mulligans make sense? [duplicate]

          Alcedinidae

          Can an atomic nucleus contain both particles and antiparticles? [duplicate]