0

Help overload resolution failed because no accessible 'new' accepts this number of arguments. after i add another new MySqlDataAdapter then the blue lines showed up

Else
    ds = New DataSet
    da = New MySqlDataAdapter("INSERT INTO rel_receipt_details values('" & txtornum.Text & "','" & cboparticular.Text & "','" & txtamtpd.Text & "')", conn)
    da.Fill(ds)

    ds = New DataSet
    da = New MySqlDataAdapter("INSERT INTO tbl_receipt values('" & txtornum.Text & "','" & CInt(txtstudid.Text.Remove(0, 2)) & "','" & dtpdate.Text & "','" & txtcashier.Text & "')", conn)
    da.Fill(ds)

    ds = New DataSet
    da = New MySqlDataAdapter("INSERT INTO tbl_check values ('" & checkno.Text & "','" & CInt(txtstudid.Text.Remove(0, 2)) &  "','" & bankname.Text "','" &  bankbranch.Text & "')",conn)   '<-- Error on this line
    da.Fill(ds)
AstroCB
  • 12,337
  • 20
  • 57
  • 73
Emmm Ramos
  • 63
  • 2
  • 9
  • I have to insert data from 3 tables and at the last one insertion the overload resolution failed because no accessible 'new' accepts this number of arguments shown up – Emmm Ramos Aug 12 '14 at 08:49
  • The MySqlDataAdapter is not the correct object to insert directly new records to a datatable. – Steve Aug 12 '14 at 08:51
  • IN the commented line you miss a `&` after the `bankname.Text` and the following `"','"`, but as I have said these lines don't insert anything in the datatable. – Steve Aug 12 '14 at 08:54
  • 2
    Actually it WILL insert the row, because DataAdapter does not limit the `SelectCommand` and it's OK if you give an `INSERT`. To me it's like towing a car using its antenna: Works but makes no sense. – Alireza Aug 12 '14 at 09:04
  • 2
    @Alireza you are right. I have overlooked the fact that everything you pass as the command text will be blindly passed to the database for execution. So also an INSERT INTO, DELETE, UPDATE and even a CREATE TABLE, if it is syntactically correct, will be executed. I agree on the _makes no sense_. – Steve Aug 12 '14 at 09:12

1 Answers1

2

In the commented line you miss a & after the bankname.Text and the following "','", but if you need to insert directly data in the database table from some input textbox then you should use a MySqlCommand with the appropriate command and ExecuteNonQuery with it.

So, just for example

  Dim cmdText = "INSERT INTO rel_receipt_details values(@p1,@p2,@p3)"
  Dim cmd = new MySqlCommand(cmdText,  conn)
  cmd.Parameters.AddWithValue("@p1", txtornum.Text)
  cmd.Parameters.AddWithValue("@p2", cboparticular.Text)
  cmd.Parameters.AddWithValue("@p3", txtamtpd.Text)
  cmd.ExecuteNonQuery()

Note that you should not write a command text using string concatenation. Use always a parameterized query to avoid Sql Injection and parsing problems caused by strings with embedded quotes or date formats or decimals symbols.

The code above should be repeated for the other two tables. (or you could build a single cmdText with the three commandtexts separated by a semicolon without forgetting the required parameters)

As pointed out by a comment the DataAdapter will insert that data because it will blindly call ExecuteReader on the commandtext passed, but this is completely wrong from a logic point of view. A DataAdapter is used initially to retrieve records from the database and fill a DataSet and then, when you have made changes to the tables (via code or via a binding control like the DataGridView), call the Update method that executes the Insert, Update and Delete required.

Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286