|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
A simple insert problemI 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 ----------------------- Posted by a user from .NET 247 (http://www.dotnet247.com/) <Id>FdFbRYaMk0mqARLDPpAkqQ==</Id> 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(); "Dave Cooke via .NET 247" <anonym***@dotnet247.com> wrote in message and I need some help before I loose the rest of my hair.news:OXwGBUPNFHA.2580@TK2MSFTNGP09.phx.gbl... > Hi all, > I know I am going to kick myself but I have run into a bit of a brick wall > vb.net app I'm writing.> All I want to do is insert a new row of data into a access database from a Show quote > System.EventArgs) Handles Button1.Click> 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 Show quote > of an object> '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 Show quote > > 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 > > ----------------------- > Posted by a user from .NET 247 (http://www.dotnet247.com/) > > <Id>FdFbRYaMk0mqARLDPpAkqQ==</Id> 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 myNewRow = New Dataset1.FlightsRowinstead try: 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 |
|||||||||||||||||||||||