enter image description here

I am new to working with bd on C #. An error occurs when trying to add data to the database. On other forms, the same code works without problems. enter image description here

private void button1_Click(object sender, EventArgs e) { string number = textBox1.Text; string fio = textBox2.Text.ToString(); string gr = comboBox1.SelectedItem.ToString(); string text = "insert into students(nomer, fio, group) VALUES('number','" + fio + "','" + gr + "')"; OleDbCommand data = new OleDbCommand(text, myConnection); data.ExecuteNonQuery(); } 

Closed due to the fact that off-topic participants Kromster , Enikeyschik , 0xdb , aleksandr barakin , Jarvis_J 30 Nov '18 at 16:03 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Kromster, Enikeyschik, 0xdb, Jarvis_J
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    Lay out the text of the code, not its picture! - Monomax
  • remove ToString() - Monomax Nov.
  • remove the "'" from the number! - Monomax Nov.
  • @Monomax, but we still do not know the scheme of the table. Maybe his Number is a string ... - iluxa1810 Nov.
  • 1 field type string - Nurlybai Uzakbayev Nov.

3 answers 3

  1. Insert code instead of a picture.
  2. Not sure, but the word Group can be regarded as the key => screen it through [ and ] . Maybe also a number.
  3. Inserting parameters directly into a query is a bad tone and a direct path to SQL injection. If the user enters something with '' in the fields, then everything will break. Read about parameterized queries.
  4. If this does not help, then in debug take the text of the request from the Text variable and execute it in Access and it will highlight the place where the jamb.
  • judging by the picture, just the group then passes normally. - Monomax

if we take into account comments 3 from @ iluxa1810 it will be safer

Synchronous version of the method:

  public void Insert(String connectionString) { var number = textBox1.Text; var fio = textBox2.Text; var gr = comboBox1.SelectedItem.ToString(); using (var connection = new OleDbConnection(connectionString)) { using (var command = connection.CreateCommand()) { command.CommandText = "INSERT INTO STUDENTS (number, fio, group) VALUES(@number, @fio, @gr)"; command.Parameters.AddRange(new OleDbParameter[] { new OleDbParameter("@number", number), new OleDbParameter("@fio", fio), new OleDbParameter("@gr", gr) }); connection.Open(); command.ExecuteNonQuery(); } } } 

Asynchronous version of the method:

  public async void InsertAsync(String connectionString) { var number = textBox1.Text; var fio = textBox2.Text; var gr = comboBox1.SelectedItem.ToString(); using (var connection = new OleDbConnection(connectionString)) { using (var command = connection.CreateCommand()) { command.CommandText = "INSERT INTO STUDENTS (number, fio, group) VALUES(@number, @fio, @gr)"; command.Parameters.AddRange(new OleDbParameter[] { new OleDbParameter("@number", number), new OleDbParameter("@fio", fio), new OleDbParameter("@gr", gr) }); await connection.OpenAsync(); await command.ExecuteNonQueryAsync(); } } } 
  • 2
    Too complicated ... I would remove asynchrony. Since maybe the vehicle has just started to learn the language, and here we immediately show him high-level constructions that can enter into a stupor. - iluxa1810
  • in general, complicated, for what is effective) now I will correct it - Yaroslav
  • 2
    I do not argue, but the question is not about that. You could still write "WinForms garbage, do it on WPF." :) - iluxa1810
  • Minus for such asynchrony . Why the hell is Task.Run ? | OpenAsync and ExecuteReaderAsync without await - facepalm ... - Alexander Petrov
  • ToString at Text properties are not needed. Although this is not a mistake, but still. - Alexander Petrov

Your insert request is not correctly formulated, if you do not want to make a parameterized query in the database, then correct the code:

 string text ="insert into students (number,fio,group) VALUES('"+number+"','"+fio+"','"+gr+"')"; 

The DB driver swears at a match with the last name!

  • Not a compiler, but a database driver :) - iluxa1810
  • @ iluxa1810 I agree, I did not wake up yet. - Monomax