1
namespace FormUI
{
        public partial class Dashboard : Form
        {
            List<Person> people = new List<Person>();

            public Dashboard()
            {
                InitializeComponent();
                listBox1.DataSource = people;
                listBox1.DisplayMember = "FullInfo";
            }

            private void button1_Click(object sender, EventArgs e)
            {
               // Dashboard d = new Dashboard();
                DataAccess db = new DataAccess();
                people=db.GetPeople(textBox1.Text);
            }
        }
}

namespace FormUI
{
    class DataAccess
    {
        public List<Person> GetPeople(string name)
        {
            using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("databaseEx")))
            {
                var output = connection.Query<Person>($"select * from member").ToList();
                return output;
            }
        }
    }
}

namespace FormUI
{
    public static class Helper
    {
        public static string CnnVal(string na)
        {
            return ConfigurationManager.ConnectionStrings[na].ConnectionString;
        }
    }
}

Config:

<add name="SampleDB" 
     connectionString="Server=local;Database=databaseEx;Trusted_Connection=True;" 
     providerName="System.Data.SqlClient"/>

When I am running this code, I get an error:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

System.Configuration.ConnectionStringSettingsCollection.this[string].get returned null.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

Replace this line in your code

using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SampleDB"].ConnectionString))
Mihir Dave
  • 3,954
  • 1
  • 12
  • 28
  • That worked but agian its giving me a different error instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)' – Manjinder Grewal Jun 19 '18 at 15:03
  • @ManjinderGrewal you will find this helpful https://stackoverflow.com/questions/18060667/why-am-i-getting-cannot-connect-to-server-a-network-related-or-instance-speci – Mihir Dave Jun 19 '18 at 15:07