|
-
Apr 3rd, 2004, 12:37 AM
#1
Thread Starter
Member
Using the DataSet Namespace
Hi Good People
I'm a newbie in .NET and looking for help in getting my small project started. I'm using textbox controls on a form to capture some details and create an in-memory cache of my records before I eventually save the collection of records created to an XML file.
The form will also have commandbuttons to allow me to insert a new record, go next record, got first record, etc (similar to the methods of a recordset). Each time I navigate to a different record, the textbox contents on the form should reflect the change. A SUBMIT command button should write all these records in memory to an XML file.
How can I allow navigation from one record to another, as well as record changes just the way we do in VB6.0 using the Recordset object? I couldn't see any way of doing it. Which object(s) in the Dataset namespace can I use?
PLEASE HELP!! Here's a simplified version of my code snippet:
Public Shared Sub SaveRecordsInDatatable
CreateDataSet
'Add data to table
Dim i As Integer
For i = 0 To 10
dr = dt.NewRow
dr("name") = txtName.text
dr("surname") = txtSurname.text
dr("DOB") = txtDOB.text
dt.Rows.Add(dr)
Next i
End Sub
Public Shared Sub CreateDataset()
Dim dt As New DataTable
Dim dc As DataColumn
Dim dr As DataRow
' Create new DataRow objects
'create name column
dc = New DataColumn
dc.DataType = System.Type.GetType("System.String")
dc.ColumnName = "name"
dt.Columns.Add(dc)
' Create surname column.
dc = New DataColumn
dc.DataType = Type.GetType("System.String")
dc.ColumnName = "surname"
dt.Columns.Add(dc)
' Create Date Of Birth column.
dc = New DataColumn
dc.DataType = Type.GetType("System.String")
dc.ColumnName = "DOB"
dt.Columns.Add(dc)
End Sub
P/S - some code snippets would help. Thanks.
-
Apr 3rd, 2004, 03:55 AM
#2
Sleep mode
-
Apr 3rd, 2004, 04:41 AM
#3
PowerPoster
Hi,
Looking at your code I think there are problems when you declare private objects within a Shared Sub. I also don't think that having declared the objects as private within a sub you can refer to them outside of that sub.
I personally have found it easier to use a DataTable to update tables, rather than a Dataset. I have also found navigation easier in a DataTable. But remember that you can use only one table in a DataTable and if you use an SQL query referring to more than one table, you have to use a DataSet.
Having relooked at your code I notice that you have not actually used a DataSet, even though you name your Sub that way!!!
I have to go and play tennis now. If you have not received more helpful responses I will revert leter.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Apr 3rd, 2004, 05:49 AM
#4
Thread Starter
Member
Thanks taxes. You are right; with the way I wrote my code, it wouldn't work. I was trying to quickly explain my problem in a way that could be understod, but in the process I got sloppy.
I will be happy if you explain to me further.
-
Apr 3rd, 2004, 07:39 AM
#5
PowerPoster
Hi,
"How can I allow navigation from one record to another, as well as record changes just the way we do in VB6.0 using the Recordset object? I couldn't see any way of doing it. Which object(s) in the Dataset namespace can I use? "
I'll stick to this part of your posting - I'm not sure I quite understand your first part
Assuming your are accessing a single table on an Access database:
In the general section of your form;
Dim cn1 as New Oledb.OledbConnection
Dim da1 as New Oledb.OledbDataAdapter
Dim cb1 as New Oledb.OledbCommandBuilder
Dim dt1 as New DataTable
Dim iRowPosition as Integer=0
Dim rw1 as DataRow
Dim iCount as Integer
In the form Load event:
cn1.ConnectionString="Provider=Microsoft.Jet.oledb.4.0;Data Source=pathto database
cn1.Open
da1=New Oledb.OledbDataAdapter("Select * from tablename",cn1)
cb1= New Oledb.OledbCommandBuilder(da1)
da1.Fill(dt1)
You can now navigate through the DataTable using
rw1.Rows(iCount)
e.g. txt1.text=dt1.Rows((iCount)("FieldName").ToString()
Se how you get on with that (As I have spent so much time at tennis and on the internet, I now have to do penance in the garden)
EDIT: If you are familiar with Imports then you can cut down on the use of "Oledb"
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Apr 3rd, 2004, 08:19 AM
#6
Thread Starter
Member
Hi
Thanks for your advice. i'm afraid it's a bit more complicated than that. I'm not accessing any database but using XML, and I don't know how to bind my controls to the datatable.
Here's the process:
I'll create an in-memory blank DataTable first, add rows to the table, then send the result for processing via a message queue. The final message will consist of one message header and several transaction details (in this case the rows in my Datatable).
Before I send this message for processing, I need to be able to insert new rows, change any row details, delete any unwanted rows, etc (just like I do if I was using the recordset object).
Any advice? Thanks.
-
Apr 3rd, 2004, 09:59 AM
#7
PowerPoster
Hi,
Ah. Then as far as adding new rows, your code should work - provided you make the declaration amendments previously mentioned. Have you tried it? If so what errors?
You can navigate the table exactly as I posted and make the data visible in textBoxes etc by
txt1.text=dt.Rows(x)("Surname").ToString
x being the number of the row you want to view.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Apr 3rd, 2004, 08:28 PM
#8
Thread Starter
Member
Thanks so much Taxes. It worked!!
Now all I have to do is see if I can add other functionality.
Cheers.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|