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

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.