none
ComboBox Data Source Problem Please Help RRS feed

  • Question

  • Imports System.Data.OleDb
    Imports System.Data

    Public Class GameBoard
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            LabelDrawTime.Text = DateTime.Now
        End Sub

        Private Sub GameBoard_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim da As New OleDbDataAdapter
            Dim cn As New OleDbConnection
            Dim ds As New DataSet
            cn.ConnectionString = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\GameData.mdb;Persist Security Info=True;User ID=admin;Jet OLEDB:Database Password=admin")
            da = New OleDbDataAdapter("Select DrawTime From AllDraw Where ((GameCode)=1) And ((DrawTime)>Now())", cn)
            da.Fill(ds)
            ComboBox1.DataSource = ds.Tables(0)
            'Should Display Like This
            '06/16/2012 10:00:00 AM
            '06/16/2012 10:05:00 AM
            '06/16/2012 10:10:00 AM

            'But Displaying Like This
            'System.Data.DataRowView
            'System.Data.DataRowView
            'System.Data.DataRowView
        End Sub
    End Class

    Where is my Fault ??

    Saturday, June 16, 2012 8:53 AM

Answers

  • You need to set the DisplayMember property of the ComboBox to the name of the column that you want to show:

    ComboBox1.DisplayMember = "DrawTime"


    Marco Minerva [MCPD]
    Blog: http://blogs.ugidotnet.org/marcom
    Twitter: @marcominerva

    • Proposed as answer by Kris444 Sunday, June 17, 2012 10:13 AM
    • Marked as answer by Mike FengModerator Monday, June 25, 2012 11:43 AM
    Saturday, June 16, 2012 9:47 AM
  • Hi,

    Ur problem can be fixed by setting DisplayMember and ValueMember property of the ComboBox to the fields in the datatable or the entity containing data.

    Have a look at the code mentioned below. It worked for me.

                    string conStr = "Data Source=.;Initial Catalog= MyDataBase;Integrated Security=True";
                    using(SqlConnection conn = new SqlConnection(conStr))
                   {
                          string strCmd = "select empId, empName, joiningDate from Employee;";
                          SqlDataAdapter da = new SqlDataAdapter(strCmd, conn);
                         DataSet ds = new DataSet();
                         da.Fill(ds);
                         conn.Close();
                   }
                   cmbJoiningDate.DataSource = ds;
                   cmbJoiningDate.DisplayMember = "joiningDate";
                   cmbJoiningDate.ValueMember = "joiningDate";

    Saturday, June 16, 2012 10:44 AM

All replies

  • You need to set the DisplayMember property of the ComboBox to the name of the column that you want to show:

    ComboBox1.DisplayMember = "DrawTime"


    Marco Minerva [MCPD]
    Blog: http://blogs.ugidotnet.org/marcom
    Twitter: @marcominerva

    • Proposed as answer by Kris444 Sunday, June 17, 2012 10:13 AM
    • Marked as answer by Mike FengModerator Monday, June 25, 2012 11:43 AM
    Saturday, June 16, 2012 9:47 AM
  • Try this change

    from : ComboBox1.DataSource = ds.Tables(0)

    to : ComboBox1.DataSource = ds.Tables(0).value


    jdweng

    Saturday, June 16, 2012 9:47 AM
  • Hi,

    Ur problem can be fixed by setting DisplayMember and ValueMember property of the ComboBox to the fields in the datatable or the entity containing data.

    Have a look at the code mentioned below. It worked for me.

                    string conStr = "Data Source=.;Initial Catalog= MyDataBase;Integrated Security=True";
                    using(SqlConnection conn = new SqlConnection(conStr))
                   {
                          string strCmd = "select empId, empName, joiningDate from Employee;";
                          SqlDataAdapter da = new SqlDataAdapter(strCmd, conn);
                         DataSet ds = new DataSet();
                         da.Fill(ds);
                         conn.Close();
                   }
                   cmbJoiningDate.DataSource = ds;
                   cmbJoiningDate.DisplayMember = "joiningDate";
                   cmbJoiningDate.ValueMember = "joiningDate";

    Saturday, June 16, 2012 10:44 AM