While practicing some datasets today, I noticed that when I was to work with a datarow, I could not do this:
VB Code:
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.
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.
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
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:
dr = ds.Tables("Product").Rows.Find(CInt(lItem.ID))
After which I am using it like this:
VB Code:
txtID.Text = dr("ProductID").ToString
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.
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.