[RESOLVED] unable to add a record
Hi
Dont know what I'm doing wrong here (Im following an exercise in a book and adapt it to my own needs) but it keeps coming up with an error:
"Object reference not set to an instance of an object." when I try to Add a record. it stops at the point where I set the DataRow
I think it has something to do with my dataset. But im to "green" to figure out whats going on.
what I do (and works as I want it):
on my form I have a datagrid which is filled with a single column.
so far so good. Now (offcourse) I want to add a record to this datagrid. I do/try this with with the content of a textbox
now Im lost
already spend more than 3 nights on this (didnt wanna give up ) but now it's time for asking,
so if someone can put me on the track...
thanks
Hi
Dont know what I'm doing wrong here (Im following an exercise in a book and adapt it to my own needs) but it keeps coming up with an error:
"Object reference not set to an instance of an object." when I try to Add a record. it stops at the point where I set the DataRow
I think it has something to do with my dataset. But im to "green" to figure out whats going on.
what I do (and works as I want it):
on my form I have a datagrid which is filled with a single column.
VB.NET:
[SIZE=2]
dtDans = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataTable[/SIZE]
[SIZE=2][SIZE=2]dtDans.Clear()
[SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'-- Create the data adapter and fill the data table
[/COLOR][/SIZE][SIZE=2]daDans = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlClient.SqlDataAdapter("SELECT DansID, dans FROM TDanssoort", strCnn)
daDans.Fill(dtDans)
[SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] oexpData [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] SqlClient.SqlException
MsgBox(oexpData.Message)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2]dgDans.DataSource = dtDans
[/SIZE][/SIZE][/SIZE][/SIZE]
so far so good. Now (offcourse) I want to add a record to this datagrid. I do/try this with with the content of a textbox
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] drDans [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataRow
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dsDans [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataSet
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] cbDans [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] SqlClient.SqlCommandBuilder
[/SIZE][SIZE=2]
dsDans.Clear()
drDans = dsDans.Tables("TDanssoort").NewRow
mdrCursistIndiv.BeginEdit()
drDans("Dans") = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtAddDans.Text
mdrCursistIndiv.EndEdit()
[/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2]dsDans.Tables("TDanssoort").Rows.Add(drDans)
daDans.InsertCommand = cbDans.GetInsertCommand
daDans.Update(dsDans, "TDanssoort")
dsDans.Tables("TDanssoort").AcceptChanges()
daDans.InsertCommand.Connection.Close()
[/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'refreshing the grid by reloading it
[/COLOR][/SIZE][SIZE=2]loadOnderdelen()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE]
now Im lost
already spend more than 3 nights on this (didnt wanna give up ) but now it's time for asking,
so if someone can put me on the track...
thanks
Last edited: