0

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

3
Contributors
2
Replies
19
Views
3 Years
Discussion Span
Last Post by aneri28
0

hii,

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.