0

I am trying to fill list boxes with elements and been doing everything by a clean guide but I am getting connection string error when I am trying to run it.

Clearly i have done something foolish and can't understand it so a deeper explanation would be appreciated

The error

The ConnectionString property has not been initialized.

The Code

public partial class FormMain : Form
{
    SqlConnection connection;
    string connectionString;

    public FormMain()
    {
         InitializeComponent();

         connectionString = ConfigurationManager.ConnectionStrings["NaujasAutoSalonas.Properties.Settings.AutoSalonasConnectionString"].ConnectionString;    
    }

    private void FormMain_Load(object sender, EventArgs e)
    {
        PopulateAutoKlases();
    }    

    private void PopulateAutoKlases()
    {
        using (connection = new SqlConnection(connectionString));
           using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM AutoKlases", connection))
           {
                DataTable KlasesTable = new DataTable();
                adapter.Fill(KlasesTable);

                lstKlases.DisplayMember = "KlasesPavadinimas";
                lstKlases.ValueMember = "KlasesID";
                lstKlases.DataSource = KlasesTable;
           }
     }
 }
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • Possible duplicate of [How to fix "The ConnectionString property has not been initialized"](https://stackoverflow.com/questions/1007786/how-to-fix-the-connectionstring-property-has-not-been-initialized) – TheGeneral Feb 06 '18 at 02:28

1 Answers1

0

As you have stated that the connection string property is not initialized,so I guess that your applications is not able to read the connection string from the config file.You can confirm this by putting a breakpoint in the constructor of FormMain class.

If the connection string is not being set in the constructor then you need to identify whether you have declared the connection string properly in your config file.As there could be few config files ,so it is important that you declare your connection string in the correct config file.

Or if you are defining properties for your connection string in the .settings file,you can validate if you are accessing it correctly.

ashish
  • 2,028
  • 1
  • 10
  • 6