I keep receiving this error and I'm unsure how to fix it.

The code I have is

 protected void AddUserToDatabase(String username, String Password, String Email)
        {
            OleDbConnection conn;
            OleDbCommand cmd;

            using (conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["ProjectDatabaseConnectionString"].ConnectionString))
            {
                using (cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "INSERT INTO Users(username, password, emailAddress) VALUES(@username, @password, @email)";
                    cmd.CommandType = CommandType.Text;

                    cmd.Parameters.AddWithValue("@username", username);
                    cmd.Parameters.AddWithValue("@password", Password);
                    cmd.Parameters.AddWithValue("@email", Email);

                    conn.Open();
                    cmd.ExecuteNonQuery();
                }
            }
        }

It is saying the error is on the line with cmd.CommandType = CommandType.Text;

Any help would be great, thanks

Recommended Answers

Sounds like you need to open your connection, before using the command.

Jump to Post

All 2 Replies

Sounds like you need to open your connection, before using the command.

hii,

write conn.Open() before "using (cmd = conn.CreateCommand())" line...bcz u r creating command using conn obj...which is not opened yet...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.