A
A
Albert Yaman2016-12-26 18:53:47
SQL
Albert Yaman, 2016-12-26 18:53:47

The ConnectionString property is not initialized. How to fix?

How to fix this message?
Everything works fine, until the moment when I want to write the changed datagrid data to the database

static string stringConnect = ConfigurationManager.ConnectionStrings["OrdersConnection"].ConnectionString; 
SqlDataAdapter adapter;
DataSet dataSet;

public void ViewOrders()
        {
            string sqlCommandSelect = @"SELECT * FROM [ORDERS]";

            using (SqlConnection connection = new SqlConnection(stringConnect))
            {

                adapter = new SqlDataAdapter(sqlCommandSelect, connection);
                dataSet = new DataSet();
                adapter.Fill(dataSet, "ORDERS");

                gridForControl.ItemsSource = new DataView(dataSet.Tables["ORDERS"]);
            }
        }

        private void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
            adapter.Update(dataSet.Tables["ORDERS"]); // тут ошибка
            //ViewOrders();
            MessageBox.Show("Данные успешно обновленны!");

        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sumor, 2016-12-26
@albyaman

Connection works for you only within using. It also closes it.
Or you need to create and store a SqlConnection for a long time, most likely all the time the program is running.
Or, in the ButtonSave_Click handler, recreate the SqlConnection and assign it to the adapter.Connection property.
Re-creating a SqlConnection in a short period of time does not lead to a physical re-creation of the database connection, since the provider still holds connections for some time after closing in case they are needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question