0

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

Bender Bending
  • 729
  • 8
  • 25
Demandread
  • 15
  • 2
  • Possible duplicate of [C# - Fill a combo box with a DataTable](https://stackoverflow.com/questions/256832/c-sharp-fill-a-combo-box-with-a-datatable) – MethodMan Aug 03 '17 at 18:28

2 Answers2

1

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);
            }
Demandread
  • 15
  • 2
0

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;
//...
Gusman
  • 14,905
  • 2
  • 34
  • 50
  • Thanks Gusman, this really solved my issue, it seems to have been a very simple error. Also I noticed that I still had errors down below in the code which now I have corrected, because I was copying this code for a text box I was auto completing, but I actually just wanted to populate the combo box this time. – Demandread Aug 03 '17 at 20:19