DataTable.NewRow - Combine DataRow Object and String From Another












0















Is there a way I can essentially create a New Object to insert as a New Row in a Datatable using the existing DataRow plus an additional string?



Essentially I clone a Datatable but then add another 2 columns in to this Datatable and wish to update the cloned datatable with both the data from the original table from which it was cloned, plus additional information in this case who it was Assigned To and on what date.



I tried the following attempting to create a New Object using 'r' the DataRow in result and then additionally added 2 strings, however I receive an error.



Private Shared Sub PopulatedCurrAssigned

_currAssigneddt = Config.MasterTbl.Clone()
_currAssigneddt.Columns.Add("Assigned Date", GetType(DateTime))
_currAssigneddt.Columns.Add("Assigned To", GetType(String))
Dim MasterTbl As DataTable = config.MasterTbl
Dim actiontbl as DataTable = config.RefreshMasterActionTbl()

For Each row In MasterTbl.Rows

Dim result() as DataRow = actiontbl.Select("FreshAppsID = '" & row("FreshAppsID") & "'")

If result.Count() > 0 Then
For each r as DataRow in result
_currAssigneddt.Rows.Add(New Object() {r, "New", "New"})
Next
End If
Next

End Sub


Any suggestions or assistance is greatly appreciated.



Update



Tried the below suggestion from Rango and got the following:



System.ArgumentException
HResult=0x80070057
Message=Unable to cast object of type 'System.Object' to type
'System.IConvertible'.Couldn't store <System.Object> in Ledger Date Column.
Expected type is DateTime.
Source=System.Data

Inner Exception 1:
InvalidCastException: Unable to cast object of type 'System.Object' to type
'System.IConvertible'.


Update 2



My mistake for the error I had removed the declaration out of the variable as a DataRow.



I added this back in and it rectified the problem.










