473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

getting pointer to proper dataAdapter from dataset or dataTable


I would like to build some generic code that is able to figure out the
correct dataAdapter to apply changes to given a form with several
dataAdapters.

Any suggestions on the following?

Thank you.

======================================
Private Sub SaveChanges(ByVal aContainer As Object)
Dim dt As DataTable
Dim ds As New DataSet
Dim bSaveMe As Boolean
Dim myDA As System.Data.SqlClient.SqlDataAdapter

For Each dt In Me.DS1.Tables
If Not dt.GetChanges(DataRowState.Modified) Is Nothing Then
bSaveMe = True
End If

If bSaveMe Then
ds.Merge(dt.Select("", "",
DataViewRowState.ModifiedCurrent))
myDA = <<<<< figure out correct dataAdapter
here>>>>>> <<<=====NEED SUGGESTION HERE
myDA.Update(ds)
End If

bSaveMe = False

Next
Exit Sub
End Sub
======================================
Nov 21 '05 #1
4 1766
Astro,

Probably you want to use the dataadapter created using the designer in your
generic code.

It is an easier approach to create the dataadapter than completly by code.

However before I write to much maybe can you tell if my expectation is
right?

Cor
Nov 21 '05 #2
Thanks Cor - I end up reading your posts a lot and have found them helpful

I want to have a 'system' where I do not have to write a line of code for
each dataAdapter on a form. On the form I currently am working on I have 7
SQLDataAdaptors --

The following allows me to catch changes on any datagrid on a form without
'hardCoding' the names of each table/datagrid....
======================
For Each dt In Me.DS1.Tables
If Not dt.GetChanges(DataRowState.Modified) Is Nothing
etc.....
======================

If the .NET object library allows me to find each table's DataAdapter (in my
Dataset) I can write a loop that walks through every table and apply changes
back to the database....again without hardcoding the names of each
dataAdapter.....

Is this clear?

If I must I can use the table name from the Dataset to loop through all the
dataAdapters on my form till I get a string match......but I wonder if this
information is not stored in the DataSet itself......
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Astro,

Probably you want to use the dataadapter created using the designer in
your generic code.

It is an easier approach to create the dataadapter than completly by code.

However before I write to much maybe can you tell if my expectation is
right?

Cor

Nov 21 '05 #3
Astro,

.....but I wonder if this information is not stored in the DataSet
itself......

It will be in the next version of VBNet.

However see by instance this sample how to make your dataadapter by code,
than you don't have it on your screen.

However you need the updatecommand, insertcommand, deletecommand properties
from the dataadapter. For that are two approaches, you use first the
designer and copies them from the designer part, or you use the
commandbuilder (which goes fine by simple selects).

See some threads I wrote about above.
http://www.windowsformsdatagridhelp....7-5595f8147624
http://msdn.microsoft.com/library/de...mberstopic.asp
http://msdn.microsoft.com/library/de...classtopic.asp
It has not really much sense if you use one dataadapter.

I don't like all those dataadapters in my form as well. I find it too a nice
approach to create a component (selecting an item component) and use that to
make a databaseclass (databaselayer).

I hope this helps,

Cor
Cor
Nov 21 '05 #4
Thanks Cor ))

this is what i ended up doing (with some code snippets lifted from

5. enders Oct 11 2002, 9:30 am post on this newsgroup:
=====================================
Private Sub SaveChanges_DA(ByVal ads As DataSet)
'saves are at table-level scope
'need to find correct dataAdapter for passed dataset
'this info is not available in this version of vb.net dataset object
'assumptions: 1) ds has one table and da has one table

Dim propInfos() As System.Reflection.PropertyInfo
Dim propInfo As System.Reflection.PropertyInfo

propInfos = Me.GetType.GetProperties()

For Each propInfo In propInfos
If propInfo.PropertyType.FullName =
"System.Data.SqlClient.SqlDataAdapter" Then
Dim da As System.Data.SqlClient.SqlDataAdapter
da = CType(propInfo.GetValue(Me, Nothing),
System.Data.SqlClient.SqlDataAdapter)
If da.TableMappings(0).DataSetTable.ToString.ToLower =
ads.Tables(0).TableName.ToString.ToLower Then
'have match - this da is the source for the passed
dataset
Try
da.Update(ads)
Me.DS1.AcceptChanges()
Catch exc As Exception
MsgBox(exc.Message)
End Try
End If

End If
Next
=====================================

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:Ot****************@TK2MSFTNGP12.phx.gbl...
Astro,

.....but I wonder if this information is not stored in the DataSet
itself......

It will be in the next version of VBNet.

However see by instance this sample how to make your dataadapter by code,
than you don't have it on your screen.

However you need the updatecommand, insertcommand, deletecommand
properties from the dataadapter. For that are two approaches, you use
first the designer and copies them from the designer part, or you use the
commandbuilder (which goes fine by simple selects).

See some threads I wrote about above.
http://www.windowsformsdatagridhelp....7-5595f8147624
http://msdn.microsoft.com/library/de...mberstopic.asp
http://msdn.microsoft.com/library/de...classtopic.asp
It has not really much sense if you use one dataadapter.

I don't like all those dataadapters in my form as well. I find it too a
nice approach to create a component (selecting an item component) and use
that to make a databaseclass (databaselayer).

I hope this helps,

Cor
Cor

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: randy | last post by:
Hello all, I have a DataTable which I am building column by column and adding rows after each new column. The DataTable columns match the columns in my database table. I'm building the...
4
by: Frnak McKenney | last post by:
I'm using an in-core DataSet as an image of my application's 'database' (a multi-table Access97 mdb file). Updates are made to the DataTables within the DataSet via forms with bound TextBoxes,...
3
by: Saso Zagoranski | last post by:
Hi! A question about database tables... I have a SELECT statement: SELECT something FROM someTable WHERE code = @code The parameter code starts with 1 and is autoincremented... Now I want...
13
by: Doug Bell | last post by:
Hi, I thought I had this sorted this morning but it is still a problem. My application has a DataAccess Class. When it starts, it: Connects to a DB (OLE DB) If it connects it uses an...
2
by: susan.f.barrett | last post by:
Hi, Despite me being able to type the following in to SQL Server and it updating 1 row: > updatestockcategory 1093, 839 In my code, it is not updating any rows. dataSet = new DataSet();
2
by: MDB | last post by:
Hello All, I have a data grid that I fill using a dataset. The results of the query has around 15 columns and 500 rows (and growing). The reason I am using the datagrid is so the end users can...
0
by: mike1402 | last post by:
Hi ! I get the error below sometimes when retrieving a big amount of data using Datadapter.Fill(dataset,"table"). But when I send the command Fill again, there is no error. Is it a fault of...
3
by: Fred Chateau | last post by:
Any obvious reason here why data is not being loaded into the database? SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlCommand); SqlCommandBuilder commandBuilder = new...
2
by: BobLewiston | last post by:
Some of you may have seen my earlier thread “PasswordHash NULL problem”. I’ve started a new thread because investigation has shown that the problem is actually quite different than I previously...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.