DataSet does not return Data

Radu Flesar 26 Reputation points
2022-02-14T23:02:42.53+00:00

Hi! I want to get data from an Access Database through a DataSet but all the tables appear to be empty.

using(var x = new DataSet1())
 {
       Console.WriteLine("Am intrat");
       var l = x.Angajati_Detalii.FirstOrDefault();
       foreach(var item in x.Angajati_Detalii)
       {  
            Console.WriteLine(item);
       }
       Console.WriteLine("Am iesit");
 }

This is my code, the DataSet was created automatically from DataSource menu in visual studio.
I checked from the debugger when the program is running but all the tables have count 0.
Am I missing something?
Thank you very much!

Azure Open Datasets
Azure Open Datasets
An Azure service that provides curated open data for machine learning workflows.
27 questions
Azure Data Explorer
Azure Data Explorer
An Azure data analytics service for real-time analysis on large volumes of data streaming from sources including applications, websites, and internet of things devices.
529 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,995 questions
Access Development
Access Development
Access: A family of Microsoft relational database management systems designed for ease of use.Development: The process of researching, productizing, and refining new or existing technologies.
880 questions
{count} votes

Accepted answer
  1. Karen Payne MVP 35,431 Reputation points
    2022-02-15T00:36:15.4+00:00

    Here is an example for reading not using a setup done in a form

    public static async Task<DataSet> GetDataSet()
    {
    
        DataSet dataSet = new ();
    
        await using var cn = new OleDbConnection { ConnectionString = ConnectionString };
        await using var cmd = new OleDbCommand() { Connection = cn };
    
        cmd.CommandText = "SELECT EmployeeID, FirstName, LastName FROM Employees";
    
        await cn.OpenAsync();
    
        DataTable table = new ("EmployeeTable");
        table.Load(cmd.ExecuteReader());
    
        dataSet.Tables.Add(table);
    
        return dataSet;
    }
    

    Caller

    private async void ReadDatSetButton_Click(object sender, EventArgs e)
    {
    
        await Task.Run(async () =>
        {
            DataSet dataSet = await EmployeesOperations.GetDataSet();
    
            DataTable employeeTable = dataSet.Tables["EmployeeTable"];
    
            foreach (DataRow row in employeeTable.Rows)
            {
                Debug.WriteLine($"{string.Join(",", row.ItemArray)}");
            }
    
        });
    }
    

    And EF Core is even better, requires the following package. Using Microsoft's common model, blog with post.

    Main model - by using model name + Id EF uses it for the primary key, otherwise you need to tell EF via a configuration.

    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }
        public List<Post> Posts { get; set; }
        public override string ToString() => BlogId.ToString();
    
    }
    

    Child model, same thing with the primary key

    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public int BlogId { get; set; }
        public Blog Blog { get; set; }
        public override string ToString() => Title;
    
    }
    

    The following uses SQL-Server, change UseSqlServer to whatever the access provider uses.

    public class BloggingContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
    
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(
                "Server=(localdb)\\mssqllocaldb;" + 
                "Database=EFSaving.RelatedData;Trusted_Connection=True");
        }
    }
    

    Create the database in code

    using (var context = new BloggingContext())
    {
        context.Database.EnsureDeleted();
        context.Database.EnsureCreated();
    }
    

    Add some data

    using (var context = new BloggingContext())
    {
        var blog = new Blog
        {
            Url = "http://blogs.msdn.com/dotnet",
            Posts = new List<Post>
            {
                new() { Title = "Intro to C#" },
                new() { Title = "Intro to VB.NET" },
                new() { Title = "Intro to F#" }
            }
        };
    
        context.Blogs.Add(blog);
        Console.WriteLine($"{context.SaveChanges()}");
    }
    

    Read data

    foreach (var item in context.Blogs.ToList())
    {
        Console.WriteLine($"{item.BlogId}");
        foreach (var postItem in item.Posts)
        {
            Console.WriteLine($"\t{postItem.Title}");
        }
    }
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Ken Tucker 5,851 Reputation points
    2022-02-14T23:56:56.52+00:00

    This line of code creates a new dataset1 with no data in it.

      using(var x = new DataSet1())
      {
    

    You would have load data into your dataset with your code or fill it from a database or xml file

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.