connect MS access DB oleDB problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: connect MS access DB oleDB problem

Hybrid View

  1. #1
    Join Date
    Jan 2009
    Posts
    3

    Unhappy connect MS access DB oleDB problem

    Hi again, I'm having trouble whilst connecting a MS access db using oleDB

    Problem is with getting more than one column added into a listbox in my application from the DB
    here's the code so far


    OleDbConnection Conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Scott\\Documents\\Menu.accdb");

    OleDbDataAdapter da = new OleDbDataAdapter(
    "Select * from Menu", Conn);

    DataSet ds = new DataSet();
    da.Fill(ds);
    DataTable dt = ds.Tables[0];

    foreach (DataRow dr in dt.Rows)
    {
    listBox1.Items.Add(dr["Dish"]);
    }
    }

    I just can't find a way to get the second column added ?

    thanks,Scott

  2. #2
    Join Date
    Jan 2006
    Location
    18 32' N / 73 52' E
    Posts
    416

    Re: connect MS access DB oleDB problem

    Instead of using ListBox, use ListView.
    Regards,
    MMH
    Rate my post if you find it usefull.

  3. #3
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: connect MS access DB oleDB problem

    Code:
    foreach (DataRow dr in dt.Rows)
    {
      ListViewItem item = new ListViewItem(dr["Dish"]);
      item.SubItems.Add(dr["secondField"]);
      listView1.Items.Add(item);
    }
    Change the View property to Details and add some columns to the listview

  4. #4
    Join Date
    Jan 2009
    Posts
    3

    Re: connect MS access DB oleDB problem

    thanks

    I'm getting some errors when trying to use a listbox:

    cannot convert from 'double' to 'System.Windows.Forms.ListViewItem.ListViewSubItem

    it's referring to my second field in the DB, which is 'Cost' with data type double

    any idea's?

    much appreciated..

  5. #5
    Join Date
    Jan 2006
    Location
    18 32' N / 73 52' E
    Posts
    416

    Re: connect MS access DB oleDB problem

    You cannot directly add double to ListView Collection.
    First convert it to string type.

    Code:
    item.SubItems.Add(dr["cost"].ToString());
    Regards,
    MMH
    Rate my post if you find it usefull.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center