Dchris
Dchris

Reputation: 3047

Cannot insert data into SQL Server database

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();

Upvotes: 0

Views: 1468

Answers (4)

naspinski
naspinski

Reputation: 34687

SqlConnection thisConnection = new SqlConnection(
    ConfigurationManager.ConnectionStrings["NameOfConnectionString"].ConnectionString); 

Upvotes: 0

Dismissile
Dismissile

Reputation: 33071

I'm not sure if this is a mistake or not, but the ConnectionStrings indexer in the ConfigurationManager is looking for the name of the connection string:

<connectionStrings>
    <add name="SomeDB" connectionString="..." />
</connectionStrings>

ConfigurationManager.ConnectionStrings["SomeDB"].ConnectionString

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.

Upvotes: 1

vansimke
vansimke

Reputation: 974

I think you are pulling the connection string incorrectly. The ConfigurationManager.Connectionstrings is keyed on the name of the connection string in the app/web.config file. Try using the name in the .config file or just hard-coding the connection string into the SqlConnection objects constructor.

Upvotes: 0

Chriseyre2000
Chriseyre2000

Reputation: 2053

You are indexing the connection strings collection with a connection string?

int admin = 23;

  SqlConnection thisConnection = new SqlConnection(
"Data Source=...;Persist Security Info=True;User ID=myusername;Password=mypassword");
 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();

You need to fix up the connection string.

Upvotes: 2

Related Questions