What is the QUERY variable ? Is it a Stored procedure ?
An InsertCommand is required by your DataAdapter because you are using
the NewRow() command to add a new row. This results in the RowState
becoming "Added". Then you are using the Update method of the
DataAdapter to update the DB. Since you have not provided an
appropriate UpdateCommand and InsertCommand and linked them with your
DataAdapter, it does not know how to Insert a Row into the DB.
You,therefore, have two options :
1. Use a CommandBuilder with your SelectCommand and it will
automatically generate all the other Commands (Update, Delete,
Insert). I normally do not recommend this method because using the
CommandBuilder is performance intensive. Also, this will not work if
your QUERY is a stored procedure.
2. Provide an InsertCommand and UpdateCommand and link them to the
DataAdapter.
On Nov 10, 3:39 am, Dickery1 <[EMAIL PROTECTED]> wrote:
> now i get another error.
> Exception caught.System.InvalidOperationException: Update requires a
> valid InsertCommand when passed DataRow collection with new rows.
>
> not sure what is wrong now.
>
> On Nov 9, 3:30 am, Cerebrus <[EMAIL PROTECTED]> wrote:
>
>
>
> > Most common ADO.NET error of all time :
>
> > - Remove the line that calls AcceptChanges on the DataTable.
>
> > Another Tip: You don't need to manually open and close connections
> > when using a DataAdapter.
>
> > On Nov 9, 7:00 am, Dickery1 <[EMAIL PROTECTED]> wrote:
>
> > > i dont know why this code is not saving the data that i am trying to
> > > insert. appreciate any help
> > > MySqlConnection myConnection = new MySqlConnection(CONN);
> > > MySqlDataAdapter myDataAdapter = new
> > > MySqlDataAdapter(QUERY, myConnection);
> > > DataTable dt = new DataTable();
>
> > > //Open connection
> > > myConnection.Open();
>
> > > try
> > > {
> > > //Fill dataset
> > > myDataAdapter.Fill(dt);
>
> > > //Close connection
> > > myConnection.Close();
> > > DataTable dtPortfolios = dt;
> > > DataRow newRow = dtPortfolios.NewRow();
> > > newRow["symbol"] = "errwerew";
> > > newRow["quantity"] = 2;
> > > newRow["cost"] = 4;
> > > newRow["created_at"] = DateTime.Now;
> > > dtPortfolios.Rows.Add(newRow);
> > > dtPortfolios.AcceptChanges();
> > > myDataAdapter.Update(dt);
>
> > > }
> > > catch (Exception ex)
> > > {
>
> > > //Close connection
> > > myConnection.Close();
>
> > > throw ex;
>
> > > }- Hide quoted text -
>
> > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -