Question on updating data

G

Gary Paris

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
 
S

Stephany Young

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.
 
G

Gary Paris

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
 
C

Cor Ligthert

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
 
G

Gary Paris

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
 
C

Cor Ligthert

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?

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
 
E

ECathell

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.
 
C

Cor Ligthert

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
 

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