Demandread
Demandread

Reputation: 15

Fill combo box from access table c#

I come from VB.net, and I'm trying to learn C# so I'm programming my apps now in C# instead of Vb.net.

I'm trying to fill a combo box with some data I have in an access table, but my code that worked in vb.net, doesn't seem to behave the same in C#. Can anyone help me find out why this is not working?

try
{
    //string turno = "1";
    //fillnames(turno);

    OleDbConnection conn = new OleDbConnection();
    conn.ConnectionString = @"Provider= Microsoft.ACE.OLEDB.12.0; Data Source=path.accdb;";

    DataSet ds = new DataSet();
    DataTableCollection tables = new DataTableCollection();
    OleDbDataAdapter da = new OleDbDataAdapter();
    tables = ds.Tables;
    da = new OleDbDataAdapter("SELECT [Materialista] FROM [OPS] WHERE [Turno] = '" + "1" + "'", conn);
    da.Fill(ds, "Ops");

    AutoCompleteStringCollection col = new AutoCompleteStringCollection();

    for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
    {
        col.Add(ds.Tables[0].Rows[i]["Dnum"].ToString());
    }

    cmb_operador.AutoCompleteSource = AutoCompleteSource.CustomSource;
    cmb_operador.AutoCompleteCustomSource = col;
    cmb_operador.AutoCompleteMode = AutoCompleteMode.Suggest;

}
catch
{
}

The error I get is:

The type System.Data.DataTableCollection has no constructors defined

I use pretty much the same just in vb.net syntax and it works flawlessly

Upvotes: 0

Views: 780

Answers (2)

Demandread
Demandread

Reputation: 15

I will use this answer to attach the corrected code for what i wanted to do, using @Gusman suggestion, in case anyone has any use for it.

  try
            {
                //string turno = "1";
                //fillnames(turno);

                OleDbConnection conn = new OleDbConnection();
                conn.ConnectionString = @"Provider= Microsoft.ACE.OLEDB.12.0; Data Source=path.accdb;";

                DataSet ds = new DataSet();

                OleDbDataAdapter da = new OleDbDataAdapter();
               DataTableCollection tables = ds.Tables;
                da = new OleDbDataAdapter("SELECT [Materialista] FROM [OPS] WHERE [Turno] = '" + "1" + "'", conn);
                da.Fill(ds, "Ops");

                AutoCompleteStringCollection col = new AutoCompleteStringCollection();

                for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                {
                    col.Add(ds.Tables[0].Rows[i]["Materialista"].ToString());
                }

                cmb_operador.DataSource = col;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

Upvotes: 1

Gusman
Gusman

Reputation: 15151

DataTableCollection does not have a public constructor, so you can't instantiate it. And in your case, you don't need it, change your code to this:

//...
//remove DataTableCollection tables = new DataTableCollection();
OleDbDataAdapter da = new OleDbDataAdapter();
DataTableCollection tables = ds.Tables;
//...

Upvotes: 0

Related Questions