473,395 Members | 1,554 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,395 software developers and data experts.

Question on updating data

I am pretty new at .NET and am reading a book on ADO.NET but I need some
help.

I have the following code in the Load routine of a sample program. I would
like to read in some data and take the first row and put a few fields on a
form. Then I would like to update the data. How on earth can I update the
rows when I call an update routine? The table and datarow are defined in
the Load routine and are not global. If I have another routine, how can I
put the text fields into the table and do an update?

Should be simple, but my poor little brain is on overload. HELP

Thanks,

Gary

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim strConn As String
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\adonetsbs\SampleDBs\nwind.mdb;"

Dim cn As New OleDb.OleDbConnection(strConn)

Dim strSQL As String
strSQL = "SELECT EmployeeID, FirstName, LastName, Address, City,
Region, " & _
"PostalCode from Employees ORDER BY LastName, FirstName"

Dim da As New OleDb.OleDbDataAdapter(strSQL, strConn)
Dim ds As New DataSet

da.Fill(ds, "Employees")

cmbLastName.DataSource = ds.Tables(0)
cmbLastName.DisplayMember = "LastName"
cmbLastName.ValueMember = "EmployeeID"

Dim tbl As DataTable = ds.Tables(0)

Dim rowEmployee As DataRow = tbl.Rows(0)
txtFirstName.Text = rowEmployee("FirstName")
txtLastName.Text = rowEmployee("LastName")
txtAddress.Text = rowEmployee("Address")

End Sub
Nov 21 '05 #1
7 1029
You already recognised the answer but you haven't realised it.

<quote>
.... and are not global.
</quote>

Make them global.

Now, I hear you ask, how do I do that?

Declare the dataset object outside the event handler it will be available to
other procedures in your class (Form).

Dim ds As DataSet

