1

I am currently getting the error: System.InvalidOperationException: 'The ConnectionString property has not been initialized.' I would like some help to fix this as I am trying to input data from a c#form (multiple textboxes) to an SQL data base form my current code is

 private void AcceptData()
    {
        using (Connection = new SqlConnection(connectionString))
        using (SqlDataAdapter adapter = new SqlDataAdapter("INPUT INTO Person", Connection))//, Connection
        {
            DataTable RegisterTable = new DataTable();
            adapter.Fill(RegisterTable); //System.InvalidOperationException: 'The ConnectionString property has not been initialized.' TO FIX

            string name = textBox1.Text;
            string organisation = textBox3.Text;
            DateTime Time = DateTime.Parse(textBox2.Text);
            string strDateTimeIn = Time.ToString("yyyy-MM-dd HH:mm:ss.ffff");
            string query = "INSERT INTO Person (Name,Organisation,TimeIn) VALUES('" + name + "','" + organisation + "','" +strDateTimeIn+ "')";
            SqlCommand SignIn = new SqlCommand(query,Connection);
            SignIn.ExecuteNonQuery();
        }

    }

Any help would be appreciated thank you

The connection string used is :string connectionString = (@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Register.mdf;Integrated Security=True");

WMTS
  • 53
  • 1
  • 1
  • 12

2 Answers2

4

You need to open the connection

using (Connection = new SqlConnection(connectionString))
{
    connection.Open();

    using (SqlDataAdapter adapter = new SqlDataAdapter("INPUT INTO Person", Connection))//, Connection
        {
            DataTable RegisterTable = new DataTable();
            adapter.Fill(RegisterTable); //System.InvalidOperationException: 'The ConnectionString property has not been initialized.' TO FIX

            string name = textBox1.Text;
            string organisation = textBox3.Text;
            DateTime Time = DateTime.Parse(textBox2.Text);
            string strDateTimeIn = Time.ToString("yyyy-MM-dd HH:mm:ss.ffff");
            string query = "INSERT INTO Person (Name,Organisation,TimeIn) VALUES('" + name + "','" + organisation + "','" +strDateTimeIn+ "')";
            SqlCommand SignIn = new SqlCommand(query,Connection);
            SignIn.ExecuteNonQuery();
        }
}
Richard Boyce
  • 413
  • 5
  • 12
1

"The ConnectionString property has not been initialized" clearly indicates that the connection string property for opening a SqlConnection has not assigned properly inside the method. To fix this issue, either assign the connection string in method body like this:

private void AcceptData()
{
    // assumed the connection string obtained from app.config or web.config file
    string connectionString = ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;
    using (SqlConnection Connection = new SqlConnection(connectionString))
    {
        Connection.Open(); // open the connection before using adapter
        using (SqlDataAdapter adapter = new SqlDataAdapter("INPUT INTO Person", Connection))
        {
            // other stuff
        }
    }

    // other stuff
}

Or pass the connection string as method argument:

private void AcceptData(string connectionString)
{
    using (SqlConnection Connection = new SqlConnection(connectionString))
    {
        Connection.Open(); // open the connection before using adapter
        using (SqlDataAdapter adapter = new SqlDataAdapter("INPUT INTO Person", Connection))
        {
            // other stuff
        }
    }

    // other stuff
}

References:

How to fix "The ConnectionString property has not been initialized"

C# Database The ConnectionString property has not been initialized

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61