0

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" />

3
  • please let us know what exception exactly you are facing. Commented Jun 28, 2013 at 9:22
  • Can you add a try catch, and take exception, because is more easy know whats is exactly happening. Commented Jun 28, 2013 at 9:23
  • In addition to what others have already said, SqlCommand insert = new SqlCommand(command +","+ connection); should be SqlCommand insert = new SqlCommand(command, connection);. Or even better, wrap it in using(...) { ... }. Commented Jun 28, 2013 at 10:09

1 Answer 1

8

Key in web.config file is different then it is used in code.

It should be,

string connection = ConfigurationManager.ConnectionStrings["ApplicationServices"]
                                        .ConnectionString;

And no need to wrap parameter names in single quote.

 string command = @"
   INSERT INTO Persons
      (username, firstname, lastname, email, password, gender, nationality,phone) 
     VALUES
      (@username,@firstname,@lastname,@email,@password,@gender,@nationality,@phone)";

A side note, always dispose the ADO resources by using the using statement ( or calling Dispose()).

using(SqlConnection connect = new SqlConnection(connectionString))
 {
  //
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, thank you. I also noticed( I actually received an error ) that I forgot to open the connection. XD

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.