Private Sub Form1_Load(...

In the Load event handler you now only have to instantiate the dataset
object instaed of declaring it also:

...
ds = New DataSet
...

It would help you to get your head around the 'scope' of variables. This
subject is covered ad nauseum in the documentation.
"Gary Paris" <te**@yada.com> wrote in message
news:OJ**************@TK2MSFTNGP10.phx.gbl...
I am pretty new at .NET and am reading a book on ADO.NET but I need some
help.

I have the following code in the Load routine of a sample program. I
would like to read in some data and take the first row and put a few
fields on a form. Then I would like to update the data. How on earth can
I update the rows when I call an update routine? The table and datarow
are defined in the Load routine and are not global. If I have another
routine, how can I put the text fields into the table and do an update?

Should be simple, but my poor little brain is on overload. HELP

Thanks,

Gary

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim strConn As String
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\adonetsbs\SampleDBs\nwind.mdb;"

Dim cn As New OleDb.OleDbConnection(strConn)

Dim strSQL As String
strSQL = "SELECT EmployeeID, FirstName, LastName, Address,
City, Region, " & _
"PostalCode from Employees ORDER BY LastName, FirstName"

Dim da As New OleDb.OleDbDataAdapter(strSQL, strConn)
Dim ds As New DataSet

da.Fill(ds, "Employees")

cmbLastName.DataSource = ds.Tables(0)
cmbLastName.DisplayMember = "LastName"
cmbLastName.ValueMember = "EmployeeID"

Dim tbl As DataTable = ds.Tables(0)

Dim rowEmployee As DataRow = tbl.Rows(0)
txtFirstName.Text = rowEmployee("FirstName")
txtLastName.Text = rowEmployee("LastName")
txtAddress.Text = rowEmployee("Address")

End Sub

Nov 21 '05 #2
Stephany,

Thanks for pointing out what I should have realized. I guess I was just
looking too hard at the code and not taking a deep breath and thinking about
the problem. I appreciate the quick reply.

Gary

"Stephany Young" <noone@localhost> wrote in message
news:ey**************@TK2MSFTNGP12.phx.gbl...
You already recognised the answer but you haven't realised it.

<quote>
... and are not global.
</quote>

Make them global.

Now, I hear you ask, how do I do that?

Declare the dataset object outside the event handler it will be available
to other procedures in your class (Form).

Dim ds As DataSet

Private Sub Form1_Load(...

In the Load event handler you now only have to instantiate the dataset
object instaed of declaring it also:

...
ds = New DataSet
...

It would help you to get your head around the 'scope' of variables. This
subject is covered ad nauseum in the documentation.
"Gary Paris" <te**@yada.com> wrote in message
news:OJ**************@TK2MSFTNGP10.phx.gbl...
I am pretty new at .NET and am reading a book on ADO.NET but I need some
help.

I have the following code in the Load routine of a sample program. I
would like to read in some data and take the first row and put a few
fields on a form. Then I would like to update the data. How on earth
can I update the rows when I call an update routine? The table and
datarow are defined in the Load routine and are not global. If I have
another routine, how can I put the text fields into the table and do an
update?

Should be simple, but my poor little brain is on overload. HELP

Thanks,

Gary

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim strConn As String
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\adonetsbs\SampleDBs\nwind.mdb;"

Dim cn As New OleDb.OleDbConnection(strConn)

Dim strSQL As String
strSQL = "SELECT EmployeeID, FirstName, LastName, Address,
City, Region, " & _
"PostalCode from Employees ORDER BY LastName, FirstName"

Dim da As New OleDb.OleDbDataAdapter(strSQL, strConn)
Dim ds As New DataSet

da.Fill(ds, "Employees")

cmbLastName.DataSource = ds.Tables(0)
cmbLastName.DisplayMember = "LastName"
cmbLastName.ValueMember = "EmployeeID"

Dim tbl As DataTable = ds.Tables(0)

Dim rowEmployee As DataRow = tbl.Rows(0)
txtFirstName.Text = rowEmployee("FirstName")
txtLastName.Text = rowEmployee("LastName")
txtAddress.Text = rowEmployee("Address")

End Sub


Nov 21 '05 #3
Gary,

When you don't want them global, you can use any reference to them. Supose
you have a datagrid with a datasource set to a dataview that you want to
update.

Than you can do
\\\
dim myfunctionDatatable as DataTable = _
directcast(Datagrid1.datasource,dataview).Table
///
Seems strange, however you don't need a global datatable in that case.

I hope this helps,

Cor
Nov 21 '05 #4
Cor,

If you dimension a datarow or datatable in a subroutine, how can you
reference it in another subroutine? Isn't it just available to that
routine?

Thanks,

Gary

"Cor Ligthert" <no************@planet.nl> wrote in message
news:u9*************@TK2MSFTNGP12.phx.gbl...
Gary,

When you don't want them global, you can use any reference to them. Supose
you have a datagrid with a datasource set to a dataview that you want to
update.

Than you can do
\\\
dim myfunctionDatatable as DataTable = _
directcast(Datagrid1.datasource,dataview).Table
///
Seems strange, however you don't need a global datatable in that case.

I hope this helps,

Cor

Nov 21 '05 #5
Gary,

See bellow

If you dimension a datarow or datatable in a subroutine, how can you
reference it in another subroutine? Isn't it just available to that
routine?

When you don't want them global, you can use any reference to them.
Supose you have a datagrid with a datasource set to a dataview that you
want to update.

Than you can do
\\\
dim myfunctionDatatable as DataTable = _
directcast(Datagrid1.datasource,dataview).Table
///


An object exist in VBNet as long as there is any reference to it. Therefore
as long as a datatable is connected to a datagrid.datasource, it is there.

Therefore you can use the sample above everywhere in our program where you
can reference that datagrid. What you do is creating a new pointer to that
datatable in the sub/function you are in..

I hope this helps,

Cor
Nov 21 '05 #6
Would it not make more sense to simply make a global object to hold that
datatable instead? The way you are doing it seems to break one of the
encapsulation rules of OOP.

--
--Eric Cathell, MCSA
"Cor Ligthert" <no************@planet.nl> wrote in message
news:Ow**************@TK2MSFTNGP12.phx.gbl...
Gary,

See bellow

If you dimension a datarow or datatable in a subroutine, how can you
reference it in another subroutine? Isn't it just available to that
routine?

When you don't want them global, you can use any reference to them.
Supose you have a datagrid with a datasource set to a dataview that you
want to update.

Than you can do
\\\
dim myfunctionDatatable as DataTable = _
directcast(Datagrid1.datasource,dataview).Table
///


An object exist in VBNet as long as there is any reference to it.
Therefore as long as a datatable is connected to a datagrid.datasource, it
is there.

Therefore you can use the sample above everywhere in our program where you
can reference that datagrid. What you do is creating a new pointer to that
datatable in the sub/function you are in..

I hope this helps,

Cor

Nov 21 '05 #7
ECathell.

What are you encapsulating with that global variable more?
It is the same (extra) reference to an object which is private in the
object.

I tried to show that there are more references possible than only those who
are set explicitly.

Cor
Nov 21 '05 #8

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

Similar topics

6
by: Hennie de Nooijer | last post by:
Hi, Currently we're a building a metadatadriven datawarehouse in SQL Server 2000. We're investigating the possibility of the updating tables with enormeous number of updates and insert and the...
0
by: AMDIRT | last post by:
I have a few questions about IssueVision (from WindowsForms) concerning its scalability and performance. Rather, if I were to implement techniques described here into another application, how...
20
by: akej via SQLMonster.com | last post by:
Hi, i have table with 15 columns CREATE TABLE . ( PRIMARY KEY , NULL , NULL , NULL , NULL , (50) NULL , NULL
3
by: suzy | last post by:
Hello, I am trying to write a generic tool that accesses a SQL server and reads/updates/deletes/creates records. I want to reference this tool from my asp.net pages to talk to my db. by the...
1
by: Natalia DeBow | last post by:
Hi, I am working on a Windows-based client-server application. I am involved in the development of the remote client modules. I am using asynchronous delegates to obtain information from...
1
by: Mark | last post by:
I'm having a problem updating recordsin an Access DB table. I can update other tables in this db with no problem, and I can dreate new record in all of the tables (including this one.)> But I can't...
14
by: el_sid | last post by:
Our developers have experienced a problem with updating Web References in Visual Studio.NET 2003. Normally, when a web service class (.asmx) is created, updating the Web Reference will...
5
by: temp2 | last post by:
Hello, I have an app that reads data params from a stream and updates controls accordingly. The stream reader is on a different thread than the main thread that created the controls. I fully...
12
by: nyathancha | last post by:
Hi, I have a question regarding best practices in database design. In a relational database, is it wise/necessary to sometimes create tables that are not related to other tables through a...
16
by: William Gill | last post by:
I am writing some new editing pages for a db using php. I want to use an object approach for the underlying MySQL tables, and the edit page will display related data from several tables. I am...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.