0

Okay so this code produces no errors but doesn't add the data to the database. When a button is pressed, it should insert all values in the text boxes into the database.

private void addSportButton_Click(object sender, EventArgs e){

  for(int i = 0; i < numberOfPlayers; i++){

    OleDbConnection connection = new OleDbConnection(CONNECTION STRING HERE);
    OleDbCommand command = new OleDbCommand();

    command.CommandText = "INSERT INTO TotalPlayerName ([PlayerName]) VALUES (@name)";
    command.CommandType = CommandType.Text;
    command.Connection = connection;
    connection.Open();
    command.Parameters.Add("@name", OleDbType.VarWChar).Value = textBox[i].Text;
    command.ExecuteNonQuery();
    connection.Close();
  }
}

What am I doing wrong?

EDIT:

changed some things around in previous sections of code and now there are rows added but nothing appears in the PlayerName field

Code for creating the text boxes

        for (int t = 0; t < 18; t++)
         {

             textBox[t] = new TextBox(); 

             this.Controls.Add(textBox[t]);
             this.textBox[t].Font = new System.Drawing.Font("Calibri", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

            // if it is the first text box then it must go in this location
             if (t == 0)
             {

                textBox[t].Location = new Point(32, 41);
                textBox[t].Visible = true;


             }
            else
             {
                 // every other text box will be 27px below the previous
                textBox[t].Location = new System.Drawing.Point(32, 41 + (t * 27));
                textBox[t].Visible = false;


             }
        }
18
  • Are you sure about errors? Insert code in try-catch block Commented Jan 4, 2016 at 21:53
  • what should I add into each part? and as for the parameters, surely there is one parameter every time it loops trough the for loop? Commented Jan 4, 2016 at 21:54
  • So the code is definitely running, ie numberOfPlayers is greater than zero and you can step through in the debugger? Commented Jan 4, 2016 at 21:56
  • yeah the code works fine Commented Jan 4, 2016 at 21:57
  • Do you have your ACCDB/MDB file listed between your project files? If yes click on it and look at the value of the property Copy To The Output directory. What is its value? Commented Jan 4, 2016 at 21:57

1 Answer 1

2

Nine times out of ten, when an insert 'fails' and there is no error message....you are looking in the wrong database.

You are inserting to database 'A'

But looking for the record in database 'B'

Sign up to request clarification or add additional context in comments.

1 Comment

Ah! this was exactly what happened to me. In my case I provided a relative path and it was pointing to a mdb file in bin/debug where as I was looking for the inserted record in my main project folder, scratching my head. Awesome. thanks Steve.

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.