简体   繁体   中英

`DataTable` does not contain a definition for 'Fill' - c#

I am not understanding why I am getting an error after reading documentation in regards to the .Fill() . Am I missing something that is causing this to return with an error?

    protected void FillData()
    {
        using (SqlConnection connection = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB;AttachDbFilename= C:\Users\home\Documents\C# Programs\shop\Database.mdf ;Integrated Security = True"))
        {
            connection.Open();

            using (SqlDataAdapter dataAdapter = new SqlDataAdapter("select * from Employee", connection))
            {
                DataTable table = new DataTable();
                table.Fill(table);
                employeeDataGridView.DataSource = table;
            }

        }
    }

The problem is in this line of code

table.Fill(table);

You can't use table, to fill your table. The correct syntax would be

dataAdapter.Fill(table) 

You can't populate a DataTable in that way, you need to fill your DataAdapter, use a DataSet and then set the DataGridView to use this.

using (SqlDataAdapter dataAdapter = new SqlDataAdapter("select * from Employee", connection))
{
    var ds = new DataSet();
    dataAdapter.Fill(ds);
    employeeDataGridView.DataSource = ds.Tables[0];
}

The neatest way to code this up is:

protected void FillData()
{

        using (SqlDataAdapter dataAdapter = new SqlDataAdapter("select * from Employee", @"Data Source = (LocalDB)\MSSQLLocalDB;AttachDbFilename= C:\Users\home\Documents\C# Programs\shop\Database.mdf ;Integrated Security=True"))
        {
            DataTable table = new DataTable();
            dataAdapter.Fill(table);
            employeeDataGridView.DataSource = table;
        }


}

It'll get neater if you put your connection string in a static "global" variable somewhere

Points of note:

  • use the dataadapter constructor that takes two strings - it will create the connection and the command for you
  • you don't need to open the connection for the adapter- it knows how to
  • this means you can have just one using
  • if your sql needs parameters put them inside the using as dataAdapter.SelectCommand.Paramaters.Add...
  • you could turn this into a method that accepts any sql string and parameter collection and returns you a datatable
  • consider putting a WHERE clause in your sql; users might not like to see a grid with 20,000 employees in because it makes it harder to edit a handful of employees

It would be better to add a DataSet to your project (right click project, add>>new item, choose dataset - it gives you something that looks like a db visual design surface you can add queries to, create datatables that become components that can be added to your forms/create databound controls automatically) and create strongly typed datatables and table adapters

An alternative better route than this would be to use Dapper and strongly typed POCOs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM