GtOkAi
GtOkAi

Reputation: 87

Error with Connection property has not been initialized

Well, I work little bit with C # and I'm starting to work with Database with C # now, I've googled in several places and I am unable to identify where it is wrong, everywhere say I need to open a connection, but it is already open .

        SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;Integrated Security=True;AttachDbFilename=C:\Users\Gustavo\Documents\Visual Studio 2013\Projects\hour\hour\Database1.mdf");
        con.Open();
        try
        {
            string query = "INSERT INTO [Table] (name, time) VALUES ('test',1)";
            SqlCommand cmd = new SqlCommand(query);
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

Upvotes: 0

Views: 278

Answers (3)

Fred
Fred

Reputation: 5808

Use using, takes care of the closing and disposal for you just in case you forget to do it explicitly. Put it inside the try, you have the connection open command outside the try so it wont catch any connection error. You probably want to look at parameterizing your command too.

using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;Integrated Security=True;AttachDbFilename=C:\Users\Gustavo\Documents\Visual Studio 2013\Projects\hour\hour\Database1.mdf"))
{
        conn.Open();
        using (SqlCommand cmd = new SqlCommand("INSERT INTO [Table] (name, time) VALUES (@name,@time)", conn))
        {
            cmd.Parameters.AddWithValue("@name", "test");
            cmd.Parameters.AddWithValue("@time", 1);
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
        }
}

Upvotes: 2

Khurram Ali
Khurram Ali

Reputation: 1679

  SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;Integrated Security=True;AttachDbFilename=C:\Users\Gustavo\Documents\Visual Studio 2013\Projects\hour\hour\Database1.mdf");
        try
        {
            string query = "INSERT INTO [Table] (name, time) VALUES ('test',1)";
            SqlCommand cmd = new SqlCommand(query,con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

Upvotes: 1

Ewan
Ewan

Reputation: 1285

you need to assign the command to the connection. eg:

    private static void ReadOrderData(string connectionString)
    {
        string queryString = 
            "SELECT OrderID, CustomerID FROM dbo.Orders;";
        using (SqlConnection connection = new SqlConnection(
                   connectionString))
        {

//----

            SqlCommand command = new SqlCommand(
                queryString, connection);


//----
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            try
            {
                while (reader.Read())
                {
                    Console.WriteLine(String.Format("{0}, {1}",
                        reader[0], reader[1]));
                }
            }
            finally
            {
                // Always call Close when done reading.
                reader.Close();
            }
        }
    }

Upvotes: 0

Related Questions