can't add row to ms access table

  • Thread starter Thread starter dennist
  • Start date Start date
D

dennist

I am using ADO.NET Core Reference by David Sceppa to try
to add rows to an access database using code only. In
particular, I am using the material in chapter 9,
Creating Strongly Typed DataSet Objects as my guide.

Unfortunately, so far I'm not getting very far. He
starts off by saying there's a hard way and an easy way
of creating a strongly typed dataset. One is by code and
the second is by using the controls in the datatab. For
many reasons, I want to do it entirely in code.

Below is his introductory code doing it the 'hard way':




Dim strConn, strSQL As String
strConn = "Provider=SQLOLEDB;Data Source=(local)\NetSDK;"
& _
"Initial
Catalog=Northwind;Trusted_Connection=Yes;"
Dim cn As New OleDbConnection(strConn)
strSQL = "SELECT CustomerID, CompanyName, ContactName,
Phone " & _
"FROM Customers"
Dim daCustomers As New OleDbDataAdapter(strSQL, cn)
strSQL = "SELECT OrderID, CustomerID, EmployeeID,
OrderDate " & _
"FROM Orders"
Dim daOrders As New OleDbDataAdapter(strSQL, cn)
Dim ds As New DataSet()
ds.DataSetName = "Chapter9"
cn.Open()
daCustomers.FillSchema(ds, SchemaType.Source, "Customers")
daOrders.FillSchema(ds, SchemaType.Source, "Orders")
cn.Close()
ds.Relations.Add("CustomersOrders", _
ds.Tables("Customers").Columns
("CustomerID"), _
ds.Tables("Orders").Columns
("CustomerID"))
ds.WriteXmlSchema("C:\Chapter9.XSD")

And below is the console command that allegedly turns the
code into a strongly typed dataset. He warns about
paths. I just copied xsd.exe to my project, navigated to
it's path, then ran the command successfully.

C:\>XSD Chapter9.XSD /d /l:VB


Then he says,

Now you can simply add your new class file to your
project, and you can create an instance of your new
strongly typed DataSet class, as shown in the following
code snippet:

Visual Basic .NET
Dim ds As New Chapter9()


And after he shows how to do it the easy way with the
wizard, he offers the code as follows:


Dim ds As New Chapter9()
OleDbDataAdapter1.Fill(ds)
Dim tblCustomers As Chapter9.CustomersDataTable =
ds.Customers
Dim rowCustomer As Chapter9.CustomersRow = tblCustomers(0)



Now I have only one table I want to start with so I pared
down the code as follows:




Private Sub btnBuild_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnBuild.Click

Dim strConn, strSQL As String
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=H:\HasbaraNET\ado.net tests\DateTypes.mdb;"
Dim cn As New OleDbConnection(strConn)
strSQL = "SELECT * FROM DateType ORDER BY
DateType;"
Dim da As New OleDbDataAdapter(strSQL, cn)

