im trying to get data from access to a combobox1 but its not working.
my database is a .accdb file. my table name is "list".
the columns names are:
"ID_lista" = autonumber
"Nome" = smalltext
"Link" = hyperlink
here is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace teste_pwd
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public static class Globals
{
public static String ConString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + Application.StartupPath + "\PWD_BD.accdb";
public static OleDbCoection CoectionString = new OleDbCoection(Globals.ConString);
}
private void Form2_Load(object sender, EventArgs e)
{
Globals.CoectionString.Close();
pCMB();
}
public void pCMB()
{
try
{
Globals.CoectionString.Open();
int i = 0;
string query = "SELECT * FROM List";
OleDbDataAdapter dataAdapeter = new OleDbDataAdapter(query, Globals.CoectionString);
DataSet mydata = new DataSet();
dataAdapeter.Fill(mydata, "List");
comboBox1.DisplayMember = "Texto";
comboBox1.ValueMember = "Valor";
DataTable tb = new DataTable();
tb.Columns.Add("Texto", typeof(string));
tb.Columns.Add("Valor", typeof(int));
for (i = 0; i <= mydata.Tables[0].Rows.Count - 1; i++)
{
tb.Rows.Add(mydata.Tables[0].Rows[i].ItemArray[1], mydata.Tables[0].Rows[i].ItemArray[0]);
}
comboBox1.DataSource = tb;
Globals.CoectionString.Close();
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
}
}
