Results 1 to 8 of 8

Thread: Dim dr As New DataRow??? [Resolved!]

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Question Dim dr As New DataRow??? [Resolved!]

    While practicing some datasets today, I noticed that when I was to work with a datarow, I could not do this:

    VB Code:
    1. Dim dr As New DataRow

    The tooltip said this:

    'System.Data.DataRow.Protected Sub New(builder As System.Data.DataRowBuilder)' is not accessible in this context because it is 'Protected'.
    Can someone clarify the meaning of this to me? Also, why can I not declare a new datarow? Apprently, my OO concepts really aren't that good, since I fail to understand why this was done.

    Last edited by mendhak; Jun 21st, 2004 at 06:04 AM.

  2. #2
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724
    You have to create new records via the DataTable instance, I believe it is to ensure that each row has the correct column structure

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    To create a datarow obj do this :
    VB Code:
    1. Dim dr As DataRow=AnyDataset.Tables.Rows.NewDataRow (not sure of the last one)

    Private members inside public classes gives this error after trying to instantiating it .

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Because the DataRow object is part of a hierarchy of objects it does not have a default constructor, which is a constructor with no arguments. The DataRow must have information about the fields and table it is associated with so you would need to create a DataRowBuilder and pass that it if you choose that way. The normal way of creating a new row is from the DataTable object though and its NewRow method which will return a DataRow that is already associated with that table.
    VB Code:
    1. Dim ds As New DataSet
    2.         Dim dr As DataRow = ds.Tables(0).NewRow

    As Pirate mentioned.

  5. #5
    Junior Member
    Join Date
    Jun 2004
    Posts
    29

    Smile Data row not independent

    Data row itself is not an independent .The DataRow and DataColumn objects are primary components of a DataTable.The kind of declaration tried is to declare a new row object which is wrong use.
    The structure is like Data TableàData Column àData row

    The error you are facing is because the base class data row does not allow it. That is why you get that exception.
    Give me a place to stand and I will move the Earth

  6. #6

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    OK, then can someone help me with this bit of code.

    It's very simple. I've already populated my dataset. I am trying to populate fields in my form by using the elements of the datarow. Now, to do this, I obviously ened to set the datarow to a row in the dataset. So, I'm doing this:

    VB Code:
    1. dr = ds.Tables("Product").Rows.Find(CInt(lItem.ID))

    After which I am using it like this:

    VB Code:
    1. txtID.Text = dr("ProductID").ToString
    2.         txtName.Text = dr("ProductName").ToString

    etc.

    But, the dr = ds.Tables(...) line is showing an error:

    Object reference not set to an instance of an object.
    Is something wrong with that line? I can upload the project if you'd like. It's a very simple DB project that uses the Northwind database.

  7. #7

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Btw, lItem.ID is a class I've made, I doubt it's of any relevance here, as it's returning proper values. I've even tried replacing lItem.ID with 0 or 1, but it still won't work.

    I'm uploading the sample here all the same. You can take a look at it.
    Attached Files Attached Files

  8. #8

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    MOTHER ****...

    I hate it when I solve my own problems.

    I misspelt products as product.

    Thanks a lot for your inputs, guys.

    This thread is resolved.

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