How to do a database search using a textbox and a search button in visual studio VB Code - vb.net

I want to type something in my txtSearchCriteria textbox and when I hit the Perform Search button it displays all the records it finds into the other textboxes I have in my form. I am using a localdb file with a dataset in Visual Studio 2012. So far I have everything working except the search button. Here is my entire code in my project.
'Import Namespaces for SQL
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1 Dim objCurrencyManager As CurrencyManager
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Loads data into the 'CustomersDataSet.Customers' table.
Me.CustomersTableAdapter.Fill(Me.CustomersDataSet.Customers)
'Controls Record Number Movement
objCurrencyManager = CType(Me.BindingContext(CustomersBindingSource), CurrencyManager)
'Display the current record number
Position()
'Display a tool tip at the bottom of screen
lblToolStripLabel1.Text = "Ready To Compute Data"
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
'Used try catch block to validate all fields to ensure proper use of form and to display error if required fields are missing user imput
Try
Me.Validate()
Me.CustomersBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.CustomersDataSet)
lblToolStripLabel1.Text = "Record Saved" 'Display that record has been saved
Catch ex As Exception
MsgBox(ex.Message)
End Try
Position()
End Sub
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
'code to update an existing record and display that redord was updated successfully
Try
Me.Validate()
Me.CustomersBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.CustomersDataSet)
lblToolStripLabel1.Text = "Record Updated" 'display record updated
Catch ex As Exception
MsgBox(ex.Message)
End Try
Position()
End Sub
Private Sub dtnDelete_Click(sender As Object, e As EventArgs) Handles dtnDelete.Click
' Removes the current record and displays the new position in the database
Me.CustomersBindingSource.RemoveCurrent()
Position()
End Sub
Private Sub btnMoveFirst_Click(sender As Object, e As EventArgs) Handles btnMoveFirst.Click
'Moves the current record to the begining of the dataset. The First Record
Me.CustomersBindingSource.MoveFirst()
Position()
End Sub
Private Sub btnMovePrevious_Click(sender As Object, e As EventArgs) Handles btnMovePrevious.Click
'moves to the previous record and displays the position
Me.CustomersBindingSource.MovePrevious()
Position()
End Sub
Private Sub btnMoveNext_Click(sender As Object, e As EventArgs) Handles btnMoveNext.Click
'Moves to the next record in the dataset
Me.CustomersBindingSource.MoveNext()
Position()
End Sub
Private Sub btnMoveLast_Click(sender As Object, e As EventArgs) Handles btnMoveLast.Click
'Moves to the last Record in the Dataset
Me.CustomersBindingSource.MoveLast()
Position()
End Sub
Private Sub Position()
'This code displays the positio of the current record the user is viewing and shows it out of the number of recors that are held in the dataset
txtRecordPosition.Text = objCurrencyManager.Position + 1 & " Of " & objCurrencyManager.Count()
End Sub
Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles btnNew.Click
'Clears the form for user imput, Created a new record and adds the current date to CustomerDate field and adds the next CustomerNumber available.
Me.CustomersBindingSource.AddNew()
txtCustomerNumber.Text = Me.CustomersBindingSource.Count + 1
txtCustomerDate.Text = Date.Today.ToShortDateString
End Sub
Private Sub btnPerformSort_Click(sender As Object, e As EventArgs) Handles btnPerformSort.Click
'Code to Sort by different field within the dataset
Select Case cboField.SelectedIndex
Case 0 'CustomerNumber
CustomersBindingSource.Sort = "CustomerNumber"
Case 1 'First Name
CustomersBindingSource.Sort = "FirstName"
Case 2 'Last Name
CustomersBindingSource.Sort = "LastName"
Case 3 'City
CustomersBindingSource.Sort = "City"
Case 4 'Province
CustomersBindingSource.Sort = "Province"
End Select
btnMoveFirst_Click(Nothing, Nothing)
lblToolStripLabel1.Text = "Records Sorted"
End Sub
Private Sub btnPerformSearch_Click(sender As Object, e As EventArgs) Handles btnPerformSearch.Click
'This will take the user imput from the txtSearchCriteria textbox and return all records that have the criteria
End Sub
End Class

