A simple insert problem

  • Thread starter Dave Cooke via .NET 247
  • Start date
D

Dave Cooke via .NET 247

Hi all,
I know I am going to kick myself but I have run into a bit of a brick wall and I need some help before I loose the rest of my hair.

All I want to do is insert a new row of data into a access database from a vb.net app I'm writing.

I have read so many forums today and I think I must be missing something really basic.

I have a database with a table in it called Flights.
The columns in the database are:
CellInfo type=string
Flightdate type=dateTime
FlightTime type=int
Key type=int
recordNum autonumber, primarykey
WhyComment type=string

I have created a OleDbDataAdapter1, and Dataset1 to this database.


With my code all I am doing at this stage is trying to insert the word "Test"
into the column "WhyComment" when I click a button.

Heres my code:
***************************************
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'Fill Dataset
DataSet11.Clear()
OleDbDataAdapter1.Fill(DataSet11)

'Assign variables
Dim myNewRow As DataSet1.FlightsRow
Dim myDataset1 As DataSet1
Dim FlightsTable As DataSet1.FlightsDataTable

'Insert the word test into the colum "WhyComment" in table "Flights
'in the database.
'WhyComment is a string table

myNewRow = myDataset1.Flights.NewRow

'The above row is where it errors out with this error:
'An unhandled exception of type 'System.NullReferenceException' occurred
'in DatabaseInserts.exe
'
'Additional information: Object reference not set to an instance of an object

myNewRow("WhyComment") = "Test"
myDataset1.Tables("WhyComment").Rows.Add(myNewRow)


'update Database
OleDbDataAdapter1.Update(DataSet11)
MessageBox.Show("Database updated!")

End Sub
****************************************

Anyones help would be appreciated.

Cheers Dave
 
W

W.G. Ryan eMVP

Dave, there's no new keyword there so that's to be expected. You need to
use Dim dr as DataRow = SomeDataTable.NewRow - or in this case, the
strongly typed equivalent.

OISDto.Races RacesData = new OISDto.Races();

OISDto.Races.RaceRow RowData = RacesData.Race.NewRaceRow();


--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
Dave Cooke via .NET 247 said:
Hi all,
I know I am going to kick myself but I have run into a bit of a brick wall
and I need some help before I loose the rest of my hair.
All I want to do is insert a new row of data into a access database from a vb.net app I'm writing.

I have read so many forums today and I think I must be missing something really basic.

I have a database with a table in it called Flights.
The columns in the database are:
CellInfo type=string
Flightdate type=dateTime
FlightTime type=int
Key type=int
recordNum autonumber, primarykey
WhyComment type=string

I have created a OleDbDataAdapter1, and Dataset1 to this database.


With my code all I am doing at this stage is trying to insert the word "Test"
into the column "WhyComment" when I click a button.

Heres my code:
***************************************
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
 
G

Guest

Hi Dave,

Your myDataSet1 object is null when you are trying to access the
Flights.NewRow method.

You have to change:
myNewRow = myDataset1.Flights.NewRow
instead try:
myNewRow = New Dataset1.FlightsRow


Or set myDataSet to a new instance before the line of code throwing the
exception. Like this: (I belive this is the syntax, I narmally use C#)

Set myDataSet1 = New DataSet1
myNewRow = myDataset1.Flights.NewRow 'this will work now


Hope it works
 

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