I have been trying to connect to database, but it shows 'null' error. So, I tried opening the previous form that I practiced on, it turned out to be perfectly fine.
This is my code:
string username = txtusername.Text;
string firstname = txtfirstname.Text;
string lastname = txtlastname.Text;
string email = txtemail.Text;
string password = txtpass.Text;
string gender = rbgender.Text;
string nationality = dcountry.Text;
string phone = txtphone.Text;
string connection = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
SqlConnection connect = new SqlConnection(connection);
string command = "INSERT INTO Persons(username, firstname, lastname, email, password, gender, nationality, phone) VALUES('@username','@firstname','@lastname','@email','@password','@gender','@nationality','@phone')";
SqlCommand insert = new SqlCommand(command +","+ connection);
insert.Parameters.AddWithValue("@username", username);
insert.Parameters.AddWithValue("@firstname", firstname);
insert.Parameters.AddWithValue("@lastname", lastname);
insert.Parameters.AddWithValue("@email", email);
insert.Parameters.AddWithValue("@password", password);
insert.Parameters.AddWithValue("@gender", gender);
insert.Parameters.AddWithValue("@nationality", nationality);
insert.Parameters.AddWithValue("@phone", phone);
insert.ExecuteNonQuery();
connect.Close();
Does it have anything to do with the connectionstrings in web.config?
<connectionStrings>
<add name="ApplicationServices"
connectionString="Data Source=ADRIAN-LAPTOP\SQL;Initial Catalog=New;Integrated Security=True"
providerName="System.Data.SqlClient" />
SqlCommand insert = new SqlCommand(command +","+ connection);should beSqlCommand insert = new SqlCommand(command, connection);. Or even better, wrap it inusing(...) { ... }.