Here is a sample to query database assuming you are connecting to sql server.
Private Sub BindGrid()
Dim constr As String = ConfigurationManager.ConnectionStrings("<your connection string name>").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand()
cmd.CommandText = "SELECT ContactName, City, Country FROM Customers WHERE ContactName LIKE '%' + #ContactName + '%'"
cmd.Connection = con
cmd.Parameters.AddWithValue("#ContactName", txtSearch.Text.Trim())
Dim dt As New DataTable()
Using sda As New SqlDataAdapter(cmd)
sda.Fill(dt)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Using
End Using
End Using
End Sub
To explain at first step we are declaring connection string then we are opening SqlConnection object and then we are querying the database table ContactName based on the txtSearch value inputted.Next we are filling the dataset and binding the grid.
I suggest you to read this msdn article on sql connection.

Related

Datagridview form always open with row in selected state

I have two forms: frmMain and frmProduct
In the main form frmMain, I have a Datagridview1 and the buttons: tsbInsert_click, tsbAlter_click and tsbConsult_click.
When I open the main form frmMain, there is still no row in the Datagridview1 and when I click on any of the tsbAlter_click or tsbConsult_click buttons, the code displays the message "Select a product", which is correct , because there is still no product created in the register.
When I click on the tsbInsert_click button, the code opens the frmProduct to insert a record in the register and then returns to the main form frmMain, which, in the line of the Datagridview1, shows the record that was included.
The problem is that when I include a record in the register, the Datagridview1 ALWAYS returns with the state of the selected row, and the condition If DGProducts.CurrentRow Is Nothing is never true again ... so when I click on tsbAlter_click or tsbConsult_click, the "Select a product" message no longer appears and it should have since I didn't select any rows in Datagridview1.
frmMain Code:
Public Class frmMain
Private Sub tsbInsert_Click(sender As Object, e As EventArgs) Handles tsbInsert.Click
Using frm As New frmProduct("Insert", "")
frm.ShowDialog()
If frm.txtPrdCod.Text <> "" Then
DGProducts.Rows.Add(Campo(0), Campo(1), Campo(2))
End If
End Using
End Sub
Private Sub tsbAlter_Click(sender As Object, e As EventArgs) Handles tsbAlter.Click
If DGProducts.CurrentRow Is Nothing Then
MessageBox.Show("Select a product")
Exit Sub
End If
Dim codProd As String = DGProducts.CurrentRows.Cells("prdCod").Value
Using frm As New frmProduct("Alter", codProd)
frm.ShowDialog()
With DGProducts.Rows(DGProds.CurrentCell.RowIndex)
.Cells("prdCod").Value = Field(0)
.Cells("prdName").Value = Field(1)
.Cells("prdManufac").Value = Field(2)
End With
End Using
End Sub
Private Sub DGProducts_CellEnter(sender As Object, e As DataGridViewCellEventArgs) Handles DGProducts.CellEnter
With DGProducts.Rows(DGProducts.CurrentCell.RowIndex)
Campo(0) = .Cells("prdCod").Value
Campo(1) = .Cells("prdName").Value
Campo(2) = .Cells("prdManufac").Value
End With
End Sub
End Class
frmProduct code:
Public Class frmProduct
Private Sub frmProduct_Load(sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If _operation = "Alter" Then
txtPrdCod.Text = Campo(0)
txtPrdName.Text = Campo(1)
txtManufct.Text = Campo(2)
Else
txtPrdCod.Text = ""
txtPrdName.Text = ""
txtManufct.Text = ""
Enf If
End Sub
Dim _operation As String
Dim _codProd As String
Public Sub New(operation As String, codProd As String)
InitializeComponent()
Select Case operation
Case "Insert"
_operation = operation
Case "Alter"
_operation = operation
_codProd = codProd
Case Else
MessageBox.Show("Invalid option!")
Close()
End Select
End Sub
Private Sub tsbRecord_Click(sender As Object, e As EventArgs) Handles tsbRecord.Click
Field(0) = txtPrdCod.Text
Field(1) = txtPrdName.Text
Field(2) = txtManufct.Text
Me.Close()
End Sub
End Class
How do I make it so that when I add a product to the register, Datagridview1 DOES NOT return with the line in the SELECTED state and the message "Select a product" appears when I don't select a line from Datagridview1?
The command DGProducts.Rows.Add(Field(0), Field(1), Field(2)), automatically changes the value DGProducts.SelectedRows.Count to 1, so I set DGProducts.CurrentCell = Nothing after command DGProducts.Rows.Add in code:
Private Sub tsbInsert_Click(sender As Object, e As EventArgs) Handles tsbInsert.Click
Using frm As New frmProduct("Insert", "")
frm.ShowDialog()
If frm.txtPrdCod.Text <> "" Then
DGProducts.Rows.Add(Campo(0), Campo(1), Campo(2))
DGProducts.CurrentCell = Nothing
End If
End Using
End Sub

Coding navigation buttons using vb.net 2012

My main goal is to code navigation buttons to control records of a vb.net project connected with Microsoft Access Database. The four buttons included are First record, Last record, Next record and Previous record. The errors showing say "ShowData" and "CurrentRow" are not declared.
CurrentRow shows the following error correction options:
ShowData shows the following error correction options:
'to navigate to first record
Private Sub btn_first_Click(sender As Object, e As EventArgs) Handles btn_first.Click
CurrentRow = 0
ShowData(CurrentRow)
End Sub
'to navigate to last record
Private Sub btn_last_Click(sender As Object, e As EventArgs) Handles btn_last.Click
CurrentRow = Dst.Tables("Purchases_file").Rows.Count - 1
ShowData(CurrentRow)
End Sub
'to navigate to previous record
Private Sub btn_previous_Click(sender As Object, e As EventArgs) Handles btn_previous.Click
If CurrentRow <> 0 Then
CurrentRow -= 1
ShowData(CurrentRow)
Else
MsgBox("First record is reached!", MsgBoxStyle.Exclamation)
End If
End Sub
'to navigate to next record
Private Sub btn_next_Click(sender As Object, e As EventArgs) Handles btn_next.Click
If CurrentRow = Dst.Tables("Purchases_file").Rows.Count - 1 Then
MsgBox("Last record is reached!", MsgBoxStyle.Exclamation)
Else
CurrentRow += 1
ShowData(CurrentRow)
End If
End Sub
According to the error you get you did not declare CurrentRow before using it in the methods. Put
Private CurrentRow as Integer
in your class. ShowData calls the method to retrieve the data from the database. It should be something like this:
Private Sub ShowData(byval row as integer)
'code to get the data in the database
End Function
If the variable and method exist in another class of the project you need to reference them via their class. For example:
ClassName.CurrentRow
ClassName.ShowData(CurrentRow)
My Roasters table has field names of Name, City, and Zip. They are bond to labels on the BindingNavigator form and also a DataGridView. The BindingNavigator provides the buttons you describe.
Imports System.Data.SqlClient
Public Class BindingNavigator
Dim WithEvents bindSrc As New BindingSource
Private Sub BindingNavigator_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt = LoadData1()
bindSrc.DataSource = dt
BindingNavigator1.BindingSource = bindSrc
lblItem.DataBindings.Add(New Binding("Text", bindSrc, "Name"))
lblDescription.DataBindings.Add(New Binding("Text", bindSrc, "City"))
lblPrice.DataBindings.Add(New Binding("Text", bindSrc, "Zip"))
DataGridView1.DataSource = bindSrc
End Sub
Private Function LoadData1() As DataTable
Dim dt As New DataTable
Using conn As New SqlConnection(My.Settings.CoffeeConnection),
cmd As New SqlCommand("Select Top 10 * From Roasters;", conn)
conn.Open()
dt.Load(cmd.ExecuteReader)
End Using
Return dt
End Function
End Class

vb.net How to overwrite the text in a combobox after the dropdownclosed event

I created this piece of code to illustrate the idea, it uses a combobox and a textbox
Private Sub ComboBox2_DropDown(sender As Object, e As EventArgs) Handles ComboBox2.DropDown
ComboBox2.Items.Clear()
ComboBox2.Items.Add("0001 | Apple")
ComboBox2.Items.Add("0002 | Pear")
ComboBox2.Items.Add("0003 | Banana")
ComboBox2.Items.Add("0004 | Pineapple")
ComboBox2.Items.Add("0005 | Cherry")
End Sub
Private Sub ComboBox2_DropDownClosed(sender As Object, e As EventArgs) Handles ComboBox2.DropDownClosed
Dim selecteditem As String = ComboBox2.Items(ComboBox2.SelectedIndex)
ComboBox2.Text = Strings.Left(selecteditem,4)
TextBox2.Text = Strings.Left(selecteditem,4)
End Sub
When I select an item from the combobox what happens is that the combobox keeps showing the whole string while the textbox only shows the first 4 characters.
How can I overwrite the combobox text after I close the combobox?
* edit *
I tried a combo of the solutions but ran into a problem because the data was bound to a datasource so it's not possible to change the item.
This is the new code:
Private Sub ComboBox2_DropDown(sender As Object, e As EventArgs) Handles ComboBox2.DropDown
SQL.ExecQuery($"select ID, Name, RTRIM(ID + ' | ' + Name) as SingleColumn from GCCTEST.dbo.tblFruit")
ComboBox2.DataSource = SQL.DBDT
ComboBox2.DisplayMember = "SingleColumn"
End Sub
Private Sub ComboBox2_DropDownClosed(sender As Object, e As EventArgs) Handles ComboBox2.DropDownClosed
ComboBox2.DisplayMember = "ID"
ComboBox2.SelectedIndex = 0
End Sub
Now I only need to have the 0 be the index I chose...
The following should work.
If not necessary, don't populate the combobox on every drop-down, instead call the FillComboBox-method when loading the Form.
Private Sub FillComboBox()
SQL.ExecQuery($"select ID, Name, RTRIM(ID + ' | ' + Name) as SingleColumn from GCCTEST.dbo.tblFruit")
ComboBox2.DataSource = SQL.DBDT
ComboBox2.DisplayMember = "ID"
ComboBox2.ValueMember = "ID"
End Sub
Private Sub ComboBox2_DropDown(sender As Object, e As EventArgs) Handles ComboBox2.DropDown
Me.ComboBox2.DisplayMember = "SingleColumn"
End Sub
Private Sub ComboBox2_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox2.SelectionChangeCommitted
Dim selV As Object = Me.ComboBox2.SelectedValue
Me.TextBox2.Text = CStr(selV)
Me.ComboBox2.DisplayMember = "ID"
'Set the current value again, otherwise the combobox will always display the first item
Me.ComboBox2.SelectedValue = selV
End Sub
I used a few properties and .net String.SubString method instead of the old vb6 Strings.Left.
Private Sub ComboBox1_DropDownClosed(sender As Object, e As EventArgs) Handles ComboBox1.DropDownClosed
Dim SelectedString As String = ComboBox1.SelectedItem.ToString
Dim ChangedString As String = SelectedString.Substring(0, 4)
Dim index As Integer = ComboBox1.SelectedIndex
ComboBox1.Items(index) = ChangedString
End Sub
You can fill your combo box one by one to avoid binding problems as follows...
Private Sub ComboBox1_DropDown(sender As Object, e As EventArgs) Handles ComboBox1.DropDown
Using cn As New SqlConnection("Your connection string")
Using cmd As New SqlCommand("Select ID, Name From tblFruit;", cn)
cn.Open()
Using dr As SqlDataReader = cmd.ExecuteReader
ComboBox1.BeginUpdate()
While dr.Read
ComboBox1.Items.Add(dr(0).ToString & " | " & dr(1).ToString)
End While
ComboBox1.EndUpdate()
End Using
End Using
End Using
You can solve this graphical problem putting a Label in your Form and moving it over your ComboBox.
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.Label1.AutoSize = False
Me.Label1.BackColor = Me.ComboBox1.BackColor
Me.Label1.Location = New Point(Me.ComboBox1.Location.X + 1, Me.ComboBox1.Location.Y + 1)
Me.Label1.Size = New Size(Me.ComboBox1.Width - 18, Me.ComboBox1.Height - 2)
Me.ComboBox1.Items.Add("0001 | Apple")
Me.ComboBox1.Items.Add("0002 | Pear")
Me.ComboBox1.Items.Add("0003 | Banana")
Me.ComboBox1.Items.Add("0004 | Pineapple")
Me.ComboBox1.Items.Add("0005 | Cherry")
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Me.Label1.Text = Trim(Me.ComboBox1.SelectedItem.Split("|")(0))
End Sub
Please note that:
you can populate your ComboBox a single time during Form load event
you can use SelectedIndexChanged instead of DropDown and DropDownClosed event
if this is not a graphical problem please give a look at DisplayMember and ValueMember properties

Datagridview and access database only updates after clicking a different row

I am reading an access database and populating the info in datagridview. My form has a DGV, and 3 buttons.
Button one copies the selected row to a datetimepicker control.
Button two copied the updated datetimepicker value back to the DVG
Button three does an update (writes the info back to the database).
My issue is that the info only gets updated in the database if I select a different row before hitting button three. I am not getting any error message in either case.
Below is my code. The database only has 2 columns (name and DOB - which is date/time).
Public Class Form1
Dim dbConn As New OleDb.OleDbConnection
Dim sDataset As New DataSet
Dim sDataAdapter As OleDb.OleDbDataAdapter
Dim sql As String
Dim iTotalRows As Integer
Dim sShipTypeFilter As String
Dim sBuildingFilter As String
Dim sCustSuppFilter As String
Dim sStatusFilter As String
Dim sDayFilter As String
Dim dv As New DataView
Sub myDBConn()
dbConn.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\terry\Documents\Database1.accdb"
Debug.Print("Start:" & DateAndTime.Now.ToString)
dbConn.Open()
sql = "select * from TableX"
sDataAdapter = New OleDb.OleDbDataAdapter(Sql, dbConn)
sDataAdapter.Fill(sDataset, "MyTable")
dbConn.Close()
iTotalRows = sDataset.Tables("MyTable").Rows.Count
Debug.Print("Rows from Access:" & iTotalRows)
Debug.Print("End:" & DateAndTime.Now.ToString)
End Sub
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Call myDBConn()
Debug.Print("DVG1 row count before binding:" & DataGridView1.Rows.Count)
'dv = New DataView(sDataset.Tables(0), "Shipment = 'Regular' and Building = 'CSE'", "Company DESC", DataViewRowState.CurrentRows)
dv = sDataset.Tables(0).DefaultView
Debug.Print("DataView count:" & dv.Count)
DataGridView1.DataSource = dv
Debug.Print("DVG1 Rows:" & DataGridView1.Rows.Count)
DataGridView1.Columns("DOB").DefaultCellStyle.Format = "hh:mm tt"
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
dtp1.Value = DataGridView1.SelectedRows(0).Cells("DOB").Value
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
DataGridView1.SelectedRows(0).Cells("DOB").Value = dtp1.Value
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
Debug.Print("switched row")
Me.Visible = False
Dim sqlcb As New OleDb.OleDbCommandBuilder(sDataAdapter)
sDataAdapter.Update(sDataset.Tables("MyTable"))
Me.Close()
End Sub
End Class
Before updating you need to Endedit. This means you need to add Endedit for the datagridview.
So this will be your code:
Debug.Print("switched row")
Me.Visible = False
Dim sqlcb As New OleDb.OleDbCommandBuilder(sDataAdapter)
Datagridview1.EndEdit()
sDataAdapter.Update(sDataset.Tables("MyTable"))
Me.Close()
EDIT1:
Dim dt As New DataTable
dbConn.Open()
sDataset.Tables.Add(dt)
sDataAdapter = New OleDbDataAdapter("Select * from TableX", dbConn)
sDataAdapter.Update(dt)
dbConn.Close()
I figured it out - thanks Stef for putting me on the right track.
My DGV is only updated programmatically (not by user edits) so I updated the code for button 2 to set the editmode, begin editing, update the selected DVG row and end editing:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
DataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically
DataGridView1.BeginEdit(True)
DataGridView1.SelectedRows(0).Cells("DOB").Value = dtp1.Value
DataGridView1.EndEdit()
End Sub
After doing this modification - my datadapter.update command works!!

How to save data within the Application?

I'm creating a basic To-do program. I want to save the list of tasks the user creates. Now I've tried with saving them as text files but I don't want it that way. I want it so that I can save the tasks the user creates within the program rather than external text files and then retrieve those saved files and display them in a text file.
Essentially I need a way to save data without needing to rely on databases.
A good example is GeeTeeDee. It seems to be saving its files and data etc. in the program within rather than external text file.(I'm assuming this because I can't seem find them. I could be wrong)
Update
I was doing a bit of searching can came across this: Click here!!!
But the problem is that I'm confused as to how this works. Is someone able to clear things for me? It would be GREATLY appreciated as it's exactly what I'm looking for.
The "code project" example saves the data at an external file with extension [*.brd] .
You can Use XmlSerializer to save and load your data from external xml file with extension xml,brd or anything else.
Try the code below, add into a form1 three buttons (Button1,Button2,Button3) and a DataGridView1, paste the code and Run.
press button "add data dynamically" or/and add,edit,delete row directlly from DataGridView1.
press Save data.
close and run programm
press Load data.
Imports System.Xml.Serialization
Imports System.IO
Class Form1
Dim ds As DataSet
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Button1.Text = "Load Data"
Button2.Text = "add data dynamically"
Button3.Text = "Save Data"
'Create Dataset
ds = CreateDataset()
'Set DataGridView1
DataGridView1.DataSource = ds.Tables("Person")
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
LoadFromXMLfile("c:\temp\persons.xml")
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
AddDataToDataSetDynamically(ds)
End Sub
Private Sub Button3_Click_1(sender As System.Object, e As System.EventArgs) Handles Button3.Click
SaveToXMLFile("c:\temp\persons.xml", ds)
End Sub
Private Function CreateDataset() As DataSet
Dim dataset1 As New DataSet("Persons")
Dim table1 As New DataTable("Person")
table1.Columns.Add("Id")
table1.Columns.Add("FName")
table1.Columns.Add("Age")
'...
dataset1.Tables.Add(table1)
Return dataset1
End Function
Private Sub AddDataToDataSetDynamically(d As DataSet)
d.Tables("Person").Rows.Add(1, "Andrew", "46")
d.Tables("Person").Rows.Add(2, "Nicky", "43")
d.Tables("Person").Rows.Add(3, "Helen", "15")
End Sub
Private Sub SaveToXMLFile(filename As String, d As DataSet)
Dim ser As XmlSerializer = New XmlSerializer(GetType(DataSet))
Dim writer As TextWriter = New StreamWriter(filename)
ser.Serialize(writer, d)
writer.Close()
End Sub
Private Sub LoadFromXMLfile(filename As String)
If System.IO.File.Exists(filename) Then
Dim xmlSerializer As XmlSerializer = New XmlSerializer(ds.GetType)
Dim readStream As FileStream = New FileStream(filename, FileMode.Open)
ds = CType(xmlSerializer.Deserialize(readStream), DataSet)
readStream.Close()
DataGridView1.DataSource = ds.Tables("Person")
Else
MsgBox("file not found! add data and press save button first.", MsgBoxStyle.Exclamation, "")
End If
End Sub
End Class
add that code to form1 and get the data to a textbox (add button4,textbox1)
Private Function PrintRows(dataSet As DataSet) As String
Dim s As String = ""
Dim thisTable As DataTable
For Each thisTable In dataSet.Tables
Dim row As DataRow
For Each row In thisTable.Rows
Dim column As DataColumn
For Each column In thisTable.Columns
s &= row(column) & " "
Next column
s &= vbCrLf
Next row
Next thisTable
Return s
End Function
Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
TextBox1.Text = PrintRows(ds)
End Sub