How do I add a setting in my app.config?

I can't seem to get this working. I am using VS2005. I am adding datagridview column width as settings to my app.config appsettings when I click the close button (SaveColumnSettings).
But it's not saving it. What could possibly be wrong? Am I missing something?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace MyTest
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
			
		}



		private void Form1_Load(object sender, EventArgs e)
		{
			GetData(dataGridView1, bindingSource1, "select * from mytable ", CommandType.Text);

		}


		public void GetData(DataGridView grid, BindingSource bindingSource, string commandText, CommandType commandType)
		{
			SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["mainconnectionstring"].ConnectionString);
			if (connection.State == ConnectionState.Open)
			{
				connection.Close();
			}

			connection.Open();

			SqlCommand command = new SqlCommand(commandText, connection);
			command.CommandType = commandType;
			command.CommandTimeout = 0;
			SqlDataAdapter adapter = new SqlDataAdapter(command);
			SqlCommandBuilder cb = new SqlCommandBuilder(adapter);
			adapter.UpdateCommand = cb.GetUpdateCommand(true);
			adapter.DeleteCommand = cb.GetDeleteCommand(true);
			adapter.InsertCommand = cb.GetInsertCommand(true);

			DataTable dataTable = new DataTable();
			adapter.FillSchema(dataTable, SchemaType.Mapped);
			adapter.Fill(dataTable);


			// Refresh grid.
			bindingSource.DataSource = dataTable;
			grid.DataSource = bindingSource.DataSource;
		}

		public static void SaveColumnSettings(Form frm, DataGridView grid)
		{
			try
			{
				Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);

				foreach (DataGridViewColumn column in grid.Columns)
				{
					config.AppSettings.Settings.Add(frm.Name + "." + grid.Name + ".ColumnWidth", column.Width.ToString());
					config.Save();
				}

				
			}
			catch (Exception ex)
			{
				//
			}

		}

		private void CloseButton_Click(object sender, EventArgs e)
		{
			SaveColumnSettings(this, dataGridView1);

			Application.Exit();
		}

	}
}

Open in new window


My form
stylelyfAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
>But it's not saving it.
did you check the config file during debug when this code had run?
note that you have to check in the debug folder (where the app.config file is copied when starting) ...
Tapan PattanaikSenior EngineerCommented:
Hi stylelyf,

1. As per your code, it will save the below  value.

 <add key="Form1.dataGridView1.ColumnWidth" value="100,100,100,100" />

2. You can find out in your bin folder by opening ".exe"  file  whose file type is "XML Configuration File "

Please check the  screen shot:
Path.JPG
saved-to-configfile.JPG

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stylelyfAuthor Commented:
Correct! It was indeed saving in my vshost.exe.config. I thought it will be saved in app.config in my development. Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.