Dim ds As New DataSet
ds.DataSetName = "ds1"
cn.Open()
da.FillSchema(ds, SchemaType.Source, "DateType")
cn.Close()
ds.WriteXmlSchema("H:\HasbaraNET\ado.net
tests\CodeUpdate\ds.xsd")

To this point it worked fine. The project was built
successfully and ran; when I pressed the button no
exceptions occured. I ds file was in the folder. I was
unsure whether to use ds or ds1(the name) so I tried them
both with precisely the same results.

Now he adds the following code. Here I got nowhere.
Whatever I tried didn't work. I tried creating a new
dataset many ways. The only statement that worked was
da.Fill(ds)

Whether I tried dim as ds or ds1, nothing appeared after
the dot. So clearly I'm missing something. This is
where I desperately need help to go farther. He speaks
about the 'easy' way as creating a class file. And maybe
the rest of his code is meant for an invoking project.
If so, should I do my first part as a class library
compiled to a dll and then use it in an invoking windows
project?

'Dim ds As New Chapter9
da.Fill(ds)
'Dim tblDateType As ds1..CustomersDataTable =
ds.Customers
'Dim rowCustomer As Chapter9.CustomersRow =
tblCustomers(0)



End Sub

Thank you for your help.

dennist
 
Did you use the following XSD command to generate the class from the .xsd
file, and then add the class in the project.

C:\>XSD Chapter9.XSD /d /l:VB

The following article addresses how to create and use a typed dataset:

315678.KB.EN-US HOW TO: Create and Use a Typed DataSet by Using Visual
Basic .NET
http://support.microsoft.com/default.aspx?scid=KB;EN-US;315678

Sincerely,

Kevin
Microsoft Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Get Secure! - www.microsoft.com/security

--------------------
| Content-Class: urn:content-classes:message
| From: "dennist" <[email protected]>
| Sender: "dennist" <[email protected]>
| Subject: can't add row to ms access table
| Date: Thu, 16 Oct 2003 12:18:40 -0700
| Lines: 135
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcOUGk6waihy25PETLm8SC9XsQCqig==
| Newsgroups: microsoft.public.dotnet.framework.adonet
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.adonet:63796
| NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
|
| I am using ADO.NET Core Reference by David Sceppa to try
| to add rows to an access database using code only. In
| particular, I am using the material in chapter 9,
| Creating Strongly Typed DataSet Objects as my guide.
|
| Unfortunately, so far I'm not getting very far. He
| starts off by saying there's a hard way and an easy way
| of creating a strongly typed dataset. One is by code and
| the second is by using the controls in the datatab. For
| many reasons, I want to do it entirely in code.
|
| Below is his introductory code doing it the 'hard way':
|
|
|
|
| Dim strConn, strSQL As String
| strConn = "Provider=SQLOLEDB;Data Source=(local)\NetSDK;"
| & _
| "Initial
| Catalog=Northwind;Trusted_Connection=Yes;"
| Dim cn As New OleDbConnection(strConn)
| strSQL = "SELECT CustomerID, CompanyName, ContactName,
| Phone " & _
| "FROM Customers"
| Dim daCustomers As New OleDbDataAdapter(strSQL, cn)
| strSQL = "SELECT OrderID, CustomerID, EmployeeID,
| OrderDate " & _
| "FROM Orders"
| Dim daOrders As New OleDbDataAdapter(strSQL, cn)
| Dim ds As New DataSet()
| ds.DataSetName = "Chapter9"
| cn.Open()
| daCustomers.FillSchema(ds, SchemaType.Source, "Customers")
| daOrders.FillSchema(ds, SchemaType.Source, "Orders")
| cn.Close()
| ds.Relations.Add("CustomersOrders", _
| ds.Tables("Customers").Columns
| ("CustomerID"), _
| ds.Tables("Orders").Columns
| ("CustomerID"))
| ds.WriteXmlSchema("C:\Chapter9.XSD")
|
| And below is the console command that allegedly turns the
| code into a strongly typed dataset. He warns about
| paths. I just copied xsd.exe to my project, navigated to
| it's path, then ran the command successfully.
|
| C:\>XSD Chapter9.XSD /d /l:VB
|
|
| Then he says,
|
| Now you can simply add your new class file to your
| project, and you can create an instance of your new
| strongly typed DataSet class, as shown in the following
| code snippet:
|
| Visual Basic .NET
| Dim ds As New Chapter9()
|
|
| And after he shows how to do it the easy way with the
| wizard, he offers the code as follows:
|
|
| Dim ds As New Chapter9()
| OleDbDataAdapter1.Fill(ds)
| Dim tblCustomers As Chapter9.CustomersDataTable =
| ds.Customers
| Dim rowCustomer As Chapter9.CustomersRow = tblCustomers(0)
|
|
|
| Now I have only one table I want to start with so I pared
| down the code as follows:
|
|
|
|
| Private Sub btnBuild_Click(ByVal sender As
| System.Object, ByVal e As System.EventArgs) Handles
| btnBuild.Click
|
| Dim strConn, strSQL As String
| strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data
| Source=H:\HasbaraNET\ado.net tests\DateTypes.mdb;"
| Dim cn As New OleDbConnection(strConn)
| strSQL = "SELECT * FROM DateType ORDER BY
| DateType;"
| Dim da As New OleDbDataAdapter(strSQL, cn)
|
| Dim ds As New DataSet
| ds.DataSetName = "ds1"
| cn.Open()
| da.FillSchema(ds, SchemaType.Source, "DateType")
| cn.Close()
| ds.WriteXmlSchema("H:\HasbaraNET\ado.net
| tests\CodeUpdate\ds.xsd")
|
| To this point it worked fine. The project was built
| successfully and ran; when I pressed the button no
| exceptions occured. I ds file was in the folder. I was
| unsure whether to use ds or ds1(the name) so I tried them
| both with precisely the same results.
|
| Now he adds the following code. Here I got nowhere.
| Whatever I tried didn't work. I tried creating a new
| dataset many ways. The only statement that worked was
| da.Fill(ds)
|
| Whether I tried dim as ds or ds1, nothing appeared after
| the dot. So clearly I'm missing something. This is
| where I desperately need help to go farther. He speaks
| about the 'easy' way as creating a class file. And maybe
| the rest of his code is meant for an invoking project.
| If so, should I do my first part as a class library
| compiled to a dll and then use it in an invoking windows
| project?
|
| 'Dim ds As New Chapter9
| da.Fill(ds)
| 'Dim tblDateType As ds1..CustomersDataTable =
| ds.Customers
| 'Dim rowCustomer As Chapter9.CustomersRow =
| tblCustomers(0)
|
|
|
| End Sub
|
| Thank you for your help.
|
| dennist
|
|
 
Yes, I used that command, modifying it of course for the
name of my dataset and my path. If fact, I made a copy
of xsd.exe and put it in my project folder. In console
mode, I navigated to my folder and ran the command
successfully. The files appeared in my folder and in the
project all files command.

Still didn't work. Please help.

Dennist
 
Hi Dennist,

I tested the sample code you implemented, the ds.vb file is generated from
ds.xsd by using xsd.exe tool. Then I add the ds.vb file into my project and
re-build.

When testing the ds1 object, it works fine.

===============
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim strConn, strSQL As String
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source
=D:\ZhiSun\CASE\testdb\Northwind2000.mdb;"

Dim cn As New System.Data.OleDb.OleDbConnection(strConn)
strSQL = "SELECT * FROM customers ORDER BY customerID;"

Dim da As New System.Data.OleDb.OleDbDataAdapter(strSQL, cn)

Dim ds As New DataSet
ds.DataSetName = "ds1"
cn.Open()
da.FillSchema(ds, SchemaType.Source, "DateType")
cn.Close()
ds.WriteXmlSchema("D:\ZhiSun\temp\ds.xsd")

End Sub
End Class
===================

Sincerely,

Kevin
Microsoft Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Get Secure! - www.microsoft.com/security

--------------------
| Content-Class: urn:content-classes:message
| From: <[email protected]>
| Sender: <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: RE: can't add row to ms access table
| Date: Thu, 16 Oct 2003 19:36:49 -0700
| Lines: 35
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcOUV4Q7SKxIyRzmSzms/ZHdwwraQw==
| Newsgroups: microsoft.public.dotnet.framework.adonet
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.adonet:63842
| NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
|
| Yes, I used that command, modifying it of course for the
| name of my dataset and my path. If fact, I made a copy
| of xsd.exe and put it in my project folder. In console
| mode, I navigated to my folder and ran the command
| successfully. The files appeared in my folder and in the
| project all files command.
|
| Still didn't work. Please help.
|
| Dennist
| >-----Original Message-----
| >Did you use the following XSD command to generate the
| class from the .xsd
| >file, and then add the class in the project.
| >
| >C:\>XSD Chapter9.XSD /d /l:VB
| >
| >The following article addresses how to create and use a
| typed dataset:
| >
| >315678.KB.EN-US HOW TO: Create and Use a Typed DataSet
| by Using Visual
| >Basic .NET
| >http://support.microsoft.com/default.aspx?scid=KB;EN-
| US;315678
| >
| >Sincerely,
| >
| >Kevin
| >Microsoft Support
| >
| >This posting is provided "AS IS" with no warranties, and
| confers no rights.
| >Get Secure! - www.microsoft.com/security
| >
|
 
My CodeUpdate worked to this point without adding the
ds.vb file to my application. I added this and ds.xsd
(just because I see the xsd file in apps when the wizards
are used. I notice the xsd file is on the dataset, not
the name of the data set, so I added ds.xsd and not
ds1.xsd.

It's the statements that come after that I was hoping to
make progress on, but I am hopelessly confused.

Let me repeat the few following statements as ado.net
core reference had them:

'Dim ds As New Chapter9
da.Fill(ds)
'Dim tblDateType As ds1..CustomersDataTable =
ds.Customers
'Dim rowCustomer As Chapter9.CustomersRow =
tblCustomers(0)

These statements lead directly to the goal for which I
thirst. I thirst for it - adding a row to my access
database, all in code - like I thirsted for water when as
a teenager I went hiking in Death Valley will two other
foolish sophomores. Obviously we survived.

Three of the four statements are commented out for
obvious reasons. I put them in my code to change them
one by one to be appropriate to my database connection.

the da.Fill(ds) worked, by the way.

I tried any number of things once adding the ds.vb and
ds.xsd files to my project, and got a tiny bit farther.
But not far enough. I'm asking for more help.

I can't change Dim ds As New Chapter9
to Dim ds as New ds1
I get the error 'local variable ds is already declared in
current block.

But Dim dsA as New ds1 works fine.
da.Fill(dsA) works fine as well, if I keep the last two
statements commented out.

Now I tried to put them into action.
But as soon as I type
Dim tblDateType As dsA... nothing shows up after the .
dot.
If instead I continue with ds1, I get farther..
Dim tblDateType As ds1.DateTypeDataTable The members
show up after the dot. However I am totally confused,
don't know what I'm doing or what these entities
represent anymore. In any case I can't get past the =
sign following. So I won't go into anymore detail.

Can you get these statements working? Can you tell me
where I've gone wrong?

Thanks,

Dennist
 
I thought I had tracked down the proper code pretty well,
coming up with the following:

da.Fill(ds, "DateType")
Dim tblDateType As ds1.DateTypeDataTable
Dim rowDateType As ds1.DateTypeRow
rowDateType = tblDateType.NewDateTypeRow
rowDateType.DateType = "xxx"
rowDateType.CreateDate = Now
rowDateType.ChangeDate = Now
rowDateType.Active = True
tblDateType.AddDateTypeRow(rowDateType)

Unfortunately when I tried to send the message the
newsgroup was down again.

It seemed to be pretty close to working, except this line
produced the following exception

rowDateType = tblDateType.NewDateTypeRow

An unhandled exception of
type 'System.NullReferenceException' occurred in
CodeUpdate.exe

Additional information: Object reference not set to an
instance of an object.

Another unresolved problem in my mind is the ID, which is
an autoincrementing field, so I didn't put it in. Should
I have?

Sorry for emailing you but for who knows how many days
the newsgroup will be down again? And I'm clearly on the
verge of solving my problem and zooming forward.

Thanks millions.

Dennis
 
Hi Dennis,

I change the code and test it on my side, it works fine on my side. Note to
refer tblDateType object to a table in the dataset.

==============
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
' da.Fill(ds, "DateType")
Dim strConn, strSQL As String
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source
=D:\Northwind2000.mdb;"

Dim cn As New System.Data.OleDb.OleDbConnection(strConn)
strSQL = "SELECT * FROM customers ORDER BY customerID;"

Dim da As New System.Data.OleDb.OleDbDataAdapter(strSQL, cn)

Dim ds As New ds1
cn.Open()

da.Fill(ds, ds.Tables(0).TableName)

Dim tblDateType As ds1.DateTypeDataTable = ds.Tables(0)
'tblDateType.NewDateTypeRow()
Dim rowDateType As ds1.DateTypeRow
rowDateType = tblDateType.NewDateTypeRow()
rowDateType.Country = "xxx"
rowDateType.CustomerID = "AAA" ' 'this is primary key
tblDateType.AddDateTypeRow(rowDateType)
'...

End Sub
================

Working with a Typed DataSet

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconworkingwithtypeddataset.asp:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconworkingwithtypeddataset.asp



Sincerely,

Kevin
Microsoft Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Get Secure! - www.microsoft.com/security

--------------------
| Content-Class: urn:content-classes:message
| From: <[email protected]>
| Sender: <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| Subject: RE: can't add row to ms access table
| Date: Fri, 17 Oct 2003 22:12:42 -0700
| Lines: 40
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Thread-Index: AcOVNnUybLb78BJaTJmNFHAzmDyamw==
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Newsgroups: microsoft.public.dotnet.framework.adonet
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.adonet:63939
| NNTP-Posting-Host: TK2MSFTNGXA13 10.40.1.165
| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
|
| I thought I had tracked down the proper code pretty well,
| coming up with the following:
|
| da.Fill(ds, "DateType")
| Dim tblDateType As ds1.DateTypeDataTable
| Dim rowDateType As ds1.DateTypeRow
| rowDateType = tblDateType.NewDateTypeRow
| rowDateType.DateType = "xxx"
| rowDateType.CreateDate = Now
| rowDateType.ChangeDate = Now
| rowDateType.Active = True
| tblDateType.AddDateTypeRow(rowDateType)
|
| Unfortunately when I tried to send the message the
| newsgroup was down again.
|
| It seemed to be pretty close to working, except this line
| produced the following exception
|
| rowDateType = tblDateType.NewDateTypeRow
|
| An unhandled exception of
| type 'System.NullReferenceException' occurred in
| CodeUpdate.exe
|
| Additional information: Object reference not set to an
| instance of an object.
|
| Another unresolved problem in my mind is the ID, which is
| an autoincrementing field, so I didn't put it in. Should
| I have?
|
| Sorry for emailing you but for who knows how many days
| the newsgroup will be down again? And I'm clearly on the
| verge of solving my problem and zooming forward.
|
| Thanks millions.
|
| Dennis
|
|
 
Thanks, I'm going to try it; even if it works, there will
be a couple more questions on why you did certain
things. A few of the statements I don't understand why
they work, why they're needed. But first let's see if it
works when I adapt your code to my problem.

Thanks again.

Dennist
 
Kevin,

I got farther, but not quite there. The problem may have
to to with my autoincrementing ID, which your example
doesn't have.

Here was your revision:

Private Sub Button3_Click(ByVal sender As System.Object,
ByVal e As
System.EventArgs) Handles Button3.Click
' da.Fill(ds, "DateType")
Dim strConn, strSQL As String
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source
=D:\Northwind2000.mdb;"

Dim cn As New System.Data.OleDb.OleDbConnection
(strConn)
strSQL = "SELECT * FROM customers ORDER BY
customerID;"

Dim da As New System.Data.OleDb.OleDbDataAdapter
(strSQL, cn)

Dim ds As New ds1
cn.Open()

da.Fill(ds, ds.Tables(0).TableName)

Dim tblDateType As ds1.DateTypeDataTable =
ds.Tables(0)
'tblDateType.NewDateTypeRow()
Dim rowDateType As ds1.DateTypeRow
rowDateType = tblDateType.NewDateTypeRow()
rowDateType.Country = "xxx"
rowDateType.CustomerID = "AAA" ' 'this is
primary key
tblDateType.AddDateTypeRow(rowDateType)
'...

End Sub
================
And this was my adaptation:

If I dim ds as new ds1 I get the error message 'local
variable ds already declared in current block. Why did it
work for you? I want to understand.

Therefore I tried

dim dsA as new ds1
that worked.

Why did you use the statement,
da.Fill(ds, ds.Tables(0).TableName)
instead of da.fill(ds,"Customers")

In any case, here was my code:

Dim ds As New DataSet
ds.DataSetName = "ds1"
cn.Open()
da.FillSchema(ds, SchemaType.Source, "DateType")
'cn.Close()
ds.WriteXmlSchema("H:\HasbaraNET\ado.net
tests\CodeUpdate\ds.xsd")

Dim dsA As New ds1

da.Fill(dsA, dsA.Tables(0).TableName)
MsgBox(dsA.Tables(0).TableName.ToString)
Dim tblDateType As ds1.DateTypeDataTable =
dsA.Tables(0)
Dim rowDateType As ds1.DateTypeRow
rowDateType = tblDateType.NewDateTypeRow
rowDateType.DateType = "xxx"
rowDateType.CreateDate = Now
rowDateType.ChangeDate = Now
rowDateType.Active = True
tblDateType.AddDateTypeRow(rowDateType)

When I ran it, it ran without error. However, when I
opened the database there was no new row.

I wasn't entirely surprised, because I didn't have a
value for ID. But what should that statement look like?
It's an autoincrementing ID, so the statement

rowDateType.ID = ???
I can't select it. How to I put in a new row with
autoincrementing keyindexes. Most of my tables are like
that.

I'm almost there, but not quite. I'll be there when the
program runs AND I open the database and the new row is
there.

Thanks for your help.

dennist
 
I am performing research on this, and will get back to you.

Sincerely,

Kevin
Microsoft Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Get Secure! - www.microsoft.com/security

--------------------
| Content-Class: urn:content-classes:message
| From: <[email protected]>
| Sender: <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| Subject: RE: can't add row to ms access table
| Date: Mon, 20 Oct 2003 05:32:07 -0700
| Lines: 166
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcOXBi0a1lKbFpYrQke29kS7zEX1Cw==
| Newsgroups: microsoft.public.dotnet.framework.adonet
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.adonet:64053
| NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
|
| Kevin,
|
| I got farther, but not quite there. The problem may have
| to to with my autoincrementing ID, which your example
| doesn't have.
|
| Here was your revision:
|
| Private Sub Button3_Click(ByVal sender As System.Object,
| ByVal e As
| System.EventArgs) Handles Button3.Click
| ' da.Fill(ds, "DateType")
| Dim strConn, strSQL As String
| strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data
| Source
| =D:\Northwind2000.mdb;"
|
| Dim cn As New System.Data.OleDb.OleDbConnection
| (strConn)
| strSQL = "SELECT * FROM customers ORDER BY
| customerID;"
|
| Dim da As New System.Data.OleDb.OleDbDataAdapter
| (strSQL, cn)
|
| Dim ds As New ds1
| cn.Open()
|
| da.Fill(ds, ds.Tables(0).TableName)
|
| Dim tblDateType As ds1.DateTypeDataTable =
| ds.Tables(0)
| 'tblDateType.NewDateTypeRow()
| Dim rowDateType As ds1.DateTypeRow
| rowDateType = tblDateType.NewDateTypeRow()
| rowDateType.Country = "xxx"
| rowDateType.CustomerID = "AAA" ' 'this is
| primary key
| tblDateType.AddDateTypeRow(rowDateType)
| '...
|
| End Sub
| ================
| And this was my adaptation:
|
| If I dim ds as new ds1 I get the error message 'local
| variable ds already declared in current block. Why did it
| work for you? I want to understand.
|
| Therefore I tried
|
| dim dsA as new ds1
| that worked.
|
| Why did you use the statement,
| da.Fill(ds, ds.Tables(0).TableName)
| instead of da.fill(ds,"Customers")
|
| In any case, here was my code:
|
| Dim ds As New DataSet
| ds.DataSetName = "ds1"
| cn.Open()
| da.FillSchema(ds, SchemaType.Source, "DateType")
| 'cn.Close()
| ds.WriteXmlSchema("H:\HasbaraNET\ado.net
| tests\CodeUpdate\ds.xsd")
|
| Dim dsA As New ds1
|
| da.Fill(dsA, dsA.Tables(0).TableName)
| MsgBox(dsA.Tables(0).TableName.ToString)
| Dim tblDateType As ds1.DateTypeDataTable =
| dsA.Tables(0)
| Dim rowDateType As ds1.DateTypeRow
| rowDateType = tblDateType.NewDateTypeRow
| rowDateType.DateType = "xxx"
| rowDateType.CreateDate = Now
| rowDateType.ChangeDate = Now
| rowDateType.Active = True
| tblDateType.AddDateTypeRow(rowDateType)
|
| When I ran it, it ran without error. However, when I
| opened the database there was no new row.
|
| I wasn't entirely surprised, because I didn't have a
| value for ID. But what should that statement look like?
| It's an autoincrementing ID, so the statement
|
| rowDateType.ID = ???
| I can't select it. How to I put in a new row with
| autoincrementing keyindexes. Most of my tables are like
| that.
|
| I'm almost there, but not quite. I'll be there when the
| program runs AND I open the database and the new row is
| there.
|
| Thanks for your help.
|
| dennist
|
| >-----Original Message-----
| >Hi Dennis,
| >
| >I change the code and test it on my side, it works fine
| on my side. Note to
| >refer tblDateType object to a table in the dataset.
| >
| >==============
| > Private Sub Button3_Click(ByVal sender As
| System.Object, ByVal e As
| >System.EventArgs) Handles Button3.Click
| > ' da.Fill(ds, "DateType")
| > Dim strConn, strSQL As String
| > strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data
| Source
| >=D:\Northwind2000.mdb;"
| >
| > Dim cn As New System.Data.OleDb.OleDbConnection
| (strConn)
| > strSQL = "SELECT * FROM customers ORDER BY
| customerID;"
| >
| > Dim da As New System.Data.OleDb.OleDbDataAdapter
| (strSQL, cn)
| >
| > Dim ds As New ds1
| > cn.Open()
| >
| > da.Fill(ds, ds.Tables(0).TableName)
| >
| > Dim tblDateType As ds1.DateTypeDataTable =
| ds.Tables(0)
| > 'tblDateType.NewDateTypeRow()
| > Dim rowDateType As ds1.DateTypeRow
| > rowDateType = tblDateType.NewDateTypeRow()
| > rowDateType.Country = "xxx"
| > rowDateType.CustomerID = "AAA" ' 'this is
| primary key
| > tblDateType.AddDateTypeRow(rowDateType)
| > '...
| >
| > End Sub
| >================
| >
| >Working with a Typed DataSet
| >
| >http://msdn.microsoft.com/library/default.asp?
| url=/library/en-us/cpguide/htm
| >l/cpconworkingwithtypeddataset.asp:
| >http://msdn.microsoft.com/library/default.asp?
| url=/library/en-us/cpguide/htm
| >l/cpconworkingwithtypeddataset.asp
| >
| >
| >
| >Sincerely,
| >
| >Kevin
| >Microsoft Support
| >
| >This posting is provided "AS IS" with no warranties, and
| confers no rights.
| >Get Secure! - www.microsoft.com/security
|
|
 
I was helped trememdously by people in my thread. From
ado.net core reference and programming microsoft visual
basic .net for microsoft access databases, I figured the
reason I couldn't update was twofold.

As a prelude, here is my entire code. I create a
database with one simple table in it. It's called
datetypes, with 9 entries, and 5 columns. The five
columns are:

ID autoincrementing integer
DateType String
CreateDate = Date, default Now
ChangeDate = Date, default Now
Active = Boolean, default True

Now the code:

Imports System.Data
Imports System.Data.OleDb


Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form
Designer.
InitializeComponent()

'Add any initialization after the
InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component
list.
Protected Overloads Overrides Sub Dispose(ByVal
disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the
Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnBuild As
System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private
Sub InitializeComponent()
Me.btnBuild = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'btnBuild
'
Me.btnBuild.Location = New System.Drawing.Point
(16, 24)
Me.btnBuild.Name = "btnBuild"
Me.btnBuild.Size = New System.Drawing.Size(72, 24)
Me.btnBuild.TabIndex = 0
Me.btnBuild.Text = "Build"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5,
13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.btnBuild)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub btnBuild_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnBuild.Click

Dim strConn, strSQL As String
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=H:\HasbaraNET\ado.net tests\DateTypes.mdb;"
Dim cn As New OleDbConnection(strConn)
strSQL = "SELECT * FROM DateType ORDER BY
DateType;"
Dim da As New OleDbDataAdapter(strSQL, cn)

Dim ds As New DataSet
ds.DataSetName = "ds1"
cn.Open()
da.FillSchema(ds, SchemaType.Source, "DateType")
'cn.Close()
ds.WriteXmlSchema("H:\HasbaraNET\ado.net
tests\CodeUpdate\ds.xsd")

Dim dsA As New ds1

da.Fill(dsA, dsA.Tables(0).TableName)
Dim tblDateType As ds1.DateTypeDataTable =
dsA.Tables(0)
Dim rowDateType As ds1.DateTypeRow
rowDateType = tblDateType.NewDateTypeRow
rowDateType.DateType = "xxx"
rowDateType.CreateDate = Now
rowDateType.ChangeDate = Now
rowDateType.Active = True
rowDateType.ID = Integer.MaxValue
tblDateType.AddDateTypeRow(rowDateType)

Try
da.InsertCommand.Parameters.Add("@ID",
OleDb.OleDbType.Integer, "ID")
Catch er As Exception
MessageBox.Show("Type = " &
er.GetType.ToString & vbCr & "Message = " & er.Message)
End Try

da.InsertCommand.Parameters.Add("@DateType",
OleDb.OleDbType.VarChar, 255, "DateType")
da.InsertCommand.Parameters.Add("@CreateDate",
OleDb.OleDbType.Date, "CreateDate")
da.InsertCommand.Parameters.Add("@ChangeDate",
OleDb.OleDbType.Date, "ChangeDate")
da.InsertCommand.Parameters.Add("@Active",
OleDb.OleDbType.Boolean, "Active")


Try
da.Update(dsA, "DateType")
Catch er As System.Exception
MessageBox.Show("Type = " &
er.GetType.ToString & vbCr & "Message = " & er.Message)
Finally
cn.Close()
End Try

End Sub

End Class

At the end of the last discussion the last line of code
was:

tblDateType.AddDateTypeRow(rowDateType)

Nobody by that time could tell me why my access database
wasn't actually updated. By looking at the two books, I
figured what I was missing was the dataadapter update
statement. From the error that ensued I learned I needed
to create the insert command. Then things got
complicated because of the parameter collection. The
code you see is only one of the variations, but the one
that I think is the best.

At least most of the people who read this newsgroup can
add a new row to a database. Certainly all the Microsoft
people who monitor the newsgroup for we MSDN subscribers
know how. So I can't understand why I can't get an
answer. I'll keep on trying for a while, then resort to
using one of my 4 direct consultations as a universal
subscriber. I would prefer not to resort to that.



dennist
-----Original Message-----
I am performing research on this, and will get back to you.

Sincerely,

Kevin
Microsoft Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Get Secure! - www.microsoft.com/security

--------------------
| Content-Class: urn:content-classes:message
| From: <[email protected]>
| Sender: <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| Subject: RE: can't add row to ms access table
| Date: Mon, 20 Oct 2003 05:32:07 -0700
| Lines: 166
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcOXBi0a1lKbFpYrQke29kS7zEX1Cw==
| Newsgroups: microsoft.public.dotnet.framework.adonet
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.adonet:64053
| NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
|
| Kevin,
|
| I got farther, but not quite there. The problem may have
| to to with my autoincrementing ID, which your example
| doesn't have.
|
| Here was your revision:
|
| Private Sub Button3_Click(ByVal sender As System.Object,
| ByVal e As
| System.EventArgs) Handles Button3.Click
| ' da.Fill(ds, "DateType")
| Dim strConn, strSQL As String
| strConn
= "Provider=Microsoft.Jet.OLEDB.4.0;Data
| Source
| =D:\Northwind2000.mdb;"
|
| Dim cn As New System.Data.OleDb.OleDbConnection
| (strConn)
| strSQL = "SELECT * FROM customers ORDER BY
| customerID;"
|
| Dim da As New System.Data.OleDb.OleDbDataAdapter
| (strSQL, cn)
|
| Dim ds As New ds1
| cn.Open()
|
| da.Fill(ds, ds.Tables(0).TableName)
|
| Dim tblDateType As ds1.DateTypeDataTable =
| ds.Tables(0)
| 'tblDateType.NewDateTypeRow()
| Dim rowDateType As ds1.DateTypeRow
| rowDateType = tblDateType.NewDateTypeRow()
| rowDateType.Country = "xxx"
| rowDateType.CustomerID = "AAA" ' 'this is
| primary key
| tblDateType.AddDateTypeRow(rowDateType)
| '...
|
| End Sub
| ================
| And this was my adaptation:
|
| If I dim ds as new ds1 I get the error message 'local
| variable ds already declared in current block. Why did it
| work for you? I want to understand.
|
| Therefore I tried
|
| dim dsA as new ds1
| that worked.
|
| Why did you use the statement,
| da.Fill(ds, ds.Tables(0).TableName)
| instead of da.fill(ds,"Customers")
|
| In any case, here was my code:
|
| Dim ds As New DataSet
| ds.DataSetName = "ds1"
| cn.Open()
| da.FillSchema(ds, SchemaType.Source, "DateType")
| 'cn.Close()
| ds.WriteXmlSchema("H:\HasbaraNET\ado.net
| tests\CodeUpdate\ds.xsd")
|
| Dim dsA As New ds1
|
| da.Fill(dsA, dsA.Tables(0).TableName)
| MsgBox(dsA.Tables(0).TableName.ToString)
| Dim tblDateType As ds1.DateTypeDataTable =
| dsA.Tables(0)
| Dim rowDateType As ds1.DateTypeRow
| rowDateType = tblDateType.NewDateTypeRow
| rowDateType.DateType = "xxx"
| rowDateType.CreateDate = Now
| rowDateType.ChangeDate = Now
| rowDateType.Active = True
| tblDateType.AddDateTypeRow(rowDateType)
|
| When I ran it, it ran without error. However, when I
| opened the database there was no new row.
|
| I wasn't entirely surprised, because I didn't have a
| value for ID. But what should that statement look like?
| It's an autoincrementing ID, so the statement
|
| rowDateType.ID = ???
| I can't select it. How to I put in a new row with
| autoincrementing keyindexes. Most of my tables are like
| that.
|
| I'm almost there, but not quite. I'll be there when the
| program runs AND I open the database and the new row is
| there.
|
| Thanks for your help.
|
| dennist
|
| >-----Original Message-----
| >Hi Dennis,
| >
| >I change the code and test it on my side, it works fine
| on my side. Note to
| >refer tblDateType object to a table in the dataset.
| >
| >==============
| > Private Sub Button3_Click(ByVal sender As
| System.Object, ByVal e As
| >System.EventArgs) Handles Button3.Click
| > ' da.Fill(ds, "DateType")
| > Dim strConn, strSQL As String
| > strConn
= "Provider=Microsoft.Jet.OLEDB.4.0;Data
 
I was helped trememdously by people in my thread. From
ado.net core reference and programming microsoft visual
basic .net for microsoft access databases, I figured the
reason I couldn't update was twofold.

As a prelude, here is my entire code. I create a
database with one simple table in it. It's called
datetypes, with 9 entries, and 5 columns. The five
columns are:

ID autoincrementing integer
DateType String
CreateDate = Date, default Now
ChangeDate = Date, default Now
Active = Boolean, default True

Now the code:

Imports System.Data
Imports System.Data.OleDb


Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form
Designer.
InitializeComponent()

'Add any initialization after the
InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component
list.
Protected Overloads Overrides Sub Dispose(ByVal
disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the
Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnBuild As
System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private
Sub InitializeComponent()
Me.btnBuild = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'btnBuild
'
Me.btnBuild.Location = New System.Drawing.Point
(16, 24)
Me.btnBuild.Name = "btnBuild"
Me.btnBuild.Size = New System.Drawing.Size(72, 24)
Me.btnBuild.TabIndex = 0
Me.btnBuild.Text = "Build"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5,
13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.btnBuild)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub btnBuild_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnBuild.Click

Dim strConn, strSQL As String
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=H:\HasbaraNET\ado.net tests\DateTypes.mdb;"
Dim cn As New OleDbConnection(strConn)
strSQL = "SELECT * FROM DateType ORDER BY
DateType;"
Dim da As New OleDbDataAdapter(strSQL, cn)

Dim ds As New DataSet
ds.DataSetName = "ds1"
cn.Open()
da.FillSchema(ds, SchemaType.Source, "DateType")
'cn.Close()
ds.WriteXmlSchema("H:\HasbaraNET\ado.net
tests\CodeUpdate\ds.xsd")

Dim dsA As New ds1

da.Fill(dsA, dsA.Tables(0).TableName)
Dim tblDateType As ds1.DateTypeDataTable =
dsA.Tables(0)
Dim rowDateType As ds1.DateTypeRow
rowDateType = tblDateType.NewDateTypeRow
rowDateType.DateType = "xxx"
rowDateType.CreateDate = Now
rowDateType.ChangeDate = Now
rowDateType.Active = True
rowDateType.ID = Integer.MaxValue
tblDateType.AddDateTypeRow(rowDateType)

Try
da.InsertCommand.Parameters.Add("@ID",
OleDb.OleDbType.Integer, "ID")
Catch er As Exception
MessageBox.Show("Type = " &
er.GetType.ToString & vbCr & "Message = " & er.Message)
End Try

da.InsertCommand.Parameters.Add("@DateType",
OleDb.OleDbType.VarChar, 255, "DateType")
da.InsertCommand.Parameters.Add("@CreateDate",
OleDb.OleDbType.Date, "CreateDate")
da.InsertCommand.Parameters.Add("@ChangeDate",
OleDb.OleDbType.Date, "ChangeDate")
da.InsertCommand.Parameters.Add("@Active",
OleDb.OleDbType.Boolean, "Active")


Try
da.Update(dsA, "DateType")
Catch er As System.Exception
MessageBox.Show("Type = " &
er.GetType.ToString & vbCr & "Message = " & er.Message)
Finally
cn.Close()
End Try

End Sub

End Class

At the end of the last discussion the last line of code
was:

tblDateType.AddDateTypeRow(rowDateType)

Nobody by that time could tell me why my access database
wasn't actually updated. By looking at the two books, I
figured what I was missing was the dataadapter update
statement. From the error that ensued I learned I needed
to create the insert command. Then things got
complicated because of the parameter collection. The
code you see is only one of the variations, but the one
that I think is the best.

At least most of the people who read this newsgroup can
add a new row to a database. Certainly all the Microsoft
people who monitor the newsgroup for we MSDN subscribers
know how. So I can't understand why I can't get an
answer. I'll keep on trying for a while, then resort to
using one of my 4 direct consultations as a universal
subscriber. I would prefer not to resort to that.



dennist
-----Original Message-----
I am performing research on this, and will get back to you.

Sincerely,

Kevin
Microsoft Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Get Secure! - www.microsoft.com/security

--------------------
| Content-Class: urn:content-classes:message
| From: <[email protected]>
| Sender: <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| Subject: RE: can't add row to ms access table
| Date: Mon, 20 Oct 2003 05:32:07 -0700
| Lines: 166
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcOXBi0a1lKbFpYrQke29kS7zEX1Cw==
| Newsgroups: microsoft.public.dotnet.framework.adonet
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.adonet:64053
| NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
|
| Kevin,
|
| I got farther, but not quite there. The problem may have
| to to with my autoincrementing ID, which your example
| doesn't have.
|
| Here was your revision:
|
| Private Sub Button3_Click(ByVal sender As System.Object,
| ByVal e As
| System.EventArgs) Handles Button3.Click
| ' da.Fill(ds, "DateType")
| Dim strConn, strSQL As String
| strConn
= "Provider=Microsoft.Jet.OLEDB.4.0;Data
| Source
| =D:\Northwind2000.mdb;"
|
| Dim cn As New System.Data.OleDb.OleDbConnection
| (strConn)
| strSQL = "SELECT * FROM customers ORDER BY
| customerID;"
|
| Dim da As New System.Data.OleDb.OleDbDataAdapter
| (strSQL, cn)
|
| Dim ds As New ds1
| cn.Open()
|
| da.Fill(ds, ds.Tables(0).TableName)
|
| Dim tblDateType As ds1.DateTypeDataTable =
| ds.Tables(0)
| 'tblDateType.NewDateTypeRow()
| Dim rowDateType As ds1.DateTypeRow
| rowDateType = tblDateType.NewDateTypeRow()
| rowDateType.Country = "xxx"
| rowDateType.CustomerID = "AAA" ' 'this is
| primary key
| tblDateType.AddDateTypeRow(rowDateType)
| '...
|
| End Sub
| ================
| And this was my adaptation:
|
| If I dim ds as new ds1 I get the error message 'local
| variable ds already declared in current block. Why did it
| work for you? I want to understand.
|
| Therefore I tried
|
| dim dsA as new ds1
| that worked.
|
| Why did you use the statement,
| da.Fill(ds, ds.Tables(0).TableName)
| instead of da.fill(ds,"Customers")
|
| In any case, here was my code:
|
| Dim ds As New DataSet
| ds.DataSetName = "ds1"
| cn.Open()
| da.FillSchema(ds, SchemaType.Source, "DateType")
| 'cn.Close()
| ds.WriteXmlSchema("H:\HasbaraNET\ado.net
| tests\CodeUpdate\ds.xsd")
|
| Dim dsA As New ds1
|
| da.Fill(dsA, dsA.Tables(0).TableName)
| MsgBox(dsA.Tables(0).TableName.ToString)
| Dim tblDateType As ds1.DateTypeDataTable =
| dsA.Tables(0)
| Dim rowDateType As ds1.DateTypeRow
| rowDateType = tblDateType.NewDateTypeRow
| rowDateType.DateType = "xxx"
| rowDateType.CreateDate = Now
| rowDateType.ChangeDate = Now
| rowDateType.Active = True
| tblDateType.AddDateTypeRow(rowDateType)
|
| When I ran it, it ran without error. However, when I
| opened the database there was no new row.
|
| I wasn't entirely surprised, because I didn't have a
| value for ID. But what should that statement look like?
| It's an autoincrementing ID, so the statement
|
| rowDateType.ID = ???
| I can't select it. How to I put in a new row with
| autoincrementing keyindexes. Most of my tables are like
| that.
|
| I'm almost there, but not quite. I'll be there when the
| program runs AND I open the database and the new row is
| there.
|
| Thanks for your help.
|
| dennist
|
| >-----Original Message-----
| >Hi Dennis,
| >
| >I change the code and test it on my side, it works fine
| on my side. Note to
| >refer tblDateType object to a table in the dataset.
| >
| >==============
| > Private Sub Button3_Click(ByVal sender As
| System.Object, ByVal e As
| >System.EventArgs) Handles Button3.Click
| > ' da.Fill(ds, "DateType")
| > Dim strConn, strSQL As String
| > strConn
= "Provider=Microsoft.Jet.OLEDB.4.0;Data
 
Oops, forgot.

When I run this version, the first try produces the
following error message:

Type System.InvalidCastException
Message = Cast from string "ID" to type 'Integer is not
valid.
 
Hi Dennist,

I understand the key point is that this problem occur on your side;
however, it doesn't occur on my side.

If possible, could you please simplify the code and the backend Access
database, compress them to a .zip file and send me with reproducing steps?
I am be able to reproduce the same problem on my side and find out the root
reason.

Sincerely,

Kevin
Microsoft Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Get Secure! - www.microsoft.com/security

--------------------
| Content-Class: urn:content-classes:message
| From: <[email protected]>
| Sender: <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| Subject: RE: can't add row to ms access table
| Date: Tue, 21 Oct 2003 07:51:45 -0700
| Lines: 407
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcOX4tjLcr8JJGhjQI6PdFR20uAHkg==
| Newsgroups: microsoft.public.dotnet.framework.adonet
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.adonet:64152
| NNTP-Posting-Host: TK2MSFTNGXA13 10.40.1.165
| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
|
|
|
| I was helped trememdously by people in my thread. From
| ado.net core reference and programming microsoft visual
| basic .net for microsoft access databases, I figured the
| reason I couldn't update was twofold.
|
| As a prelude, here is my entire code. I create a
| database with one simple table in it. It's called
| datetypes, with 9 entries, and 5 columns. The five
| columns are:
|
| ID autoincrementing integer
| DateType String
| CreateDate = Date, default Now
| ChangeDate = Date, default Now
| Active = Boolean, default True
|
| Now the code:
|
| Imports System.Data
| Imports System.Data.OleDb
|
|
| Public Class Form1
| Inherits System.Windows.Forms.Form
|
| #Region " Windows Form Designer generated code "
|
| Public Sub New()
| MyBase.New()
|
| 'This call is required by the Windows Form
| Designer.
| InitializeComponent()
|
| 'Add any initialization after the
| InitializeComponent() call
|
| End Sub
|
| 'Form overrides dispose to clean up the component
| list.
| Protected Overloads Overrides Sub Dispose(ByVal
| disposing As Boolean)
| If disposing Then
| If Not (components Is Nothing) Then
| components.Dispose()
| End If
| End If
| MyBase.Dispose(disposing)
| End Sub
|
| 'Required by the Windows Form Designer
| Private components As System.ComponentModel.IContainer
|
| 'NOTE: The following procedure is required by the
| Windows Form Designer
| 'It can be modified using the Windows Form Designer.
| 'Do not modify it using the code editor.
| Friend WithEvents btnBuild As
| System.Windows.Forms.Button
| <System.Diagnostics.DebuggerStepThrough()> Private
| Sub InitializeComponent()
| Me.btnBuild = New System.Windows.Forms.Button
| Me.SuspendLayout()
| '
| 'btnBuild
| '
| Me.btnBuild.Location = New System.Drawing.Point
| (16, 24)
| Me.btnBuild.Name = "btnBuild"
| Me.btnBuild.Size = New System.Drawing.Size(72, 24)
| Me.btnBuild.TabIndex = 0
| Me.btnBuild.Text = "Build"
| '
| 'Form1
| '
| Me.AutoScaleBaseSize = New System.Drawing.Size(5,
| 13)
| Me.ClientSize = New System.Drawing.Size(292, 266)
| Me.Controls.Add(Me.btnBuild)
| Me.Name = "Form1"
| Me.Text = "Form1"
| Me.ResumeLayout(False)
|
| End Sub
|
| #End Region
|
| Private Sub btnBuild_Click(ByVal sender As
| System.Object, ByVal e As System.EventArgs) Handles
| btnBuild.Click
|
| Dim strConn, strSQL As String
| strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data
| Source=H:\HasbaraNET\ado.net tests\DateTypes.mdb;"
| Dim cn As New OleDbConnection(strConn)
| strSQL = "SELECT * FROM DateType ORDER BY
| DateType;"
| Dim da As New OleDbDataAdapter(strSQL, cn)
|
| Dim ds As New DataSet
| ds.DataSetName = "ds1"
| cn.Open()
| da.FillSchema(ds, SchemaType.Source, "DateType")
| 'cn.Close()
| ds.WriteXmlSchema("H:\HasbaraNET\ado.net
| tests\CodeUpdate\ds.xsd")
|
| Dim dsA As New ds1
|
| da.Fill(dsA, dsA.Tables(0).TableName)
| Dim tblDateType As ds1.DateTypeDataTable =
| dsA.Tables(0)
| Dim rowDateType As ds1.DateTypeRow
| rowDateType = tblDateType.NewDateTypeRow
| rowDateType.DateType = "xxx"
| rowDateType.CreateDate = Now
| rowDateType.ChangeDate = Now
| rowDateType.Active = True
| rowDateType.ID = Integer.MaxValue
| tblDateType.AddDateTypeRow(rowDateType)
|
| Try
| da.InsertCommand.Parameters.Add("@ID",
| OleDb.OleDbType.Integer, "ID")
| Catch er As Exception
| MessageBox.Show("Type = " &
| er.GetType.ToString & vbCr & "Message = " & er.Message)
| End Try
|
| da.InsertCommand.Parameters.Add("@DateType",
| OleDb.OleDbType.VarChar, 255, "DateType")
| da.InsertCommand.Parameters.Add("@CreateDate",
| OleDb.OleDbType.Date, "CreateDate")
| da.InsertCommand.Parameters.Add("@ChangeDate",
| OleDb.OleDbType.Date, "ChangeDate")
| da.InsertCommand.Parameters.Add("@Active",
| OleDb.OleDbType.Boolean, "Active")
|
|
| Try
| da.Update(dsA, "DateType")
| Catch er As System.Exception
| MessageBox.Show("Type = " &
| er.GetType.ToString & vbCr & "Message = " & er.Message)
| Finally
| cn.Close()
| End Try
|
| End Sub
|
| End Class
|
| At the end of the last discussion the last line of code
| was:
|
| tblDateType.AddDateTypeRow(rowDateType)
|
| Nobody by that time could tell me why my access database
| wasn't actually updated. By looking at the two books, I
| figured what I was missing was the dataadapter update
| statement. From the error that ensued I learned I needed
| to create the insert command. Then things got
| complicated because of the parameter collection. The
| code you see is only one of the variations, but the one
| that I think is the best.
|
| At least most of the people who read this newsgroup can
| add a new row to a database. Certainly all the Microsoft
| people who monitor the newsgroup for we MSDN subscribers
| know how. So I can't understand why I can't get an
| answer. I'll keep on trying for a while, then resort to
| using one of my 4 direct consultations as a universal
| subscriber. I would prefer not to resort to that.
|
|
|
| dennist
| >-----Original Message-----
| >I am performing research on this, and will get back to
| you.
| >
| >Sincerely,
| >
| >Kevin
| >Microsoft Support
| >
| >This posting is provided "AS IS" with no warranties, and
| confers no rights.
| >Get Secure! - www.microsoft.com/security
| >
| >--------------------
| >| Content-Class: urn:content-classes:message
| >| From: <[email protected]>
| >| Sender: <[email protected]>
| >| References: <[email protected]>
| ><[email protected]>
| ><[email protected]>
| ><[email protected]>
| ><[email protected]>
| ><[email protected]>
| >| Subject: RE: can't add row to ms access table
| >| Date: Mon, 20 Oct 2003 05:32:07 -0700
| >| Lines: 166
| >| Message-ID: <[email protected]>
| >| MIME-Version: 1.0
| >| Content-Type: text/plain;
| >| charset="iso-8859-1"
| >| Content-Transfer-Encoding: 7bit
| >| X-Newsreader: Microsoft CDO for Windows 2000
| >| X-MimeOLE: Produced By Microsoft MimeOLE
| V5.50.4910.0300
| >| Thread-Index: AcOXBi0a1lKbFpYrQke29kS7zEX1Cw==
| >| Newsgroups: microsoft.public.dotnet.framework.adonet
| >| Path: cpmsftngxa06.phx.gbl
| >| Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.framework.adonet:64053
| >| NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
| >| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
| >|
| >| Kevin,
| >|
| >| I got farther, but not quite there. The problem may
| have
| >| to to with my autoincrementing ID, which your example
| >| doesn't have.
| >|
| >| Here was your revision:
| >|
| >| Private Sub Button3_Click(ByVal sender As
| System.Object,
| >| ByVal e As
| >| System.EventArgs) Handles Button3.Click
| >| ' da.Fill(ds, "DateType")
| >| Dim strConn, strSQL As String
| >| strConn
| = "Provider=Microsoft.Jet.OLEDB.4.0;Data
| >| Source
| >| =D:\Northwind2000.mdb;"
| >|
| >| Dim cn As New System.Data.OleDb.OleDbConnection
| >| (strConn)
| >| strSQL = "SELECT * FROM customers ORDER BY
| >| customerID;"
| >|
| >| Dim da As New
| System.Data.OleDb.OleDbDataAdapter
| >| (strSQL, cn)
| >|
| >| Dim ds As New ds1
| >| cn.Open()
| >|
| >| da.Fill(ds, ds.Tables(0).TableName)
| >|
| >| Dim tblDateType As ds1.DateTypeDataTable =
| >| ds.Tables(0)
| >| 'tblDateType.NewDateTypeRow()
| >| Dim rowDateType As ds1.DateTypeRow
| >| rowDateType = tblDateType.NewDateTypeRow()
| >| rowDateType.Country = "xxx"
| >| rowDateType.CustomerID = "AAA" ' 'this is
| >| primary key
| >| tblDateType.AddDateTypeRow(rowDateType)
| >| '...
| >|
| >| End Sub
| >| ================
| >| And this was my adaptation:
| >|
| >| If I dim ds as new ds1 I get the error message 'local
| >| variable ds already declared in current block. Why did
| it
| >| work for you? I want to understand.
| >|
| >| Therefore I tried
| >|
| >| dim dsA as new ds1
| >| that worked.
| >|
| >| Why did you use the statement,
| >| da.Fill(ds, ds.Tables(0).TableName)
| >| instead of da.fill(ds,"Customers")
| >|
| >| In any case, here was my code:
| >|
| >| Dim ds As New DataSet
| >| ds.DataSetName = "ds1"
| >| cn.Open()
| >| da.FillSchema(ds,
| SchemaType.Source, "DateType")
| >| 'cn.Close()
| >| ds.WriteXmlSchema("H:\HasbaraNET\ado.net
| >| tests\CodeUpdate\ds.xsd")
| >|
| >| Dim dsA As New ds1
| >|
| >| da.Fill(dsA, dsA.Tables(0).TableName)
| >| MsgBox(dsA.Tables(0).TableName.ToString)
| >| Dim tblDateType As ds1.DateTypeDataTable =
| >| dsA.Tables(0)
| >| Dim rowDateType As ds1.DateTypeRow
| >| rowDateType = tblDateType.NewDateTypeRow
| >| rowDateType.DateType = "xxx"
| >| rowDateType.CreateDate = Now
| >| rowDateType.ChangeDate = Now
| >| rowDateType.Active = True
| >| tblDateType.AddDateTypeRow(rowDateType)
| >|
| >| When I ran it, it ran without error. However, when I
| >| opened the database there was no new row.
| >|
| >| I wasn't entirely surprised, because I didn't have a
| >| value for ID. But what should that statement look
| like?
| >| It's an autoincrementing ID, so the statement
| >|
| >| rowDateType.ID = ???
| >| I can't select it. How to I put in a new row with
| >| autoincrementing keyindexes. Most of my tables are
| like
| >| that.
| >|
| >| I'm almost there, but not quite. I'll be there when
| the
| >| program runs AND I open the database and the new row
| is
| >| there.
| >|
| >| Thanks for your help.
| >|
| >| dennist
| >|
| >| >-----Original Message-----
| >| >Hi Dennis,
| >| >
| >| >I change the code and test it on my side, it works
| fine
| >| on my side. Note to
| >| >refer tblDateType object to a table in the dataset.
| >| >
| >| >==============
| >| > Private Sub Button3_Click(ByVal sender As
| >| System.Object, ByVal e As
| >| >System.EventArgs) Handles Button3.Click
| >| > ' da.Fill(ds, "DateType")
| >| > Dim strConn, strSQL As String
| >| > strConn
| = "Provider=Microsoft.Jet.OLEDB.4.0;Data
| >| Source
| >| >=D:\Northwind2000.mdb;"
| >| >
| >| > Dim cn As New
| System.Data.OleDb.OleDbConnection
| >| (strConn)
| >| > strSQL = "SELECT * FROM customers ORDER BY
| >| customerID;"
| >| >
| >| > Dim da As New
| System.Data.OleDb.OleDbDataAdapter
| >| (strSQL, cn)
| >| >
| >| > Dim ds As New ds1
| >| > cn.Open()
| >| >
| >| > da.Fill(ds, ds.Tables(0).TableName)
| >| >
| >| > Dim tblDateType As ds1.DateTypeDataTable =
| >| ds.Tables(0)
| >| > 'tblDateType.NewDateTypeRow()
| >| > Dim rowDateType As ds1.DateTypeRow
| >| > rowDateType = tblDateType.NewDateTypeRow()
| >| > rowDateType.Country = "xxx"
| >| > rowDateType.CustomerID = "AAA" ' 'this is
| >| primary key
| >| > tblDateType.AddDateTypeRow(rowDateType)
| >| > '...
| >| >
| >| > End Sub
| >| >================
| >| >
| >| >Working with a Typed DataSet
| >| >
| >| >http://msdn.microsoft.com/library/default.asp?
| >| url=/library/en-us/cpguide/htm
| >| >l/cpconworkingwithtypeddataset.asp:
| >| >http://msdn.microsoft.com/library/default.asp?
| >| url=/library/en-us/cpguide/htm
| >| >l/cpconworkingwithtypeddataset.asp
| >| >
| >| >
| >| >
| >| >Sincerely,
| >| >
| >| >Kevin
| >| >Microsoft Support
| >| >
| >| >This posting is provided "AS IS" with no warranties,
| and
| >| confers no rights.
| >| >Get Secure! - www.microsoft.com/security
| >|
| >|
| >
| >.
| >
|
 
Hi Dennis,

I list some materials regarding how to handle Autoincrement value in
ADO.NET, please refer to the following:

Retrieving Newly Generated Autoincrement Values
=========================================

SQL Server, Access, Sybase, and other databases use autoincrement columns
(also referred to as identity columns). You can insert a new row into a
table, and the database will generate a new value for the autoincrement
column for that row. Many tables in the Northwind database-such as
Employees, Orders, and Products-use autoincrement columns for their primary
keys.

Why does working with autoincrement columns constitute an advanced updating
scenario? You can submit the new row to your table, but the database will
generate the value for that row's autoincrement column. This means you
won't know the value of the autoincrement column for your new row once
you've submitted the new row to your table. Generally speaking, you want to
know the primary key value for your rows.

So how do you use the ADO.NET object model to retrieve the newly generated
autoincrement value
for your row?

Working with SQL Server
======================
For the moment, imagine that you're not submitting changes to your database
using a DataAdapter. Let's say you're building your own queries to submit
changes.
If you're working with order information from the Northwind database, you
might use the following query to retrieve data from your table:
SELECT OrderID, CustomerID, EmployeeID, OrderDate FROM Orders
To insert a new row into your table, you might issue a query like this:
INSERT INTO Orders (CustomerID, EmployeeID, OrderDate) VALUES (?, ?, ?)
To retrieve the autoincrement value that the database generated for your
new row, you might use the following query:



This query is the key to retrieving the new autoincrement value. We can use
this query in the ADO.NET object model the same way we used the query to
retrieve the timestamp value in the previous example.
We can modify the CommandText of the DataAdapter object's InsertCommand to
execute the SELECT @@IDENTITY query after each insert:
INSERT INTO Orders (CustomerID, EmployeeID, OrderDate) VALUES (?, ?, ?);
SELECT @@IDENTITY AS OrderID

Note that the SELECT @@IDENTITY query includes an alias so that the Command
knows the column in which it should store the results of the query.

As with fetching new timestamp values, we can also return the new
autoincrement value by using a stored procedure output parameter, as shown
here:

CREATE PROCEDURE spOrdersInsert
(@OrderID int OUTPUT, @CustomerID nchar(5),
@EmployeeID int, @OrderDate datetime)
AS
INSERT INTO Orders (CustomerID, EmployeeID, OrderDate)
VALUES (@CustomerID, @EmployeeID, @OrderDate)
SELECT @OrderID = @@IDENTITY

Finally, we can use the DataAdapter object's RowUpdated event to execute a
query to fetch the new autoincrement value, as shown in the following code
samples:

Visual Basic .NET
=================
Dim da As OleDbDataAdapter = CreateMyDataAdapter()
Dim cn As OleDbConnection = da.SelectCommand.Connection
Dim cmdGetIdentity As New OleDbCommand("SELECT @@IDENTITY", cn)
AddHandler da.RowUpdated, AddressOf HandleRowUpdated
Dim tbl As DataTable = CreateMyDataTable()
da.Fill(tbl)


da.Update(tbl)

Private Sub HandleRowUpdated(ByVal sender As Object, _
ByVal e As OleDbRowUpdatedEventArgs)
If e.Status = UpdateStatus.Continue AndAlso _
e.StatementType = StatementType.Insert Then
e.Row("OrderID") = CType(cmdGetIdentity.ExecuteScalar, Integer)
e.Row.AcceptChanges()
End If
End Sub
=====================


Using Batch Queries to Retrieve Data After You Submit an Update
========================================================

How to use a DataAdapter to fetch the results of a batch query such as this
one:

SELECT CustomerID, CompanyName, ContactName, Phone FROM Customers;
SELECT OrderID, CustomerID, EmployeeID, OrderDate FROM Orders

You can also use batched queries to retrieve data after you submit an
update. You can combine the update query and the query to retrieve the new
timestamp value by using a batched query. Set the CommandText of the
DataAdapter object¡¯s UpdateCommand to the following parameterized query:

UPDATE [Order Details]
SET OrderID = ?, ProductID = ?, Quantity = ?, UnitPrice = ?
WHERE OrderID = ? AND ProductID = ? AND TSCol = ?;
SELECT TSCol FROM [Order Details] WHERE OrderID = ? AND ProductID = ?

When the DataAdapter submits the pending change, it will also execute the
subsequent SELECT query and store the results in the modified DataRow. The
DataRow object will have the newly generated value for the timestamp
column, which means you can modify the row again and successfully submit
those new changes.

Hope this help


Sincerely,

Kevin
Microsoft Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Get Secure! - www.microsoft.com/security

--------------------
| Content-Class: urn:content-classes:message
| From: <[email protected]>
| Sender: <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| Subject: RE: can't add row to ms access table
| Date: Tue, 21 Oct 2003 07:58:06 -0700
| Lines: 10
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcOX47v/GkIw209lTM+ad964FcfPLw==
| Newsgroups: microsoft.public.dotnet.framework.adonet
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.adonet:64153
| NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
|
| Oops, forgot.
|
| When I run this version, the first try produces the
| following error message:
|
| Type System.InvalidCastException
| Message = Cast from string "ID" to type 'Integer is not
| valid.
|
|
|
 
Thank you very much. The material is quite helpful. I
haven't fully translated it yet into microsoft access
form.

dennist
-----Original Message-----
Hi Dennis,

I list some materials regarding how to handle Autoincrement value in
ADO.NET, please refer to the following:

Retrieving Newly Generated Autoincrement Values
=========================================

SQL Server, Access, Sybase, and other databases use autoincrement columns
(also referred to as identity columns). You can insert a new row into a
table, and the database will generate a new value for the autoincrement
column for that row. Many tables in the Northwind database-such as
Employees, Orders, and Products-use autoincrement columns for their primary
keys.

Why does working with autoincrement columns constitute an advanced updating
scenario? You can submit the new row to your table, but the database will
generate the value for that row's autoincrement column. This means you
won't know the value of the autoincrement column for your new row once
you've submitted the new row to your table. Generally speaking, you want to
know the primary key value for your rows.

So how do you use the ADO.NET object model to retrieve the newly generated
autoincrement value
for your row?

Working with SQL Server
======================
For the moment, imagine that you're not submitting changes to your database
using a DataAdapter. Let's say you're building your own queries to submit
changes.
If you're working with order information from the Northwind database, you
might use the following query to retrieve data from your table:
SELECT OrderID, CustomerID, EmployeeID, OrderDate FROM Orders
To insert a new row into your table, you might issue a query like this:
INSERT INTO Orders (CustomerID, EmployeeID, OrderDate) VALUES (?, ?, ?)
To retrieve the autoincrement value that the database generated for your
new row, you might use the following query:



This query is the key to retrieving the new
autoincrement value. We can use
this query in the ADO.NET object model the same way we used the query to
retrieve the timestamp value in the previous example.
We can modify the CommandText of the DataAdapter object's InsertCommand to
execute the SELECT @@IDENTITY query after each insert:
INSERT INTO Orders (CustomerID, EmployeeID, OrderDate) VALUES (?, ?, ?);
SELECT @@IDENTITY AS OrderID

Note that the SELECT @@IDENTITY query includes an alias so that the Command
knows the column in which it should store the results of the query.

As with fetching new timestamp values, we can also return the new
autoincrement value by using a stored procedure output parameter, as shown
here:

CREATE PROCEDURE spOrdersInsert
(@OrderID int OUTPUT, @CustomerID nchar(5),
@EmployeeID int, @OrderDate datetime)
AS
INSERT INTO Orders (CustomerID, EmployeeID, OrderDate)
VALUES (@CustomerID, @EmployeeID, @OrderDate)
SELECT @OrderID = @@IDENTITY

Finally, we can use the DataAdapter object's RowUpdated event to execute a
query to fetch the new autoincrement value, as shown in the following code
samples:

Visual Basic .NET
=================
Dim da As OleDbDataAdapter = CreateMyDataAdapter()
Dim cn As OleDbConnection = da.SelectCommand.Connection
Dim cmdGetIdentity As New OleDbCommand("SELECT @@IDENTITY", cn)
AddHandler da.RowUpdated, AddressOf HandleRowUpdated
Dim tbl As DataTable = CreateMyDataTable()
da.Fill(tbl)


da.Update(tbl)

Private Sub HandleRowUpdated(ByVal sender As Object, _
ByVal e As OleDbRowUpdatedEventArgs)
If e.Status = UpdateStatus.Continue AndAlso _
e.StatementType = StatementType.Insert Then
e.Row("OrderID") = CType
(cmdGetIdentity.ExecuteScalar, Integer)
e.Row.AcceptChanges()
End If
End Sub
=====================


Using Batch Queries to Retrieve Data After You Submit an Update
========================================================

How to use a DataAdapter to fetch the results of a batch query such as this
one:

SELECT CustomerID, CompanyName, ContactName, Phone FROM Customers;
SELECT OrderID, CustomerID, EmployeeID, OrderDate FROM Orders

You can also use batched queries to retrieve data after you submit an
update. You can combine the update query and the query to retrieve the new
timestamp value by using a batched query. Set the CommandText of the
DataAdapter object¡¯s UpdateCommand to the following parameterized query:

UPDATE [Order Details]
SET OrderID = ?, ProductID = ?, Quantity = ?, UnitPrice = ?
WHERE OrderID = ? AND ProductID = ? AND TSCol = ?;
SELECT TSCol FROM [Order Details] WHERE OrderID = ? AND ProductID = ?

When the DataAdapter submits the pending change, it will also execute the
subsequent SELECT query and store the results in the modified DataRow. The
DataRow object will have the newly generated value for the timestamp
column, which means you can modify the row again and successfully submit
those new changes.

Hope this help


Sincerely,

Kevin
Microsoft Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Get Secure! - www.microsoft.com/security

--------------------
| Content-Class: urn:content-classes:message
| From: <[email protected]>
| Sender: <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| Subject: RE: can't add row to ms access table
| Date: Tue, 21 Oct 2003 07:58:06 -0700
| Lines: 10
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcOX47v/GkIw209lTM+ad964FcfPLw==
| Newsgroups: microsoft.public.dotnet.framework.adonet
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.adonet:64153
| NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
|
| Oops, forgot.
|
| When I run this version, the first try produces the
| following error message:
|
| Type System.InvalidCastException
| Message = Cast from string "ID" to type 'Integer is not
| valid.
|
|
|

.
 
Hi Dennist,

The following is additional information about Access database to retrieve
Newly Generated Autoincrement Values

==============
Working with Access 2000

If you¡¯re working with an Access database, you can also use the SELECT
@@IDENTITY query to retrieve new autoincrement values. This feature was
added in version 4.0 of the Jet OLE DB provider and works only with
databases formatted for Access databases version 2000 or later. Like its
SQL Server counterpart, the SELECT @@IDENTITY query returns the last
autoincrement value generated on your connection.

Access databases do not support output parameters on QueryDefs¡ªstored
queries that are similar to views and stored procedures. The Jet OLE DB
provider does not support batch queries. So the only way to fetch newly
generated autoincrement values is to use the DataAdapter object¡¯s
RowUpdated event, as shown earlier in the chapter.

===============

Sincerely,

Kevin
Microsoft Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Get Secure! - www.microsoft.com/security

--------------------
| Content-Class: urn:content-classes:message
| From: <[email protected]>
| Sender: <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| Subject: RE: can't add row to ms access table
| Date: Tue, 28 Oct 2003 10:28:27 -0800
| Lines: 230
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: quoted-printable
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcOdgUeqO/+OYTDSSO2eloG/OGK03Q==
| Newsgroups: microsoft.public.dotnet.framework.adonet
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.adonet:64761
| NNTP-Posting-Host: TK2MSFTNGXA09 10.40.1.161
| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
|
| Thank you very much. The material is quite helpful. I
| haven't fully translated it yet into microsoft access
| form.
| dennist
| >-----Original Message-----
| >Hi Dennis,
| >
| >I list some materials regarding how to handle
| Autoincrement value in
| >ADO.NET, please refer to the following:
| >
| >Retrieving Newly Generated Autoincrement Values
| >=========================================
| >
| >SQL Server, Access, Sybase, and other databases use
| autoincrement columns
| >(also referred to as identity columns). You can insert a
| new row into a
| >table, and the database will generate a new value for
| the autoincrement
| >column for that row. Many tables in the Northwind
| database-such as
| >Employees, Orders, and Products-use autoincrement
| columns for their primary
| >keys.
| >
| >Why does working with autoincrement columns constitute
| an advanced updating
| >scenario? You can submit the new row to your table, but
| the database will
| >generate the value for that row's autoincrement column.
| This means you
| >won't know the value of the autoincrement column for
| your new row once
| >you've submitted the new row to your table. Generally
| speaking, you want to
| >know the primary key value for your rows.
| >
| >So how do you use the ADO.NET object model to retrieve
| the newly generated
| >autoincrement value
| >for your row?
| >
| >Working with SQL Server
| >======================
| >For the moment, imagine that you're not submitting
| changes to your database
| >using a DataAdapter. Let's say you're building your own
| queries to submit
| >changes.
| >If you're working with order information from the
| Northwind database, you
| >might use the following query to retrieve data from your
| table:
| >SELECT OrderID, CustomerID, EmployeeID, OrderDate FROM
| Orders
| >To insert a new row into your table, you might issue a
| query like this:
| >INSERT INTO Orders (CustomerID, EmployeeID, OrderDate)
| VALUES (?, ?, ?)
| >To retrieve the autoincrement value that the database
| generated for your
| >new row, you might use the following query:
| >
| >
| >
| >This query is the key to retrieving the new
| autoincrement value. We can use
| >this query in the ADO.NET object model the same way we
| used the query to
| >retrieve the timestamp value in the previous example.
| >We can modify the CommandText of the DataAdapter
| object's InsertCommand to
| >execute the SELECT @@IDENTITY query after each insert:
| >INSERT INTO Orders (CustomerID, EmployeeID, OrderDate)
| VALUES (?, ?, ?);
| >SELECT @@IDENTITY AS OrderID
| >
| >Note that the SELECT @@IDENTITY query includes an alias
| so that the Command
| >knows the column in which it should store the results of
| the query.
| >
| >As with fetching new timestamp values, we can also
| return the new
| >autoincrement value by using a stored procedure output
| parameter, as shown
| >here:
| >
| >CREATE PROCEDURE spOrdersInsert
| >(@OrderID int OUTPUT, @CustomerID nchar(5),
| > @EmployeeID int, @OrderDate datetime)
| >AS
| >INSERT INTO Orders (CustomerID, EmployeeID, OrderDate)
| > VALUES (@CustomerID, @EmployeeID, @OrderDate)
| >SELECT @OrderID = @@IDENTITY
| >
| >Finally, we can use the DataAdapter object's RowUpdated
| event to execute a
| >query to fetch the new autoincrement value, as shown in
| the following code
| >samples:
| >
| >Visual Basic .NET
| >=================
| >Dim da As OleDbDataAdapter = CreateMyDataAdapter()
| >Dim cn As OleDbConnection = da.SelectCommand.Connection
| >Dim cmdGetIdentity As New OleDbCommand("SELECT
| @@IDENTITY", cn)
| >AddHandler da.RowUpdated, AddressOf HandleRowUpdated
| >Dim tbl As DataTable = CreateMyDataTable()
| >da.Fill(tbl)
| >
| >
| >da.Update(tbl)
| >
| >Private Sub HandleRowUpdated(ByVal sender As Object, _
| > ByVal e As
| OleDbRowUpdatedEventArgs)
| > If e.Status = UpdateStatus.Continue AndAlso _
| > e.StatementType = StatementType.Insert Then
| > e.Row("OrderID") = CType
| (cmdGetIdentity.ExecuteScalar, Integer)
| > e.Row.AcceptChanges()
| > End If
| >End Sub
| >=====================
| >
| >
| >Using Batch Queries to Retrieve Data After You Submit an
| Update
| >========================================================
| >
| >How to use a DataAdapter to fetch the results of a batch
| query such as this
| >one:
| >
| >SELECT CustomerID, CompanyName, ContactName, Phone FROM
| Customers;
| >SELECT OrderID, CustomerID, EmployeeID, OrderDate FROM
| Orders
| >
| >You can also use batched queries to retrieve data after
| you submit an
| >update. You can combine the update query and the query
| to retrieve the new
| >timestamp value by using a batched query. Set the
| CommandText of the
| >DataAdapter object¡¯s UpdateCommand to the following
| parameterized query:
| >
| >UPDATE [Order Details]
| > SET OrderID = ?, ProductID = ?, Quantity = ?,
| UnitPrice = ?
| > WHERE OrderID = ? AND ProductID = ? AND TSCol = ?;
| >SELECT TSCol FROM [Order Details] WHERE OrderID = ? AND
| ProductID = ?
| >
| >When the DataAdapter submits the pending change, it will
| also execute the
| >subsequent SELECT query and store the results in the
| modified DataRow. The
| >DataRow object will have the newly generated value for
| the timestamp
| >column, which means you can modify the row again and
| successfully submit
| >those new changes.
| >
| >Hope this help
| >
| >
| >Sincerely,
| >
| >Kevin
| >Microsoft Support
| >
| >This posting is provided "AS IS" with no warranties, and
| confers no rights.
| >Get Secure! - www.microsoft.com/security
| >
| >--------------------
| >| Content-Class: urn:content-classes:message
| >| From: <[email protected]>
| >| Sender: <[email protected]>
| >| References: <[email protected]>
| ><[email protected]>
| ><[email protected]>
| ><[email protected]>
| ><[email protected]>
| ><[email protected]>
| ><[email protected]>
| ><[email protected]>
| >| Subject: RE: can't add row to ms access table
| >| Date: Tue, 21 Oct 2003 07:58:06 -0700
| >| Lines: 10
| >| Message-ID: <[email protected]>
| >| MIME-Version: 1.0
| >| Content-Type: text/plain;
| >| charset="iso-8859-1"
| >| Content-Transfer-Encoding: 7bit
| >| X-Newsreader: Microsoft CDO for Windows 2000
| >| X-MimeOLE: Produced By Microsoft MimeOLE
| V5.50.4910.0300
| >| Thread-Index: AcOX47v/GkIw209lTM+ad964FcfPLw==
| >| Newsgroups: microsoft.public.dotnet.framework.adonet
| >| Path: cpmsftngxa06.phx.gbl
| >| Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.framework.adonet:64153
| >| NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
| >| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
| >|
| >| Oops, forgot.
| >|
| >| When I run this version, the first try produces the
| >| following error message:
| >|
| >| Type System.InvalidCastException
| >| Message = Cast from string "ID" to type 'Integer is
| not
| >| valid.
| >|
| >|
| >|
| >
| >.
| >
|
 
Back
Top