I have already read all the previous similar posts but I couldn’t find a solution. Could you please take a look at my code? I don’t get any exception. I just don’t see the new data in the database.
int admin = 23;
SqlConnection thisConnection = new SqlConnection(
ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString);
SqlCommand nonqueryCommand = thisConnection.CreateCommand();
thisConnection.Open();
nonqueryCommand.CommandText =
"INSERT INTO Account (Username, Password, AdministratorId) VALUES (@username, @password, @admin)";
nonqueryCommand.Parameters.Add("@username", SqlDbType.VarChar, 20);
nonqueryCommand.Parameters["@username"].Value = UsernameTextbox.Text.ToString();
nonqueryCommand.Parameters.Add("@password", SqlDbType.VarChar, 15);
nonqueryCommand.Parameters["@password"].Value = PasswordTextbox.Text.ToString();
nonqueryCommand.Parameters.Add("@admin", SqlDbType.Int);
nonqueryCommand.Parameters["@admin"].Value = admin;
nonquerycommand.ExecuteNonQuery();
thisConnection.Close();
I’m not sure if this is a mistake or not, but the
ConnectionStringsindexer in theConfigurationManageris looking for the name of the connection string:I assume this would’ve caused an error in your code though, perhaps a null reference exception but I don’t remember how that class works.