Hack Forums
[C#] Problem loading data from Database - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Problem loading data from Database (/showthread.php?tid=5444075)



Problem loading data from Database - JuicyProgramming - Oct 25, 2016

Hello, I am trying to create a window where I display a list of data out of a local MSSQL database. The database is hosted locally and I have added a EDMX model to the project. When I try and load data from my database with this code:

Code
using (var context = new Model.Entities())
            {
                _list = context.table.Select(p => p).ToList();
            }

It just gets stuck at that piece of code and does nothing.

Data Sources are set correctly (automatically)

Anyone? Thanks!


* - itcorp - Oct 25, 2016

Download Example with simulation(fake table) of table:
mediafire[.]com/file/4f6b52yyovlu4en/demo.zip

Code
namespace demo
{
    public partial class Form1 : Form
    {
        private List<DemoClass> fakeTable = new List<DemoClass>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            fakeTable.Add(new DemoClass() { ID = 1, Name = "Hack", Lastname = "Forums" });
            fakeTable.Add(new DemoClass() { ID = 1, Name = "Hack 1", Lastname = "Forums 1" });
            fakeTable.Add(new DemoClass() { ID = 2, Name = "Hack 2", Lastname = "Forums 2" });
            fakeTable.Add(new DemoClass() { ID = 2, Name = "Hack 23", Lastname = "Forums 23" });
            fakeTable.Add(new DemoClass() { ID = 3, Name = "Hack 3", Lastname = "Forums 3" });

            LoadData(dgvDemo);
        }

        private void LoadData(DataGridView dgvDemo)
        {
            dgvDemo.DataSource=fakeTable.ToList();          

        }

        private void Filter(ComboBox cmbDemo, DataGridView dgv)
        {
            switch (cmbDemo.SelectedIndex)
            {
                case 0:
                    dgv.DataSource = fakeTable.Where(p => p.ID==p.ID).ToList();
                    break;
                case 1:
                    dgv.DataSource = fakeTable.Where(p => p.ID == 2).ToList();
                    break;
                case 2:
                    dgv.DataSource = (from t1 in fakeTable where t1.ID==2 select t1).ToList();
                    break;
                case 3:
                    dgv.DataSource = (from t1 in fakeTable where t1.ID == 2 select new { t1.ID,t1.Name}).ToList();
                    break;
                case 4:
                    dgv.DataSource = (from t1 in fakeTable where t1.ID == 2 select new { t1.ID, t1.Name,t1.Lastname }).ToList();
                    break;
                case 5:
                    dgv.DataSource = null;
                    dgv.DataSource = fakeTable.Select(p => p).ToList();
                    
                    break;

            }

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Filter(comboBox1,dgvDemo);
        }
    }
}

you write well encapsulation,your code seems to have written ok although I thought it was not in my first post..sorry for that... so I made a little demo to check...What can happen to the project if it is to do in MV or project that is separate from the database leyer in anather projects must copy connection string in bouth project in some case or try to update EF from v5 to v6 over nuget and reload EF in project...If none of the above help shere app config with us.