can't create new rows by code

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
 
C

Cowboy \(Gregory A. Beamer\)

I think the problem is the XSD file is not being seen as part of the
project. If it were seen, you should be able to instantiate it as a class,
like the code he has. My suggestion, if you are using Visual Studio .NET, is
to go to the Solution explorer and ensure you see the XSD file there. If
not, click the top icon to show all files and include it in your project.
You should be golden then.

BTW, there are a couple of even easier ways to create strongly typed
dataSets using Visual Studio .NET. ONe is the wizard, the other is the
graphical designer. While creating the schema from a DataSet is rather easy,
it is extra work.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

**********************************************************************
Think Outside the Box!
**********************************************************************
 

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