Combobox and Textbox data binding

djiceman

Member
Joined
Oct 20, 2006
Messages
10
Location
South Africa
Programming Experience
3-5
Hi

Can anyone help me with using a combobox and textbox for showing data from a mysql database.

I mostly found examples using the datagrid and cannont find any help on using textboxes.

Please assist. I am using myodbc or you recommend another connecter?

Regards
Nolan
 
Last edited by a moderator:

djiceman

Member
Joined
Oct 20, 2006
Messages
10
Location
South Africa
Programming Experience
3-5
This is what im using so far, ive only gone as far as reading from the database, not storing/updating.

VB.NET:
Dim temp As Integer
temp = id.SelectedItem
Dim fdCom1 As New OleDb.OleDbCommand("SELECT * FROM client where clientid =" & temp, fdCon)
fdCom1.Connection.Open()
Try
Dim fdRead1 As OleDb.OleDbDataReader = fdCom1.ExecuteReader(CommandBehavior.CloseConnecti on)
While fdRead1.Read
TextBox1.Text = fdRead1.GetValue(1)
End While
Catch myerror As Exception
MessageBox.Show("Error Connecting to Database: " & myerror.Message)
fdCon.Dispose()
End Try
fdCon.Close()
 
Last edited by a moderator:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
E.g. a table with ID, Name and Description columns:
VB.NET:
Dim myDataAdapter As New OleDb.OleDbDataAdapter("SELECT * FROM MyTable", myConnection)
Dim myDataTable As DataTable

myDataAdapter.Fill(myDataTable)

Me.ComboBox1.DisplayMember = "Name"
Me.ComboBox1.ValueMember = "ID"
Me.ComboBox1.DataSource = myDataTable

Me.TextBox1.DataBindings.Add("Text", myDataTable, "Description")
 

djiceman

Member
Joined
Oct 20, 2006
Messages
10
Location
South Africa
Programming Experience
3-5
Thanks. Though I get an error:

Value cannot be null.
Parameter name: dataTable

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim myDataAdapter As New OleDb.OleDbDataAdapter("SELECT * FROM client", fdCon)
Dim myDataTable As DataTable

Try
myDataAdapter.Fill(myDataTable)
Me.ComboBox1.DisplayMember = "clientid"
Me.ComboBox1.ValueMember = "clientid"
Me.ComboBox1.DataSource = myDataTable

Me.TextBox3.DataBindings.Add("Text", myDataTable, "clientid")

Catch ex As Exception
MessageBox.Show(ex.Message)
fdCon.Dispose()

End Try

End Sub

So I used some other code:

SQL = "SELECT * FROM client"

conn.ConnectionString = myConnString

conn.Open()


myCommand.Connection = conn
myCommand.CommandText = SQL

myAdapter.SelectCommand = myCommand
myAdapter.Fill(Me.myData)
' myAdapter.Fill(test, "client")
Dim mybind As Binding = New Binding("Text", myData, "name")
TextBox1.DataBindings.Add(mybind)

This works fine, most of the declarations are done in the class.
How would I now update any information that I change in the text box to update into the database?

Thanks
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
Sorry, my code should have been
VB.NET:
         Dim myDataTable As [U][B]New[/B][/U] DataTable
As for what to do now, I suggest reading an ADO.NET tutorial. Do a member search for TechGnome and follow the links in his signature to his ADO.NET tute.
 
Top Bottom