Object reference not set to an instance of an object-

  • Thread starter Thread starter Dianna K
  • Start date Start date
D

Dianna K

I am constantly receiving the "Object reference not set to
an instance of an object" Exception when I try to set the
autoincrement to true. I found the Knowledge Base Article
819346 which seems to address the error message, but how
do I know that this is what is causing the problem..? If
this is the route I must take, is the only fix for this is
to upgrade to .Net 2003???

Here is the line of code I am receiving error on:

Dim IdCol As String = "type_id"
Dim ParentTable As String = "PgmTypes"

Ds1.Tables(ParentTable).Columns(IdCol).AutoIncrement = True
'Error occurs here
Ds1.Tables(ParentTable).Columns(IdCol).AutoIncrementSeed
= -1
Ds1.Tables(ParentTable).Columns(IdCol).AutoIncrementStep
= -1
Ds1.Tables(ParentTable).PrimaryKey = New Object()
{Ds1.Tables(ParentTable).Columns(0)}


sqlDataAdp.Fill(Ds1, ParentTable)
uwGrid.DataSource = Ds1
uwGrid.DataMember = ParentTable
'Set the datakeyfield for the row.datakey property
population
uwGrid.Bands(0).DataKeyField = IdCol
uwGrid.DataBind()
 
Dianna,

Have you checked to ensure you are setting DS1 before the
..AutoIncrement=True call? If DS1 is not Null, then perhaps the
Tables(ParentTable) is not returning an object, (because it doesn't exit),
if it is, Perhaps Columns(IdCol) is not returning one!

If you can't see any obvious problem, break up the offending line into most
basic pieces, such as

Dim tc as DataTableCollection
Dim cc as DataColumnsCollection

Set DataTableCollection tc = Ds1.Tables(ParentTable)
Set DataColumnsCollection cc = tc.Columns(IdCol)
cc.AutoIncrement = True

Then you can step through the code, checking the objects Ds1, tc and cc to
see which is being set to nothing or null.
Once you've found the particular object that is nothing- you can concentrate
on figuring out why that is.

Erin.
 
Hi Erin,

I threw in some debug code and it looks like the dataset
is good:

If Ds1 Is Nothing Then
lblError.Text = lblError.Text + "dataset is nothing"
Exit Sub
End If

Ds1.Tables(ParentTable).Columns(IdCol).AutoIncrement = True

This code is for a grid. Originally I created the dataset
via the SQLDataAdapter 'wizard'. The above code worked
fine. The above error started when I changed the code to
create the dataset programatically...? I know the data is
binding (I can see the records when the page first
loads). It incurs the error upon the update.
Dianna
 
Back
Top