Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Controller won't load data from database

I am currently making a controller to gather myself the data i need from a database. I am not allowed to use Entity Framework and i can’t seem to find the reason my controller won’t load the data from my SQL server… It is locally hosted on my computer and the connecntion string i am using is in the controller file itself.

Thanks in advance!

I have removed the data context from my controller because i can’t share them

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

namespace ControllerFile.Controller
{
    internal class ControllerFile
    {
        private static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[""].ConnectionString;

        public GroupModel GetModel(int Id)
        {
            string sqlQuery = " Query ";

            using (SqlConnection con = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand(sqlQuery, con))
                {
                    cmd.Parameters.AddWithValue("", );

                    con.Open();

                    SqlDataReader reader = cmd.ExecuteReader();

                    while (reader.Read())
                    {
                        try
                        {
                            Model user = new Model();

                            return user;
                        }
                        catch
                        {
                            MessageBox.Show("Error");
                        }
                    }
                }
            }
            return new Model();
        }


        public void AddNewItem(Model, model)
        {
            string sqlQuery = " QUERY ";

            using (SqlConnection con = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand(sqlQuery, con))
                {
                    cmd.Parameters.AddWithValue("", model.id);

                    con.Open();

                    try
                    {
                        cmd.ExecuteNonQuery();
                    }
                    catch
                    {
                        MessageBox.Show("Error");
                    }

                }
            }
            return;
        }
    }
}

The result should be a proper connection to my database where my listview loads the correct data. I have added a hardcoded variable with a filled model and that one did load into the listview. So we can rule that code out. It has to be in the controller.

>Solution :

You’re not using the data you get from the database anywhere in that code. You do this:

while (reader.Read())

but then you never actually get any data from the data reader. You just do this:

Model user = new Model();

return user;

so you’re just returning a new, empty model, no matter what you get from the database. You need to actually get the appropriate data from the data reader and put it in the model, e.g.

Model user = new Model {SomeProperty = reader.GetString(reader.GetOrdinal("SomeColumn"))};

return user;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading