I can find it, why can't my code?

G

Guest

I have a problem deleting a record from a table with 3 primary keys, in code.
I get the values of the primary keys from my datagrid displaying the record
to be deleted as follows:
....
findTheseVals(0) = DataGrid4.Item(u, 0) 'First column and unique primary key
(no duplicates allowed)
findTheseVals(1) = DataGrid4.Item(u, 1) 'Second column and primary key
(duplicates ok)
findTheseVals(2) = DataGrid4.Item(u, 2) 'Third column and primary key
(duplicates ok)
....

Then, to delete the record from the table, I use

DataSet11.Tables("Equipment").Rows.Find(findTheseVals).Delete()

but get the following error:

An unhandled exception of type 'System.NullReferenceException' occurred in
App.exe
Additional information: Object reference not set to an instance of an object.

i.e. this record was not found, although I can physically see it in the table.

I checked the populated values of findTheseVals and it matches my primary
key values (and types) in the database table exactly.
The column orders in the dataAdapter, datagrid and database table are
identical.
I checked that the table referenced is exactly the same as the one
referenced by the datagrid etc.

When I manually delete the row from my datagrid, the record is correctly
deleted from the database.

What am I missing here?
 
W

W.G. Ryan - MVP

Paul - I know you've probably already checked this, but it should be
working. Just to be safe, you are sure that the PrimaryKey is defined on
the DataTable object as well as in the database (although it being in the db
is not necessary). I only ask b/c you mention that PrimaryKey in the
database, but you must use the PK of the datatable as well. If not, you get
erratic behavior. The fact that deleting it via the grid works is a
different issue b/c it's not running the same code, so when you delete the
row, it knows the index of the datatable and deletes is accordingly. Double
check the PrimaryKey property of the table (http://shrinkster.com/8rf )
[the datatable locally) just to be sure. If that matches what you expect,
then this behavior is weird indeed - we'll take it from there if that's the
case
 
G

Guest

Thank again!

I didn't realise that is important to set up the primary keys on the .NET
side as well! in all the other cases everything worked as intended. I will
try this and let you know.

PS: The book I have from Rick Dobson as well as the online help is very
sketchy about how to set the primary key for a dataset table which exist in a
db.
Is it correct to use:
dataset.table(0).primarykey = new datacolumn() {dataset.table(0).columns(0)
dataset.table(0).columns(1) dataset.table(0).columns(2)}

Regards

W.G. Ryan - MVP said:
Paul - I know you've probably already checked this, but it should be
working. Just to be safe, you are sure that the PrimaryKey is defined on
the DataTable object as well as in the database (although it being in the db
is not necessary). I only ask b/c you mention that PrimaryKey in the
database, but you must use the PK of the datatable as well. If not, you get
erratic behavior. The fact that deleting it via the grid works is a
different issue b/c it's not running the same code, so when you delete the
row, it knows the index of the datatable and deletes is accordingly. Double
check the PrimaryKey property of the table (http://shrinkster.com/8rf )
[the datatable locally) just to be sure. If that matches what you expect,
then this behavior is weird indeed - we'll take it from there if that's the
case
PaulNaude said:
I have a problem deleting a record from a table with 3 primary keys, in
code.
I get the values of the primary keys from my datagrid displaying the
record
to be deleted as follows:
...
findTheseVals(0) = DataGrid4.Item(u, 0) 'First column and unique primary
key
(no duplicates allowed)
findTheseVals(1) = DataGrid4.Item(u, 1) 'Second column and primary key
(duplicates ok)
findTheseVals(2) = DataGrid4.Item(u, 2) 'Third column and primary key
(duplicates ok)
...

Then, to delete the record from the table, I use

DataSet11.Tables("Equipment").Rows.Find(findTheseVals).Delete()

but get the following error:

An unhandled exception of type 'System.NullReferenceException' occurred in
App.exe
Additional information: Object reference not set to an instance of an
object.

i.e. this record was not found, although I can physically see it in the
table.

I checked the populated values of findTheseVals and it matches my primary
key values (and types) in the database table exactly.
The column orders in the dataAdapter, datagrid and database table are
identical.
I checked that the table referenced is exactly the same as the one
referenced by the datagrid etc.

When I manually delete the row from my datagrid, the record is correctly
deleted from the database.

What am I missing here?
 
W

W.G. Ryan - MVP

You create an Array of datacolumns (even if it's just one column) and then
set it accordingly. that's the right syntax, but here's an example i wrote
a while back that walks you through it
http://www.knowdotnet.com/articles/adopartiii.html
PaulNaude said:
Thank again!

I didn't realise that is important to set up the primary keys on the .NET
side as well! in all the other cases everything worked as intended. I will
try this and let you know.

PS: The book I have from Rick Dobson as well as the online help is very
sketchy about how to set the primary key for a dataset table which exist
in a
db.
Is it correct to use:
dataset.table(0).primarykey = new datacolumn()
{dataset.table(0).columns(0)
dataset.table(0).columns(1) dataset.table(0).columns(2)}

Regards

W.G. Ryan - MVP said:
Paul - I know you've probably already checked this, but it should be
working. Just to be safe, you are sure that the PrimaryKey is defined on
the DataTable object as well as in the database (although it being in the
db
is not necessary). I only ask b/c you mention that PrimaryKey in the
database, but you must use the PK of the datatable as well. If not, you
get
erratic behavior. The fact that deleting it via the grid works is a
different issue b/c it's not running the same code, so when you delete
the
row, it knows the index of the datatable and deletes is accordingly.
Double
check the PrimaryKey property of the table (http://shrinkster.com/8rf )
[the datatable locally) just to be sure. If that matches what you
expect,
then this behavior is weird indeed - we'll take it from there if that's
the
case
PaulNaude said:
I have a problem deleting a record from a table with 3 primary keys, in
code.
I get the values of the primary keys from my datagrid displaying the
record
to be deleted as follows:
...
findTheseVals(0) = DataGrid4.Item(u, 0) 'First column and unique
primary
key
(no duplicates allowed)
findTheseVals(1) = DataGrid4.Item(u, 1) 'Second column and primary key
(duplicates ok)
findTheseVals(2) = DataGrid4.Item(u, 2) 'Third column and primary key
(duplicates ok)
...

Then, to delete the record from the table, I use

DataSet11.Tables("Equipment").Rows.Find(findTheseVals).Delete()

but get the following error:

An unhandled exception of type 'System.NullReferenceException' occurred
in
App.exe
Additional information: Object reference not set to an instance of an
object.

i.e. this record was not found, although I can physically see it in the
table.

I checked the populated values of findTheseVals and it matches my
primary
key values (and types) in the database table exactly.
The column orders in the dataAdapter, datagrid and database table are
identical.
I checked that the table referenced is exactly the same as the one
referenced by the datagrid etc.

When I manually delete the row from my datagrid, the record is
correctly
deleted from the database.

What am I missing here?
 
G

Guest

Fantastic!! Great article!

W.G. Ryan - MVP said:
You create an Array of datacolumns (even if it's just one column) and then
set it accordingly. that's the right syntax, but here's an example i wrote
a while back that walks you through it
http://www.knowdotnet.com/articles/adopartiii.html
PaulNaude said:
Thank again!

I didn't realise that is important to set up the primary keys on the .NET
side as well! in all the other cases everything worked as intended. I will
try this and let you know.

PS: The book I have from Rick Dobson as well as the online help is very
sketchy about how to set the primary key for a dataset table which exist
in a
db.
Is it correct to use:
dataset.table(0).primarykey = new datacolumn()
{dataset.table(0).columns(0)
dataset.table(0).columns(1) dataset.table(0).columns(2)}

Regards

W.G. Ryan - MVP said:
Paul - I know you've probably already checked this, but it should be
working. Just to be safe, you are sure that the PrimaryKey is defined on
the DataTable object as well as in the database (although it being in the
db
is not necessary). I only ask b/c you mention that PrimaryKey in the
database, but you must use the PK of the datatable as well. If not, you
get
erratic behavior. The fact that deleting it via the grid works is a
different issue b/c it's not running the same code, so when you delete
the
row, it knows the index of the datatable and deletes is accordingly.
Double
check the PrimaryKey property of the table (http://shrinkster.com/8rf )
[the datatable locally) just to be sure. If that matches what you
expect,
then this behavior is weird indeed - we'll take it from there if that's
the
case
I have a problem deleting a record from a table with 3 primary keys, in
code.
I get the values of the primary keys from my datagrid displaying the
record
to be deleted as follows:
...
findTheseVals(0) = DataGrid4.Item(u, 0) 'First column and unique
primary
key
(no duplicates allowed)
findTheseVals(1) = DataGrid4.Item(u, 1) 'Second column and primary key
(duplicates ok)
findTheseVals(2) = DataGrid4.Item(u, 2) 'Third column and primary key
(duplicates ok)
...

Then, to delete the record from the table, I use

DataSet11.Tables("Equipment").Rows.Find(findTheseVals).Delete()

but get the following error:

An unhandled exception of type 'System.NullReferenceException' occurred
in
App.exe
Additional information: Object reference not set to an instance of an
object.

i.e. this record was not found, although I can physically see it in the
table.

I checked the populated values of findTheseVals and it matches my
primary
key values (and types) in the database table exactly.
The column orders in the dataAdapter, datagrid and database table are
identical.
I checked that the table referenced is exactly the same as the one
referenced by the datagrid etc.

When I manually delete the row from my datagrid, the record is
correctly
deleted from the database.

What am I missing here?
 
G

Guest

It seems I am doing something else wrong trying to find my record.
I did not realise it but by creating the dataset after dragging the tables
onto my form, the primary key setup from the database is automatically set-up.

I've used a simple code to disply the primary keys of the table in question
(which I thought was not set up) and everything is there as it should be.

W.G. Ryan - MVP said:
You create an Array of datacolumns (even if it's just one column) and then
set it accordingly. that's the right syntax, but here's an example i wrote
a while back that walks you through it
http://www.knowdotnet.com/articles/adopartiii.html
PaulNaude said:
Thank again!

I didn't realise that is important to set up the primary keys on the .NET
side as well! in all the other cases everything worked as intended. I will
try this and let you know.

PS: The book I have from Rick Dobson as well as the online help is very
sketchy about how to set the primary key for a dataset table which exist
in a
db.
Is it correct to use:
dataset.table(0).primarykey = new datacolumn()
{dataset.table(0).columns(0)
dataset.table(0).columns(1) dataset.table(0).columns(2)}

Regards

W.G. Ryan - MVP said:
Paul - I know you've probably already checked this, but it should be
working. Just to be safe, you are sure that the PrimaryKey is defined on
the DataTable object as well as in the database (although it being in the
db
is not necessary). I only ask b/c you mention that PrimaryKey in the
database, but you must use the PK of the datatable as well. If not, you
get
erratic behavior. The fact that deleting it via the grid works is a
different issue b/c it's not running the same code, so when you delete
the
row, it knows the index of the datatable and deletes is accordingly.
Double
check the PrimaryKey property of the table (http://shrinkster.com/8rf )
[the datatable locally) just to be sure. If that matches what you
expect,
then this behavior is weird indeed - we'll take it from there if that's
the
case
I have a problem deleting a record from a table with 3 primary keys, in
code.
I get the values of the primary keys from my datagrid displaying the
record
to be deleted as follows:
...
findTheseVals(0) = DataGrid4.Item(u, 0) 'First column and unique
primary
key
(no duplicates allowed)
findTheseVals(1) = DataGrid4.Item(u, 1) 'Second column and primary key
(duplicates ok)
findTheseVals(2) = DataGrid4.Item(u, 2) 'Third column and primary key
(duplicates ok)
...

Then, to delete the record from the table, I use

DataSet11.Tables("Equipment").Rows.Find(findTheseVals).Delete()

but get the following error:

An unhandled exception of type 'System.NullReferenceException' occurred
in
App.exe
Additional information: Object reference not set to an instance of an
object.

i.e. this record was not found, although I can physically see it in the
table.

I checked the populated values of findTheseVals and it matches my
primary
key values (and types) in the database table exactly.
The column orders in the dataAdapter, datagrid and database table are
identical.
I checked that the table referenced is exactly the same as the one
referenced by the datagrid etc.

When I manually delete the row from my datagrid, the record is
correctly
deleted from the database.

What am I missing here?
 
G

Guest

Thanks for your efforts!

I found a workaround by using

DataSet.Tables("MyTable").DefaultView.Item(i).Delete() where i is the record
index of the defaultview.

This way I simply code my own for loop to find the records I am looking for.

W.G. Ryan - MVP said:
You create an Array of datacolumns (even if it's just one column) and then
set it accordingly. that's the right syntax, but here's an example i wrote
a while back that walks you through it
http://www.knowdotnet.com/articles/adopartiii.html
PaulNaude said:
Thank again!

I didn't realise that is important to set up the primary keys on the .NET
side as well! in all the other cases everything worked as intended. I will
try this and let you know.

PS: The book I have from Rick Dobson as well as the online help is very
sketchy about how to set the primary key for a dataset table which exist
in a
db.
Is it correct to use:
dataset.table(0).primarykey = new datacolumn()
{dataset.table(0).columns(0)
dataset.table(0).columns(1) dataset.table(0).columns(2)}

Regards

W.G. Ryan - MVP said:
Paul - I know you've probably already checked this, but it should be
working. Just to be safe, you are sure that the PrimaryKey is defined on
the DataTable object as well as in the database (although it being in the
db
is not necessary). I only ask b/c you mention that PrimaryKey in the
database, but you must use the PK of the datatable as well. If not, you
get
erratic behavior. The fact that deleting it via the grid works is a
different issue b/c it's not running the same code, so when you delete
the
row, it knows the index of the datatable and deletes is accordingly.
Double
check the PrimaryKey property of the table (http://shrinkster.com/8rf )
[the datatable locally) just to be sure. If that matches what you
expect,
then this behavior is weird indeed - we'll take it from there if that's
the
case
I have a problem deleting a record from a table with 3 primary keys, in
code.
I get the values of the primary keys from my datagrid displaying the
record
to be deleted as follows:
...
findTheseVals(0) = DataGrid4.Item(u, 0) 'First column and unique
primary
key
(no duplicates allowed)
findTheseVals(1) = DataGrid4.Item(u, 1) 'Second column and primary key
(duplicates ok)
findTheseVals(2) = DataGrid4.Item(u, 2) 'Third column and primary key
(duplicates ok)
...

Then, to delete the record from the table, I use

DataSet11.Tables("Equipment").Rows.Find(findTheseVals).Delete()

but get the following error:

An unhandled exception of type 'System.NullReferenceException' occurred
in
App.exe
Additional information: Object reference not set to an instance of an
object.

i.e. this record was not found, although I can physically see it in the
table.

I checked the populated values of findTheseVals and it matches my
primary
key values (and types) in the database table exactly.
The column orders in the dataAdapter, datagrid and database table are
identical.
I checked that the table referenced is exactly the same as the one
referenced by the datagrid etc.

When I manually delete the row from my datagrid, the record is
correctly
deleted from the database.

What am I missing here?
 

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