Problems with strongly typed dataset not working

S

Siegfried Heintze

The program below clearly populates the data set as seen by dumping the XML.
I'm using strongly typed data sets.

(1) Why does not my loop to dump row by row work? The record count is zero!
This is clearly wrong from the XML that is dumped!
(2) Why does not my attempt to add a new row work? I don't see "sari" in the
xml or the database when I am done!

Thanks
Siegfried



Dim sqlConnection As New System.Data.Odbc.OdbcConnection(cs)
Dim sqlCommand As New System.Data.Odbc.OdbcCommand(sel, sqlConnection)
'Dim ds As New DataSet()
Dim ds As New OdbcSimple_mdb4
Dim da As New System.Data.Odbc.OdbcDataAdapter(sel, sqlConnection)
da.Fill(ds)
Dim dt As DataTable = ds.Tables(0)
' Use the constructor that takes a type and XmlRootAttribute.
Dim ser1 As New
Xml.Serialization.XmlSerializer(GetType([OdbcSimple_mdb4]))
Dim tw1 As TextWriter = System.Console.Out
ser1.Serialize(tw1, ds)
#If noprompt Then
outp.WriteLine("exiting DisconnectedRS.vb")
#Else
outp.Write("Enter any key to continue DisconnectedRS.vb: ")
_getch()
#End If
Dim t As OdbcSimple_mdb4.simpleDataTable
t = ds.simple
outp.WriteLine("row count = " & t.Rows.Count)
Dim r As OdbcSimple_mdb4.simpleRow
For Each r In t
outp.WriteLine(r.sDescription & "," & r.dtCreation)
Next
r = t.NewRow
r.dtCreation = New DateTime(2008, 6, 18)
r.sDescription = "Sari"
ds.simple.AcceptChanges()
Dim ser2 As New
Xml.Serialization.XmlSerializer(GetType([OdbcSimple_mdb4]))
Dim tw2 As TextWriter = System.Console.Out
ser2.Serialize(tw2, ds)
 
K

Kerry Moorman

Siegfried,

Where are you adding the new row to the datatable? I don't see you calling
t.Rows.Add(r), for example.

Why are you calling AcceptChanges?

Kerry Moorman
 
S

Siegfried Heintze

It works! thanks! Should I not be calling accept changes?

How does ADO implement this internally? Does it fabricate SQL INSERT
statements that are later parsed by SQL Server or MSAccess?

thanks,
Siegfried
 
S

Siegfried Heintze

Woops! I was mistaken! When I print out th row count it is still zero and my
for loop does not display any Rows!

Why not?

And when I look in the mdb file after running the program I don't see my new
record.
How do I flush the data set to the database?

I do see it (my new record with "sari" in it) when I dump the XML, however.

Thanks,
Siegfried
 
K

Kerry Moorman

Siegfried,

First, don't call AcceptChanges.

Instead, call the dataadapter's Update method, sneding it your dataset as an
argument.

Of course, this will require that you provide the dataadapter with
appropriate Insert, Update and Delete commands. You can do that manually or
with a commandbuilder object.

Kerry Moorman
 
S

Siegfried Heintze

Thanks!
I added the command builder.
I am calling the update function.
However, it still does not display the records in my for loop and still does
not print the correct count of records and still does not add my new record
to the database.

I tried modifying a field in record zero and again in record one and it
gives me an index bounds error.

What am I doing wrong?
Thanks,
Siegfried

Dim sqlConnection As New System.Data.Odbc.OdbcConnection(cs)
Dim sqlCommand As New System.Data.Odbc.OdbcCommand(sel, sqlConnection)
'Dim ds As New DataSet()
Dim ds As New OdbcSimple_mdb4
Dim da As New System.Data.Odbc.OdbcDataAdapter(sel, sqlConnection)
Dim cb As New OdbcCommandBuilder(da)

da.Fill(ds)
Dim dt As DataTable = ds.Tables(0)
' Use the constructor that takes a type and XmlRootAttribute.
Dim ser1 As New
Xml.Serialization.XmlSerializer(GetType([OdbcSimple_mdb4]))
Dim tw1 As TextWriter = System.Console.Out
ser1.Serialize(tw1, ds)
Dim t As OdbcSimple_mdb4.simpleDataTable
t = ds.simple
outp.WriteLine("row count = " & t.Rows.Count)
Dim r As OdbcSimple_mdb4.simpleRow
For Each r In t
outp.WriteLine(r.sDescription & "," & r.dtCreation)
Next
't.Rows(0)(2) = New DateTime(1960, 1, 18, 6, 55, 23)
r = t.NewRow
r.dtCreation = New DateTime(2008, 6, 18)
r.sDescription = "Sari"
ds.simple.AddsimpleRow(r)
'ds.simple.AcceptChanges()
da.Update(ds)
Dim ser2 As New
Xml.Serialization.XmlSerializer(GetType([OdbcSimple_mdb4]))
Dim tw2 As TextWriter = System.Console.Out
ser2.Serialize(tw2, ds)
dt.Dispose()
sqlConnection.Close()
 
K

Kerry Moorman

Siegfried,

I wonder if the table has any rows.

You might try adding a new row to the database table at the very beginning
of your code block and then see if you get a row count and if your for loop
displays the row information.

Kerry Moorman
 
S

Siegfried Heintze

Kerry:
I did that. I now see the most recently added row. How do access the other
rows? They are clearly there because I can see them in the serialized
dataset.
Thanks,
Siegfried
 
K

Kerry Moorman

Siegfried,

The only thing that I can see is that you serialize the dataset, ds. That is
what you see in the console output.

But you are doing all the work with t As OdbcSimple_mdb4.simpleDataTable,
which you assign ds.simple. Perhaps something is wrong with that declaration
or assignment.

Other than that, I am out of suggestions.

Kerry Moorman
 

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