|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Question on updating datahelp. 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 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. Show quoteHide quote "Gary Paris" <t***@yada.com> wrote in message news:OJ4cgiYOFHA.3388@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 > 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 Show quoteHide quote "Stephany Young" <noone@localhost> wrote in message news:eyjpx0YOFHA.1176@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" <t***@yada.com> wrote in message > news:OJ4cgiYOFHA.3388@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 >> > > 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 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 Show quoteHide quote "Cor Ligthert" <notmyfirstn***@planet.nl> wrote in message news:u9B5DwaOFHA.244@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 > Gary,
See bellow Show quoteHide quote > An object exist in VBNet as long as there is any reference to it. Therefore > 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 >> /// 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 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. -- Show quoteHide quote--Eric Cathell, MCSA "Cor Ligthert" <notmyfirstn***@planet.nl> wrote in message news:OwFCLTnOFHA.1172@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 > > 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
?syntax to recognize code in a 2nd file
Scripting Runtime ARRAYLIST ADDING A CLASS sockets O.T.:Shameless Plug to get Free Software(for me & for you too) How to provide hint on sub params? Why "Application has generated an exception" error and not a good error message? How to check for scroll bars in web browser object? Transpose datagrid row to corresponding label text Inherited User Control + Subclassed components + Dissapear after build!!!!!!!!!!!!!!! |
|||||||||||||||||||||||