Showing data in a DatagridView or ListBox - VB 2005

Hello, i made a ODBC connection and it works. The problem is, i want to display the data in a datagrid o listbox and i just get blank fields in the grid, and in the listbox i get "System.Data.DataRowView". As a novice i don't know what's the inconveninet.

Help please..

This is the code:

Dim StrConn As String = "DSN=blabla;UID=user;PWD=password"

Dim MyConn As New OdbcConnection(StrConn)

Dim MySelect As String = "Select campo1, campo2 from tabla"

Dim MyDataAdapter As New OdbcDataAdapter

MyConn.Open()

Dim MyTable As New DataTable

MyDataAdapter.SelectCommand = New OdbcCommand(MySelect, MyConn)

MyDataAdapter.Fill(MyTable)

Me.BindingSource1.DataSource = MyTable

Me.DataGridView1.DataSource = Me.BindingSource1

MyConn.Close()



Answer this question

Showing data in a DatagridView or ListBox - VB 2005

  • Ridi

    Adam, a lot of thanks for the help.. But, i'm still getting the same "Blank Fields".

    I want to know something.. the ODBC connection is to an Oracle DataBase, does the VB 2005 Express Edition support this connection


  • Eric C.

    In Express the design time designers do not support remote connections, but you can do it in code.

    To debug your problem further, you need to check if your datatable has any rows after the fill, ie:

        MsgBox(MyDataSet.Tables(0).Rows.Count)

    If that doesn't help, try what I did and see if you can get it to work by connecting to a local Access database.

    Adam


  • TreeSapp

    Zeifer,

    You need to bind to a DataSet, not a data table.  On the binding source you tell it which datatable to bind to.  Here is sample code (I created an ODBC DSN to the Access database NWind.mdb)

     

    Dim MyDataAdapter As New Odbc.OdbcDataAdapter

    Dim MyDataSet As New DataSet

    Dim MySelect As String = "Select * from Customers"

    Dim StrConn As String = "DSN=NWind;UID=Admin;PWD=;"

    Dim MyConn As New Odbc.OdbcConnection(StrConn)

    MyConn.Open()

    MyDataAdapter.SelectCommand = New Odbc.OdbcCommand(MySelect, MyConn)

    MyDataAdapter.Fill(MyDataSet)

    Me.BindingSource1.DataMember = MyDataSet.Tables(0).TableName

    Me.BindingSource1.DataSource = MyDataSet

    Me.DataGridView2.DataSource = Me.BindingSource1

    MyConn.Close()

     

    Hope this helps,

    Adam Braden
    Visual Basic Team


  • gung

    Zeifer,

    So it does appear your dataset is getting filled.  So the problem must be with the BindingSource or the DataGridView.  However, this should only be 3 lines of code, like in my example above.  I'm not sure what the problem would be.

    How about this - can you try the code I have above, and connect to an Access NWind.mdb database   Do you have access to that   To setup the DSN connection, I went into ODBC administrator (in control panel - or run "odbcad32.exe") and created a system DSN for NWind.mdb using the Access driver.  Then use the code in my sample above.  

    Let me know what happens there or if that leads you down to a discovery of your problem.

    Adam Braden
    Visual Basic Team


  • Herbert_de

    Heeeeeelp
  • Crashin

    With the rows count i get 492 rows, that means the connection and the dataset are working properly, i guess i'm doing something wrong.

    Adam, thanks a lot.


  • Showing data in a DatagridView or ListBox - VB 2005