title: How do I re-write Gridview data to an XML file on button click? ASP.Net VB
body: <p>This is a continuation from my previous post, I'm using a Gridview in a submission form along with textboxes controls. Upon clicking the submit button, the information is rendered to an xml file. Each time the form is used, my intention is to have the xml file populate w/ the newly entered data, thus overwritting that entered from previous submissions. The user needs to be able to input multiple product numbers, which is my reason for using an editable Gridview in the submission form. My problem is that when the submit button is clicked, the data entered from the textboxes are copied over to the xml file, but the data from the Gridview is overwitten. Though, I'm able to Edit/Insert/Delete data to the XML file via the Gridview. the gridview data fails to save to the xml file: </p>
  
  <p>The following is my design:</p>
  
  <pre><code>  &lt;div&gt;
      &lt;!--Grdiview --&gt;
      &lt;div&gt;
          &lt;asp:GridView ID="gvProductNumber" runat="server" AutoGenerateColumns="False"
                          onrowdeleting="gvProductNumber_RowDeleting" onrowediting="gvProductNumber_RowEditing"
                          onrowupdating="gvProductNumber_RowUpdating" onrowcommand="gvProductNumber_RowCommand"
                          ShowFooter="True" Width="482px"
                          onrowcancelingedit="gvProductNumber_RowCancelingEdit"&gt;
                          &lt;Columns&gt;
                              &lt;asp:TemplateField HeaderText="Product ID"&gt;
                                  &lt;EditItemTemplate&gt;
                                      &lt;asp:TextBox ID="txtProductID" runat="server" Text='&lt;%# Bind("productID") %&gt;'&gt;&lt;/asp:TextBox&gt;
                                  &lt;/EditItemTemplate&gt;
                                  &lt;FooterTemplate&gt;
                                      &lt;asp:TextBox ID="txtProductIDInsert" runat="server"&gt;&lt;/asp:TextBox&gt;
                                  &lt;/FooterTemplate&gt;
                                  &lt;ItemTemplate&gt;
                                      &lt;asp:Label ID="lblProductID" runat="server" Text='&lt;%# Bind("ProductID") %&gt;'&gt;&lt;/asp:Label&gt;
                                  &lt;/ItemTemplate&gt;
                              &lt;/asp:TemplateField&gt;
                               &lt;asp:TemplateField HeaderText="Product Number"&gt;
                                  &lt;EditItemTemplate&gt;
                                      &lt;asp:TextBox ID="txtProductNumber" runat="server" Text='&lt;%# Bind("productNumber") %&gt;'&gt;&lt;/asp:TextBox&gt;
                                  &lt;/EditItemTemplate&gt;
                                  &lt;FooterTemplate&gt;
                                      &lt;asp:TextBox ID="txtProductNumberInsert" runat="server"&gt;&lt;/asp:TextBox&gt;
                                  &lt;/FooterTemplate&gt;
                                  &lt;ItemTemplate&gt;
                                      &lt;asp:Label ID="lblProductNumber" runat="server" Text='&lt;%# Bind("ProductNumber") %&gt;'&gt;&lt;/asp:Label&gt;
                                  &lt;/ItemTemplate&gt;
                              &lt;/asp:TemplateField&gt;
                              &lt;asp:TemplateField ShowHeader="False"&gt;
                                  &lt;EditItemTemplate&gt;
                                      &lt;asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
                                          CommandName="Update" Text="Update"&gt;&lt;/asp:LinkButton&gt;
                                      &amp;nbsp;&lt;asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
                                          CommandName="Cancel" Text="Cancel"&gt;&lt;/asp:LinkButton&gt;
                                  &lt;/EditItemTemplate&gt;
                                  &lt;FooterTemplate&gt;
                                      &lt;asp:LinkButton ID="btnInsert" runat="server" CommandName="insertXMLData"&gt;Insert&lt;/asp:LinkButton&gt;
                                  &lt;/FooterTemplate&gt;
                                  &lt;ItemTemplate&gt;
                                      &lt;asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
                                          CommandName="Edit" Text="Edit"&gt;&lt;/asp:LinkButton&gt;
                                  &lt;/ItemTemplate&gt;
                                  &lt;ItemStyle Width="120px" /&gt;
                              &lt;/asp:TemplateField&gt;
                              &lt;asp:CommandField ShowDeleteButton="True" /&gt;
                           &lt;/Columns&gt;
                      &lt;/asp:GridView&gt;   
      &lt;/div&gt;
      &lt;!--end gridview --&gt;
  
  
  
             &lt;br /&gt;&lt;br /&gt;
  
             &lt;div&gt; 
              &lt;div&gt;&lt;p&gt;Last Name:&amp;nbsp; &lt;asp:TextBox id="txtLastName" runat="server" /&gt;&lt;/p&gt;&lt;/div&gt;
              &lt;div&gt;&lt;p&gt;First Name:&amp;nbsp; &lt;asp:TextBox id="txtFirstName" runat="server" /&gt;&lt;/p&gt;&lt;/div&gt;
              &lt;div&gt;&lt;p&gt;Street Address:&amp;nbsp; &lt;asp:TextBox id="txtAddress" runat="server" /&gt;&lt;/p&gt;&lt;/div&gt;
              &lt;div&gt;&lt;p&gt;City:&amp;nbsp; &lt;asp:TextBox id="txtCity" runat="server" /&gt;&lt;/p&gt;&lt;/div&gt;
              &lt;div&gt;&lt;p&gt;State:&amp;nbsp; &lt;asp:TextBox id="txtState" runat="server" /&gt;&lt;/p&gt;&lt;/div&gt;
              &lt;div&gt;&lt;p&gt;Zip Code:&amp;nbsp; &lt;asp:TextBox id="txtZipCode" runat="server" /&gt;&lt;/p&gt;&lt;/div&gt;
              &lt;div&gt;&lt;asp:Button id="btnWriteXml" OnClick="Write_XML" Text="Write XML File" runat="server" /&gt;&lt;/div&gt;
            &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>This is my code behind, this is where I'm trying to re-write the data from the Grid to the xml file and at the same time, write the data that is inputted via the text-box controls:</p>
  
  <p>'Write the results to the xml file</p>
  
  <pre><code>   Public Sub Write_XML(ByVal sender As System.Object, ByVal e As System.EventArgs)
          'You have saved xml, now want to add data thorugh text boxes
  
          Dim dsgvPersonalData As New DataSet()
          dsgvPersonalData.ReadXml(Server.MapPath("~/xml/PersonDataVI.xml"))
  
          'Add new Product
          Dim drNewProduct As DataRow = dsgvPersonalData.Tables("product").NewRow()
          Dim productID As String = DirectCast(gvProductNumber.FooterRow.FindControl("txtProductIDInsert"), TextBox).Text
          Dim productNumber As String = DirectCast(gvProductNumber.FooterRow.FindControl("txtProductNumberInsert"), TextBox).Text
          drNewProduct("productID") = productID
          drNewProduct("productNumber") = productNumber
          dsgvPersonalData.Tables("product").Rows.Add(drNewProduct)
  
          'Add new Customer
          Dim drNewCustomer As DataRow = dsgvPersonalData.Tables("customer").NewRow()
          drNewProduct("LastName") = txtLastName.Text
          drNewProduct("FirstName") = txtFirstName.Text
          drNewProduct("Address") = txtAddress.Text
          drNewProduct("City") = txtCity.Text
          drNewProduct("State") = txtState.Text
          drNewProduct("ZipCode") = txtZipCode.Text
          dsgvPersonalData.Tables("customer").Rows.Add(drNewProduct)
  
          'Save the xml
          dsgvPersonalData.WriteXml(Server.MapPath("~/xml/PersonDataVI.xml"))
  
          'databinding is easy here
          gvProductNumber.DataSource = dsgvPersonalData.Tables("product")
          gvProductNumber.DataBind()
      End Sub
  </code></pre>
  
  <p>The xml I'm attempting to generate:</p>
  
  <pre><code>  &lt;PersonalData&gt;
    &lt;products&gt;
      &lt;product&gt;
        &lt;productID&gt;1&lt;/productID&gt;
        &lt;productNumber&gt;123456&lt;/productNumber&gt;
      &lt;/product&gt;
      &lt;product&gt;
        &lt;productID&gt;2&lt;/productID&gt;
        &lt;productNumber&gt;458922&lt;/productNumber&gt;
      &lt;/product&gt;
      &lt;product&gt;
        &lt;productID&gt;4&lt;/productID&gt;
        &lt;productNumber&gt;224578&lt;/productNumber&gt;
      &lt;/product&gt;
      &lt;product&gt;
        &lt;productID&gt;5&lt;/productID&gt;
        &lt;productNumber&gt;789012&lt;/productNumber&gt;
      &lt;/product&gt;
    &lt;/products&gt;
    &lt;customers&gt;
      &lt;customer&gt;
        &lt;LastName&gt;Doe&lt;/LastName&gt;
        &lt;FirstName&gt;Jane&lt;/FirstName&gt;
        &lt;Address&gt;1456 somewhere Dr.&lt;/Address&gt;
        &lt;City&gt;Frisco&lt;/City&gt;
        &lt;State&gt;California&lt;/State&gt;
        &lt;ZipCode&gt;59045&lt;/ZipCode&gt;
      &lt;/customer&gt;
    &lt;/customers&gt;
  &lt;/PersonalData&gt;
  </code></pre>
  
  <p>As mentioned, the xml from the gridiview fails to re-write to the xml file on button-click;
  Please, could I get some direction as to what I'm doing wrong here. I fill that I'm close. Thanks again.</p>