I wanted some help with vb.net code to use the previous, next and continue button. The program reads a text file and displays the info in textboxes on the form. It will then insert a new record into the sql database. It then moves to the next line on the text file. The problem is that the process is fast so the user does not get to see the details on the form before it moves to the next line. I want to be able to read one line, save to database and then pause to give the user time to read the information in the textboxes. They can then click the next button to read the next line in the text file and save to database. But I also wanted the functionality of being able to move to the next or previous record at anytime. Any ideas how to go about this?
What it sounds like you want is a dataset to use, or threaded process to upload.
The dataset being the easier of the two.
Private Function GetDataFromFile(ByVal sPath as String) As List(Of List( OfString)) Try Dim sr As New StreamReader(sPath) Dim lstStrings As New List(Of List(Of String)) Do While sr.Peek <> -1 'This will split on the , characters. lstStrings.Add(sr.ReadLine.Split(",").ToList) End While Return lstStrings Catch ex As Exception MsgBox(ex.ToString) Return New List(Of String) End Try End Function Private Function FillDataSet(ByVal lstStrings As List(Of List(Of String))) As DataSet Try Dim da As New OleDBDataAdapter("SELECT * FROM MyTable",myCon) 'myCon is an OleDBConnection Dim ds As New DataSet da.Fill(ds,"myTable") If Not IsNothing(ds.Tables("myTable")) Then For i = 0 to lstStrings.Count - 1 Dim dr As DataRow = ds.Tables("myTable").Rows.Add() 'For columns that do not allow NULL dr("Column1") = IIF(IsDBNull(lstStrings(i)(0)),TrueValue,FalseValue) '^ 1st column of the n'th list. 'For columns that allow NULL dr("Column2") = lstStrings(i)(1) '2nd column of the n'th list. Next Return ds Else Return New DataSet End If Catch ex As Exception MsgBox(ex.ToString) Return New DataSet End Try End Function
Now, with that being said, you will have a dataset containg all of the values you wanted to pass in and update.
To view them, just step through:
Dim iCurrentRecord As Integer = 0 Private Sub NextButton_Click(ByVal Sender As Object, ByVal e As EventArgs) Handles NextButton.Click Try If iCurrentRecord + 1 <= ds.Tables("myTable").Rows.Count - 1 Then LoadCurrent(iCurrentRecord + 1) iCurrentRecord += 1 End If Catch ex As Exception MsgBox(ex.ToString) End Try End Sub Private Sub LoadCurrent(ByVal dr As DataRow) Try 'Load your information controls here. 'Example: NameTextBox.Text = dr("Name") Catch ex As Exception MsgBox(ex.Tostring) End Try End Sub
To update - issue an update statement.
da.UpdateCommand = New OleDBCommandBuilder(da).GetUpdateCommand da.Update(ds) 'ds will be the value returned from FillDataSet.