Results 1 to 6 of 6

Thread: [RESOLVED] Problems adding a Datarow to a DataTable [02/03]

  1. #1

    Thread Starter
    Addicted Member Crushinator's Avatar
    Join Date
    Jan 2006
    Location
    The Dark Side of the Moon, MD
    Posts
    179

    Resolved [RESOLVED] Problems adding a Datarow to a DataTable [02/03]

    Hello All,

    I have a dataTable on one form, which I am trying to add a row to from another form. The first thing I tried was simply creating the datarow, and passing that over to my procedure which adds the row, but that gave me problems because it said the datarow didn't have those columns on it.

    So then I created a duplicate table on my second form, made a .newrow from that, but now when I pass my row it's saying that that row is already part of another table.

    This is the Code I currently have:
    VB Code:
    1. 'Assigns Values to the Datarow
    2.  
    3.         Dim ChecksDataTable As New DataTable
    4.         ChecksDataTable = NewDealFrm.GetTable(ChecksDataTable)
    5.  
    6.         NewDR = ChecksDataTable.NewRow
    7.         NewDR.Item("CheckAmount") = CheckAmountTextBox.Text
    8.         NewDR.Item("CheckNumber") = CheckNumberTextBox.Text
    9.         NewDR.Item("CheckType") = TypeofCheck()
    10.         NewDR.Item("DateEntered") = Now.ToString
    11.         CheckEntered = True
    12.  
    13.         NewDealFrm.GetEnteredChecks(NewDR)

    VB Code:
    1. Public Sub GetEnteredChecks(ByVal NewDataRow As DataRow)
    2.  
    3.  
    4.         Dim CheckAmount As DataColumn = New DataColumn("CheckAmount")
    5.         Dim CheckNumber As DataColumn = New DataColumn("CheckNumber")
    6.         Dim CheckType As DataColumn = New DataColumn("CheckType")
    7.         Dim DateEntered As DataColumn = New DataColumn("DateEntered")
    8.  
    9.         CheckAmount.DataType = System.Type.GetType("System.Double")
    10.         CheckNumber.DataType = System.Type.GetType("System.String")
    11.         CheckType.DataType = System.Type.GetType("System.String")
    12.         DateEntered.DataType = System.Type.GetType("System.String")
    13.  
    14.  
    15.         ChecksTable.Columns.Add(CheckNumber)
    16.         ChecksTable.Columns.Add(CheckAmount)
    17.         ChecksTable.Columns.Add(CheckType)
    18.         ChecksTable.Columns.Add(DateEntered)
    19.  
    20.         ChecksTable.Rows.Add(NewDataRow)
    21.        
    22. End Sub

    What I would like to do is add my items to the datarow on my second form, then pass that datarow to my first form so I can add that datarow to my datatable.

    Any help would really be appreciated!

    -Crush
    Using Framework 2.0, VB.Net 2005.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,467

    Re: Problems adding a Datarow to a DataTable [02/03]

    Method to add row:
    VB Code:
    1. Public Sub GetEnteredChecks(ByVal values As Object())
    2.     Dim dr As DataRow = ChecksTable.NewRow()
    3.  
    4.     Try
    5.         dr.ItemArray = values
    6.     Catch
    7.         MessageBox.Show("Incorrect number or type of values.")
    8.         Return
    9.     End Try
    10.  
    11.     ChecksTable.Rows.Add(dr)
    12. End Sub
    Usage:
    VB Code:
    1. Dim values As Object() = {CheckAmountTextBox.Text, CheckNumberTextBox.Text, TypeofCheck(), Now.ToString}
    2.  
    3. NewDealFrm.GetEnteredChecks(values)
    There are a few things to note here.

    1. Starting the name of a method with "Get" implies that it is a function that returns a value. What you name your methods is up to you but by normal conventions that name is inappropriate.
    2. You must make sure that the objects that you place in the array are the correct type. For instance, If the CheckAmount column is type Double then you shouldn't be assigning the Text property of a TextBox. You should be validating that string and converting it to a Double.
    3. If you are working with dates in a database then work with dates. Don't convert your dates to strings. Use the appropriate date type in the database and assign DateTime objects to that column. If you're going to convert everything to strings then these other types may as well not even exist.
    4. This is a small thing but Now is a VB Runtime property. Personally I would use the Date.Now, which I'd bet money that Now calls anyway.

  3. #3

    Thread Starter
    Addicted Member Crushinator's Avatar
    Join Date
    Jan 2006
    Location
    The Dark Side of the Moon, MD
    Posts
    179

    Re: Problems adding a Datarow to a DataTable [02/03]

    Hey JM Thanks for the reply. I'm gonna try to convert my input into array format and pass that, trhen my a new row from that data as you suggested.

    Thank you for your suggestions!
    For #1, I did not know that.
    For #2, I actually do validate the input by trying a Double.Parse elsewhere in the code.
    For #3, I was doing some research into adding columns and the way it shown was describe the type was: System.Type.GetType(system.type). This seemed a bit long to me, but I couldn't find another way of doing it. But when I tiried System.Type.GetType("System.Integer") or System.Type.GetType("System.Date") I got errors saying I have to define a proper type. So just to see if the code worked I changed it to a string for testing.

    Again, Thanks for the assistance, I'm going to go modify my code trying the array to DR strategy.

    Thanks
    -Crush
    Using Framework 2.0, VB.Net 2005.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,467

    Re: Problems adding a Datarow to a DataTable [02/03]

    Quote Originally Posted by Crushinator
    I actually do validate the input by trying a Double.Parse elsewhere in the code.
    You shouldn't call Double.Parse unless you know that the value can be parsed, otherwise it may throw an exception. You should call Double.TryParse, which returns a Boolean value indicating whether the operation was successful or not and, if so, places the result in a parameter passed by reference. No exceptions thrown under any circumstances. Also, if you have validated the the contents of the TextBox you should still be converting it to a Double before assigning it to a column of type Double. If you're not and it is working then I'd guess that you must have Option Strict Off. Unfortunately that is the default but I strongly recommend that everyone turns it On.

  5. #5

    Thread Starter
    Addicted Member Crushinator's Avatar
    Join Date
    Jan 2006
    Location
    The Dark Side of the Moon, MD
    Posts
    179

    Re: Problems adding a Datarow to a DataTable [02/03]

    Success! I modified the example and it works perfectly,

    Thanks for the help.

    EDIT: I Just read your other post, and I didn't know there was a tryparse, for some reason I thought that was in 2005. Anyways, I'll adjust the validation function to use the tryParse, thanks for the tip!
    Last edited by Crushinator; Apr 13th, 2006 at 11:35 PM.
    Using Framework 2.0, VB.Net 2005.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,467

    Re: [RESOLVED] Problems adding a Datarow to a DataTable [02/03]

    Double.TryParse existed previously but they have added TryParse to all the other numerical types, Boolean and Date as well in 2005. Also, I fed you some bad information in that last post. Option Strict On or not a String will be implicitly converted to a Double when it's assigned to a DataRow field as long as the conversion is valid. Still, I believe that all conversions should be made explicitly regardless. Many would disagree though.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width