share|improve this question





























    0















    Is there a way I can essentially create a New Object to insert as a New Row in a Datatable using the existing DataRow plus an additional string?



    Essentially I clone a Datatable but then add another 2 columns in to this Datatable and wish to update the cloned datatable with both the data from the original table from which it was cloned, plus additional information in this case who it was Assigned To and on what date.



    I tried the following attempting to create a New Object using 'r' the DataRow in result and then additionally added 2 strings, however I receive an error.



    Private Shared Sub PopulatedCurrAssigned

    _currAssigneddt = Config.MasterTbl.Clone()
    _currAssigneddt.Columns.Add("Assigned Date", GetType(DateTime))
    _currAssigneddt.Columns.Add("Assigned To", GetType(String))
    Dim MasterTbl As DataTable = config.MasterTbl
    Dim actiontbl as DataTable = config.RefreshMasterActionTbl()

    For Each row In MasterTbl.Rows

    Dim result() as DataRow = actiontbl.Select("FreshAppsID = '" & row("FreshAppsID") & "'")

    If result.Count() > 0 Then
    For each r as DataRow in result
    _currAssigneddt.Rows.Add(New Object() {r, "New", "New"})
    Next
    End If
    Next

    End Sub


    Any suggestions or assistance is greatly appreciated.



    Update



    Tried the below suggestion from Rango and got the following:



    System.ArgumentException
    HResult=0x80070057
    Message=Unable to cast object of type 'System.Object' to type
    'System.IConvertible'.Couldn't store <System.Object> in Ledger Date Column.
    Expected type is DateTime.
    Source=System.Data

    Inner Exception 1:
    InvalidCastException: Unable to cast object of type 'System.Object' to type
    'System.IConvertible'.


    Update 2



    My mistake for the error I had removed the declaration out of the variable as a DataRow.



    I added this back in and it rectified the problem.










    share|improve this question



























      0












      0








      0








      Is there a way I can essentially create a New Object to insert as a New Row in a Datatable using the existing DataRow plus an additional string?



      Essentially I clone a Datatable but then add another 2 columns in to this Datatable and wish to update the cloned datatable with both the data from the original table from which it was cloned, plus additional information in this case who it was Assigned To and on what date.



      I tried the following attempting to create a New Object using 'r' the DataRow in result and then additionally added 2 strings, however I receive an error.



      Private Shared Sub PopulatedCurrAssigned

      _currAssigneddt = Config.MasterTbl.Clone()
      _currAssigneddt.Columns.Add("Assigned Date", GetType(DateTime))
      _currAssigneddt.Columns.Add("Assigned To", GetType(String))
      Dim MasterTbl As DataTable = config.MasterTbl
      Dim actiontbl as DataTable = config.RefreshMasterActionTbl()

      For Each row In MasterTbl.Rows

      Dim result() as DataRow = actiontbl.Select("FreshAppsID = '" & row("FreshAppsID") & "'")

      If result.Count() > 0 Then
      For each r as DataRow in result
      _currAssigneddt.Rows.Add(New Object() {r, "New", "New"})
      Next
      End If
      Next

      End Sub


      Any suggestions or assistance is greatly appreciated.



      Update



      Tried the below suggestion from Rango and got the following:



      System.ArgumentException
      HResult=0x80070057
      Message=Unable to cast object of type 'System.Object' to type
      'System.IConvertible'.Couldn't store <System.Object> in Ledger Date Column.
      Expected type is DateTime.
      Source=System.Data

      Inner Exception 1:
      InvalidCastException: Unable to cast object of type 'System.Object' to type
      'System.IConvertible'.


      Update 2



      My mistake for the error I had removed the declaration out of the variable as a DataRow.



      I added this back in and it rectified the problem.










      share|improve this question
















      Is there a way I can essentially create a New Object to insert as a New Row in a Datatable using the existing DataRow plus an additional string?



      Essentially I clone a Datatable but then add another 2 columns in to this Datatable and wish to update the cloned datatable with both the data from the original table from which it was cloned, plus additional information in this case who it was Assigned To and on what date.



      I tried the following attempting to create a New Object using 'r' the DataRow in result and then additionally added 2 strings, however I receive an error.



      Private Shared Sub PopulatedCurrAssigned

      _currAssigneddt = Config.MasterTbl.Clone()
      _currAssigneddt.Columns.Add("Assigned Date", GetType(DateTime))
      _currAssigneddt.Columns.Add("Assigned To", GetType(String))
      Dim MasterTbl As DataTable = config.MasterTbl
      Dim actiontbl as DataTable = config.RefreshMasterActionTbl()

      For Each row In MasterTbl.Rows

      Dim result() as DataRow = actiontbl.Select("FreshAppsID = '" & row("FreshAppsID") & "'")

      If result.Count() > 0 Then
      For each r as DataRow in result
      _currAssigneddt.Rows.Add(New Object() {r, "New", "New"})
      Next
      End If
      Next

      End Sub


      Any suggestions or assistance is greatly appreciated.



      Update



      Tried the below suggestion from Rango and got the following:



      System.ArgumentException
      HResult=0x80070057
      Message=Unable to cast object of type 'System.Object' to type
      'System.IConvertible'.Couldn't store <System.Object> in Ledger Date Column.
      Expected type is DateTime.
      Source=System.Data

      Inner Exception 1:
      InvalidCastException: Unable to cast object of type 'System.Object' to type
      'System.IConvertible'.


      Update 2



      My mistake for the error I had removed the declaration out of the variable as a DataRow.



      I added this back in and it rectified the problem.







      vb.net






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 28 '18 at 13:58







      Lynchie

















      asked Nov 28 '18 at 13:26









      LynchieLynchie

      447825




      447825
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You could use:



          _currAssigneddt.Rows.Add(r.ItemArray.Concat({Date.Now, "Name of Person"}).ToArray())


          Although this is not very efficient, i'd prefer (fixing also the type issue in your example):



          Dim newRow = _currAssigneddt.Rows.Add(r.ItemArray)
          newRow("Assigned Date") = Date.Now
          newRow("Assigned To") = "Name of Person"


          This is much better readable, type safe and doesn't rely on indexes of the (new) columns.






          share|improve this answer


























          • Hi bud got the error I've edited above.

            – Lynchie
            Nov 28 '18 at 13:51













          • @Lynchie: you get this error because of your bad sample data. You add a DateTime column and then you try to assign "New". I have edited my answer

            – Rango
            Nov 28 '18 at 13:54













          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',
          autoActivateHeartbeat: false,
          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%2f53520536%2fdatatable-newrow-combine-datarow-object-and-string-from-another%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You could use:



          _currAssigneddt.Rows.Add(r.ItemArray.Concat({Date.Now, "Name of Person"}).ToArray())


          Although this is not very efficient, i'd prefer (fixing also the type issue in your example):



          Dim newRow = _currAssigneddt.Rows.Add(r.ItemArray)
          newRow("Assigned Date") = Date.Now
          newRow("Assigned To") = "Name of Person"


          This is much better readable, type safe and doesn't rely on indexes of the (new) columns.






          share|improve this answer


























          • Hi bud got the error I've edited above.

            – Lynchie
            Nov 28 '18 at 13:51













          • @Lynchie: you get this error because of your bad sample data. You add a DateTime column and then you try to assign "New". I have edited my answer

            – Rango
            Nov 28 '18 at 13:54


















          1














          You could use:



          _currAssigneddt.Rows.Add(r.ItemArray.Concat({Date.Now, "Name of Person"}).ToArray())


          Although this is not very efficient, i'd prefer (fixing also the type issue in your example):



          Dim newRow = _currAssigneddt.Rows.Add(r.ItemArray)
          newRow("Assigned Date") = Date.Now
          newRow("Assigned To") = "Name of Person"


          This is much better readable, type safe and doesn't rely on indexes of the (new) columns.






          share|improve this answer


























          • Hi bud got the error I've edited above.

            – Lynchie
            Nov 28 '18 at 13:51













          • @Lynchie: you get this error because of your bad sample data. You add a DateTime column and then you try to assign "New". I have edited my answer

            – Rango
            Nov 28 '18 at 13:54
















          1












          1








          1







          You could use:



          _currAssigneddt.Rows.Add(r.ItemArray.Concat({Date.Now, "Name of Person"}).ToArray())


          Although this is not very efficient, i'd prefer (fixing also the type issue in your example):



          Dim newRow = _currAssigneddt.Rows.Add(r.ItemArray)
          newRow("Assigned Date") = Date.Now
          newRow("Assigned To") = "Name of Person"


          This is much better readable, type safe and doesn't rely on indexes of the (new) columns.






          share|improve this answer















          You could use:



          _currAssigneddt.Rows.Add(r.ItemArray.Concat({Date.Now, "Name of Person"}).ToArray())


          Although this is not very efficient, i'd prefer (fixing also the type issue in your example):



          Dim newRow = _currAssigneddt.Rows.Add(r.ItemArray)
          newRow("Assigned Date") = Date.Now
          newRow("Assigned To") = "Name of Person"


          This is much better readable, type safe and doesn't rely on indexes of the (new) columns.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 28 '18 at 13:55

























          answered Nov 28 '18 at 13:32









          RangoRango

          366k46474734




          366k46474734













          • Hi bud got the error I've edited above.

            – Lynchie
            Nov 28 '18 at 13:51













          • @Lynchie: you get this error because of your bad sample data. You add a DateTime column and then you try to assign "New". I have edited my answer

            – Rango
            Nov 28 '18 at 13:54





















          • Hi bud got the error I've edited above.

            – Lynchie
            Nov 28 '18 at 13:51













          • @Lynchie: you get this error because of your bad sample data. You add a DateTime column and then you try to assign "New". I have edited my answer

            – Rango
            Nov 28 '18 at 13:54



















          Hi bud got the error I've edited above.

          – Lynchie
          Nov 28 '18 at 13:51







          Hi bud got the error I've edited above.

          – Lynchie
          Nov 28 '18 at 13:51















          @Lynchie: you get this error because of your bad sample data. You add a DateTime column and then you try to assign "New". I have edited my answer

          – Rango
          Nov 28 '18 at 13:54







          @Lynchie: you get this error because of your bad sample data. You add a DateTime column and then you try to assign "New". I have edited my answer

          – Rango
          Nov 28 '18 at 13:54






















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53520536%2fdatatable-newrow-combine-datarow-object-and-string-from-another%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

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)