Adding row to data table in .net 2.0

D

DH

I have an untyped dataset with a table. I am trying to
programmatically add a row to this table. This was working in VS 2003
/ .net 1.1 I am receiving an error "Object reference not set to an
instance of an object."

Here is the code that worked in VS 2003 / .net 1.1:

row = dtLines.NewRow()


I tried the above code in VS 2005 / .net 2.0, and I also revised to
below, but I am still receiving the above error:

Dim row As DataRow = dsQuotes.Tables("dtLines").NewRow()

Why am I receiving this error?
 
M

Miro

DH

I have been fiddling with adding rows and records
i think u might be looking for this..so if im wrong...i appologize for the
post.

I think you might be missing the fill command on the data table ?

Both codes work below... let me know if they help... and if they do, since i
dont know
the terminology, could ya let me know what you did wrong.
Would help me learn if I run into it as well.

Thanks

Miro


-Below is 2 pieces of code... 1 was given by newsgroup that got me started,
the other
is the "long" way of doing it without sql.
================code
Sub AddInitialRecords()
''''Add a quick Record thru SQL - given by Newsgroup...
''''Dim myConnectionString As String = _
''''"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
''''SystemFileDB & FileDBExtension
''''Dim myConnection As New OleDbConnection(myConnectionString)
''''myConnection.Open()
''''Dim myCommand As New OleDbCommand("INSERT INTO DBVersion
(CurVersion) VALUES (?)", _
'''' myConnection)
''''myCommand.Parameters.Add("Param1", OleDbType.VarChar, 50).Value
= "2.00"
''''myCommand.ExecuteNonQuery()
''''myCommand.Dispose()
''''myConnection.Close()

'Add a record the long way thru normal statements.
Dim cnADONetConnection As New OleDb.OleDbConnection()
Dim myConnectionString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
SystemFileDB & FileDBExtension
cnADONetConnection.ConnectionString = myConnectionString

cnADONetConnection.Open()

Dim daDataAdapter As New OleDb.OleDbDataAdapter()
daDataAdapter = _
New OleDb.OleDbDataAdapter("Select * From DBVersion",
cnADONetConnection)


Dim cbCommandBuilder As OleDb.OleDbCommandBuilder

cbCommandBuilder = New OleDb.OleDbCommandBuilder(daDataAdapter)

Dim dtVersion As New DataTable()
Dim dtRowPosition As Integer = 0 ' i dont think i use this anywhere
but later i will
'Fill with data
daDataAdapter.Fill(dtVersion)

Dim NoOfRecs As Integer = 0
'Go to first row
Dim rwVersion As DataRow '= dtVersion.Rows(0)
NoOfRecs = dtVersion.Rows.Count()

If NoOfRecs = 0 Then
MsgBox("no recs")
rwVersion = dtVersion.NewRow()

rwVersion("CurVersion") = "3.33"

dtVersion.Rows.Add(rwVersion)
daDataAdapter.Update(dtVersion)

Debug.WriteLine("added record - " +
dtVersion.Rows(dtVersion.Rows.Count - 1)("CurVersion").ToString)

Else
MsgBox("there are recs")
rwVersion = dtVersion.Rows(0)
Debug.WriteLine("read record - " + _
rwVersion("CurVersion").GetType.ToString)
End If

'Dim blastring As String = dtVersion.Rows(0)("CurVersion").ToString

Debug.WriteLine("Done debuging")

cnADONetConnection.Close()

End Sub
================end of code
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top