By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,256 Members | 1,329 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,256 IT Pros & Developers. It's quick & easy.

Question on updating data

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.