Why can't I add items to the database?

How to add items to existing database thru application form

  • I currently have a dataset and would like to continually add new items to that dataset (like type a new entry in a textbox and add it with a button) and edit current items via the applications form I created. How would I go about tackling this issue? edit: This was the best base start I could come up with watching videos and going through google. Dictionary.mdf is my dataset SqlCommand cmd = new SqlCommand("insert into dictionary(word, definition) values(@word, @definition)"); cmd.Parameters.AddWithValue("@word", textBoxWordtoAdd.Text); cmd.Parameters.AddWithValue("@definition", textBoxDefinition.Text); this.Close();

  • Answer:

    I think you are missing the cmd.ExecuteNonQuery(); If you were using Ms Sql Server. Then your code should look something like this: using(SqlConnection connection = new SqlConnection(ConnectionString)) { connection.Open(); string sql = "insert into dictionary(word, definition) values(@word, @definition)"; SqlCommand cmd = new SqlCommand(sql,connection); cmd.Parameters.Add("@word", SqlDbType.Varchar, 50).value = textBoxWordtoAdd.Text; cmd.Parameters.Add("@definition", SqlDbType.Varchar, 50).value = textBoxDefinition.Text; cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); } You also need to open a conneciton which I don't see in your code, but I assume you do it somewhere. Just replace the ConnectionString in the code with your own connection string.

John at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.