VB 2500 express with MySQL thru MySQL Connector Net

I new to VB 2500 express with MySQL. I pick up some idea from Mike Hillyer's tutorial at vbmysql.com. I desperatelly need some direction here.

I have a table [emp] in MySQL with the following:
CREATE TABLE `emp` (
`empID` int(4) unsigned NOT NULL auto_increment,
`firstname` varchar(50) NOT NULL default '',
`lastname` varchar(50) NOT NULL default '',
`status` enum('A','D') NOT NULL default 'A',
PRIMARY KEY (`empID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;


INSERT INTO `emp` VALUES (1, 'Richard', 'Sam', 'A');
INSERT INTO `emp` VALUES (2, 'Record', 'Two', 'A');
INSERT INTO `emp` VALUES (3, 'Rec', 'Three', 'A');

I have a form with a DataGridView on top, with the following
label [First Name] : text box [txt_FName]
label [Last Name] : text box [txt_LName]
label [Status] : radio button [rad_Active] radio button [rad_Disable]
Binding Navigator [Emp_BN]
Binding Source [Emp_BS]
2 buttons [Save, Cancel]

My code is as follow:

Imports MySql.Data.MySqlClient
Imports System.Data

Public Class Form1

Private Sub bnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnCancel.Click

Me.Close()

End Sub

Private Sub bnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnSave.Click

Me.Close()

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim link As String

Dim conn As New MySqlConnection

Dim myCommand As New MySqlCommand

Dim myAdapter As New MySqlDataAdapter

Dim myData As New DataTable

Dim SQL As String

link = "server= serverIP; user id=userID; password=passwd; database=db"

conn.ConnectionString = link

SQL = "SELECT * FROM emp"

myCommand.Connection = conn

Try

conn.Open()

Try

myCommand.Connection = conn

myCommand.CommandText = SQL

myAdapter.SelectCommand = myCommand

myAdapter.Fill(myData)

dgEmp.DataSource = myData

Emp_BS.DataSource = myData

Emp_BN.BindingSource = Emp_BS

Catch myerror As MySqlException

MsgBox("There was an error reading from the database : " & myerror.Message)

End Try

Catch myerror As MySqlException

MessageBox.Show("Error connecting to the database")

Finally

If conn.State <> ConnectionState.Closed Then conn.Close()

End Try

End Sub

End Class

================

I can read and display the 3 records in the datagrid. But how to display the record in the text boxes and radio buttons. Also, now the Binding Navigator is not tied to the DataGrid. When I use the next, previous button, the record number changes but not on the datagrid and vice versa.

I am desperate for a solution. I can upload the test project if anyone care to take a look. I need an answer ASAP for a project but I am new and clueless. Will appreciate any guidance.



Answer this question

VB 2500 express with MySQL thru MySQL Connector Net

  • prog.gabi


    Your code should be:

    Emp_BS.DataSource = myData

    dgEmp.DataSource = Emp_BS

    Emp_BN.BindingSource = Emp_BS

    Try it!!


  • Jayender.vs

    Yup I did, the line dgEmp.DataSource = myData is it. What is the different between DataTable and DataSet When I bind to the DataTable, the datagrid show the records. But if I use DataSet, no records. Yet I see some other people using DataSet. I am confused.

    I also can not call

    myAdapter.Update(myData)

    To copy the modified data back to the database.

  • Verne

    Did you change the DataSource for your DataGrid It should be the same value as the BindSource property of your BindingNavigator.

    If you want to add bound controls, such as a TextBox, you can drag the columns from the DataTable objects in your DataSets in the Data Sources Window. You can also add the control to your Form and then set the DataBindings Text value to your BindingSource.



  • Norge

    If you bind a DataGrid to a DataSet you will probably see a Treeview containing all of the DataTables in your DataSet. If you only want to work with a single DataTable then you should bind to it directly. A DataSet is essentially a container for one or more DataTables.



  • VB 2500 express with MySQL thru MySQL Connector Net