Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically populating a datatable in .NET 2.0

Status
Not open for further replies.

VBRookie

Programmer
May 29, 2001
331
US
Hello gurus,

I'm having a little trouble here doing something that should be relatively simple. I'm trying to add rows to a datatable programmatically. Here are my declarations:

Code:
Dim dTbl As New DataTable
Dim dRow As New DataRow

'... population code follows ...

If I try to run this I get this error:
Code:
BC30390: 'System.Data.DataRow.Protected Sub New(builder As System.Data.DataRowBuilder)' is not accessible in this context because it is 'Protected'.

If I change my decs to this:
Code:
Dim dTbl As New DataTable
Dim dRow As DataRow

'... population code follows ...

I get this error:
Code:
System.NullReferenceException: Object reference not set to an instance of an object.

So my question is how do I populate a datatable if I can't declare a datarow?

Many Thanks,
- VB Rookie
 
Never mind I figured out my own problem ...

I had it declared right the second time but I was not setting the datarow to a value before trying to use it. Here is my corrected code for anyone that might've had this question too:

Code:
Protected Function Populate_Commitment() As DataTable
  Dim dTbl As New DataTable
  Dim dRow As DataRow

  dTbl.Columns.Add("CommitText")
  dTbl.Columns.Add("CommitValue")
  dTbl.AcceptChanges()

  dRow = dTbl.NewRow()
  dRow.Item("CommitText") = "a"
  dRow.Item("CommitValue") = "b"
  dTbl.Rows.Add(dRow)
  dTbl.AcceptChanges()
  Return dTbl
 End Function

- VB Rookie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top