DataAdapters, DataSets, DataTables

C

Christopher Weaver

I'm confused about the relationships between these objects. I've written
this:

DataSet dsTaskActivities = new DataSet("TA");
DataTable dtTask = dsTaskActivities.Tables.Add("Tasks");
DataTable dtActivity = dsTaskActivities.Tables.Add("Activity");
OdbcCommand cmd = new OdbcCommand();
cmd.Connection = odbcIB;
cmd.CommandText = "SELECT * FROM \"tblTasks\"";
odbcDA_TaskActivities.SelectCommand = cmd;
odbcDA_TaskActivities.Fill(dsTaskActivities, "Tasks");
cmd.CommandText = "SELECT * FROM \"tblActivity\"";
odbcDA_TaskActivities.SelectCommand = cmd;
odbcDA_TaskActivities.Fill(dsTaskActivities, "Activity");

It compiles fine but crashes on the last line with this message:

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

What's really confusing is why it will run through
odbcDA_TaskActivities.Fill(dsTaskActivities, "Tasks");
without complaint but crash on
odbcDA_TaskActivities.Fill(dsTaskActivities, "Activity");

I've read the Help text on these subjects and patterned this code after one
of the examples. Clearly I'm missing some fundamental information about how
these objects relate to one another. Would anyone like to lend some
elucidation?
 
B

BrianGlacain

Christopher Weaver said:
I'm confused about the relationships between these objects. I've written
this:
What's really confusing is why it will run through
odbcDA_TaskActivities.Fill(dsTaskActivities, "Tasks");
without complaint but crash on
odbcDA_TaskActivities.Fill(dsTaskActivities, "Activity");

Make sure the SQL string that you've passed into the command to fill the
"Activity" table is valid.
BTW, you don't need to use the lines:
DataTable dtTask = dsTaskActivities.Tables.Add("Tasks");
DataTable dtActivity = dsTaskActivities.Tables.Add("Activity");
Using the dataadapter Fill function will create the tables if they don't
exist already.
 
C

Christopher Weaver

Thank you Brian.

I have tried changing the SQL string by simply replacing the table name with
another, and it works just fine. Actually, the only difference between the
portion of the code that succeeds and that that fails is the name of the
table. tblActivity is the actual name of the table; I've copied and pasted
it in. Any other ideas here?

In regard to the Tables.Add lines, I was attempting to specify the names of
the tables. Is there another way of doing so, or is there any need to
bother?
 
B

BrianGlacain

Christopher Weaver said:
Thank you Brian.

I have tried changing the SQL string by simply replacing the table name
with another, and it works just fine. Actually, the only difference
between the portion of the code that succeeds and that that fails is the
name of the table. tblActivity is the actual name of the table; I've
copied and pasted it in. Any other ideas here?

It sounds like it's related to the database end of things. What database are
you using? Make sure you have permission to execute the SQL statements
against the table.
In regard to the Tables.Add lines, I was attempting to specify the names
of the tables. Is there another way of doing so, or is there any need to
bother?

It depends on what you plan on doing. The Fill command of the DataAdapter
creates the tables using the names you've specified. If you need to refer to
the tables directly, you could do something like:
MessageBox.Show(dsTaskActivities.Tables["Tasks"].Rows.Count.ToString())
If you want a DataTable variable you could use:
DataTable dtTask = dsTaskActivities.Tables["Tasks"];
Saying that, the way you had it will also work.
 
C

Christopher Weaver

Thanks Brian.

I'm connecting to InterBase 7.5 as the owner and administrator of the
database. I have all rights granted.

I'm inclined to agree that it's a back end thing. Unfortunately, I think
it's a back end / front end relationship thing because I've had no trouble
selecting and updating the same table in an app written in Delphi.

If you think of anything else, please let me know. I will keep an eye on
this thread for a while! If I figure it out I will post a message for any
other soul struggling with anything similar.




BrianGlacain said:
Christopher Weaver said:
Thank you Brian.

I have tried changing the SQL string by simply replacing the table name
with another, and it works just fine. Actually, the only difference
between the portion of the code that succeeds and that that fails is the
name of the table. tblActivity is the actual name of the table; I've
copied and pasted it in. Any other ideas here?

It sounds like it's related to the database end of things. What database
are you using? Make sure you have permission to execute the SQL statements
against the table.
In regard to the Tables.Add lines, I was attempting to specify the names
of the tables. Is there another way of doing so, or is there any need to
bother?

It depends on what you plan on doing. The Fill command of the DataAdapter
creates the tables using the names you've specified. If you need to refer
to the tables directly, you could do something like:
MessageBox.Show(dsTaskActivities.Tables["Tasks"].Rows.Count.ToString())
If you want a DataTable variable you could use:
DataTable dtTask = dsTaskActivities.Tables["Tasks"];
Saying that, the way you had it will also work.
 
C

Christopher Weaver

Here's the answer, for anyone wondering. Some of the records on the back
end are corrupt in some way, or otherwise unfit for transmission through the
ODBC connection that I'm using. I can view them through a console app that
was written specifically for the back end (InterBase 7.5), but VS, using the
ODBC driver that came with InterBase has a problem with them.

So far I've found two offensive records, and in both cases, when I deleted
the contents of a VARCHAR 255 field, the problem was gone. I couldn't see
anything within those fields that looked suspicious, but something was
amiss.

Thanks for your help.


Christopher Weaver said:
Thanks Brian.

I'm connecting to InterBase 7.5 as the owner and administrator of the
database. I have all rights granted.

I'm inclined to agree that it's a back end thing. Unfortunately, I think
it's a back end / front end relationship thing because I've had no trouble
selecting and updating the same table in an app written in Delphi.

If you think of anything else, please let me know. I will keep an eye on
this thread for a while! If I figure it out I will post a message for any
other soul struggling with anything similar.




BrianGlacain said:
Christopher Weaver said:
Thank you Brian.

I have tried changing the SQL string by simply replacing the table name
with another, and it works just fine. Actually, the only difference
between the portion of the code that succeeds and that that fails is the
name of the table. tblActivity is the actual name of the table; I've
copied and pasted it in. Any other ideas here?

It sounds like it's related to the database end of things. What database
are you using? Make sure you have permission to execute the SQL
statements against the table.
In regard to the Tables.Add lines, I was attempting to specify the names
of the tables. Is there another way of doing so, or is there any need
to bother?

It depends on what you plan on doing. The Fill command of the DataAdapter
creates the tables using the names you've specified. If you need to refer
to the tables directly, you could do something like:
MessageBox.Show(dsTaskActivities.Tables["Tasks"].Rows.Count.ToString())
If you want a DataTable variable you could use:
DataTable dtTask = dsTaskActivities.Tables["Tasks"];
Saying that, the way you had it will also work.
 

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

Similar Threads


Top