Hello, this is some code I use to move forward and backwards throughs records. I running vb.net v2002 connecting to a SQL2000 database using intergrated security.
'Declare objects, use intergrated security to access Northwind db
Dim cnn1 As SqlConnection = _
New SqlConnection _
("Data Source=(local);" & "Integrated Security=SSPI;" & _
"Initial Catalog=northwind")
Dim myAdapter As SqlDataAdapter = New SqlDataAdapter( _
"SELECT * FROM customers ", cnn1)
Dim myDS As DataSet
Dim myDV As DataView
Dim myCurrencyManager As CurrencyManager
Dim relCustOrder As DataRelation
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
'Fill the DataSet and bind the fields...
FillDataSetAndView()
BindFields()
'Show the current record position...
ShowPosition()
Catch er As System.Exception
MsgBox("Error: " & er.Source & ": " & er.Message, _
MsgBoxStyle.Exclamation, "Unable to Connect to Customers DataTable - check SQL Network Connection")
'Write Exception message to the event log...
Dim log As EventLog = New EventLog()
log.Log = "NWind_Application"
log.Source = Me.Text
log.WriteEntry(er.ToString, EventLogEntryType.Error)
log.Close()
'Display message in StatusBar...
StatusBar1.Text = er.ToString
Finally
End Try
End Sub
Private Sub FillDataSetAndView()
'Initialize a new instance of the DataSet object...
myDS = New DataSet()
'Fill the DataSet object with data...
myAdapter.Fill(myDS, "customers")
'Set the DataView object to the DataSet object...
myDV = New DataView(myDS.Tables("Customers"))
'Set our CurrencyManager object to the DataView object...
myCurrencyManager = CType(Me.BindingContext(myDV), CurrencyManager)
End Sub
Private Sub BindFields()
'Clear any previous bindings...
txtCustomerID.DataBindings.Clear()
txtCompanyName.DataBindings.Clear()
txtContactName.DataBindings.Clear()
txtContactTitle.DataBindings.Clear()
txtAddress.DataBindings.Clear()
txtCity.DataBindings.Clear()
txtRegion.DataBindings.Clear()
txtPostalcode.DataBindings.Clear()
txtCountry.DataBindings.Clear()
txtPhone.DataBindings.Clear()
txtFax.DataBindings.Clear()
'Add new bindings to the DataView object...
txtCustomerID.DataBindings.Add("Text", myDV, "customerID")
txtCompanyName.DataBindings.Add("Text", myDV, "companyname")
txtContactName.DataBindings.Add("Text", myDV, "contactname")
txtContactTitle.DataBindings.Add("Text", myDV, "contacttitle")
txtAddress.DataBindings.Add("Text", myDV, "Address")
txtCity.DataBindings.Add("Text", myDV, "City")
txtRegion.DataBindings.Add("Text", myDV, "Region")
txtPostalcode.DataBindings.Add("Text", myDV, "Postalcode")
txtCountry.DataBindings.Add("Text", myDV, "Country")
txtPhone.DataBindings.Add("Text", myDV, "Phone")
txtFax.DataBindings.Add("Text", myDV, "Fax")
'Display a ready status...
StatusBar1.Text = "Ready"
End Sub
Private Sub ShowPosition()
'Display the current position and the number of records
txtRecordPosition.Text = myCurrencyManager.Position + 1 & _
" of " & myCurrencyManager.Count()
End Sub
Private Sub bntMoveFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntMoveFirst.Click
'Set the record position of the first record...
myCurrencyManager.Position = 0
'Show the current record position...
ShowPosition()
End Sub
Private Sub btnMovePrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMovePrevious.Click
'Move to the previous record...
myCurrencyManager.Position -= 1
'Show the current record position...
ShowPosition()
End Sub
Private Sub btnMoveNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveNext.Click
'Move to the next record...
myCurrencyManager.Position += 1
'Show the current record position...
ShowPosition()
End Sub
Private Sub btnMoveLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveLast.Click
'Set the record position to the last record...
myCurrencyManager.Position = myCurrencyManager.Count - 1
'Show the current record position...
ShowPosition()
End Sub
Is this of any help to you?? I'm sure there are other ways of doing this, but this works for me.
Cheers! wearytraveller