none
Prepare: CommandText property has not been initialized

    Question

  • I have a form that I drug a "details" list into from the data sources pane.  When I click the save button I get that non-descript error and I cannot figure out why.

        Private Sub ButtonSaveUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSaveUser.Click
            Me.Validate()
            Me.UsersBindingSource.EndEdit()
            Me.UsersTableAdapter.Update(Me.UsersDS.Users)
        End Sub
    Monday, October 27, 2008 4:50 PM

Answers

  • When working with DataAdapters, there is usually an insert, update and delete command associated it so that when you are done with changes, that command will execute with new values.

     

    All these commands are parameterized and automatically generated. But in certain cases, like where you did not have any primary key in the projected set, it is impossible to have a right update/delete command, cause it's not unique.

     

    Check the update/delete/insert commands of your DataAdapter, see if they are set properly. In your case they seem to be null thus the error.

     

    Make sure you have unique column in the set for these commands to work right.

    In addition to this, if you really want to avoid this fuss, use ResultSet, it is much faster and better with its own row tracking. Moreover it gives you option of TableDirect Commands.

    • Marked as answer by DigitalFusion Monday, February 16, 2009 6:50 PM
    Friday, November 07, 2008 8:56 PM
    Answerer

All replies

  • Ok, I have narrowed this down to only happening on an UPDATE.  Adding a new record works fine.  I even completely wiped it off the form and started over. 

    If I go into the Dataset in the properties of the tableadapter, if "updatecommand" is set to none, then i get the expected error:

    "Update requires a valid UpdateCommand when passed DataRow collection with modified rows."

    If I then go back into the properties of the tableadapter and select "new" for the update command and run the app again I get the orginal error which started this post:

    "Prepare: CommandText property has not been initialized"









    Wednesday, October 29, 2008 4:32 PM
  • Probably need a code sample to diagnose, but are you specifying your contrainst values if there are any?

     

    Vinc

     

    Thursday, October 30, 2008 6:53 AM
  • No, I literally simply placed a "details" list on the form from teh DataSources pane, deleted the bindingnavigator, and added a button called "ButtonSaveUser"  and a button called "ButtonAddUser"

    Private Sub ButtonSaveUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSaveUser.Click
            Me.Validate()
            Me.UsersBindingSource.EndEdit()
            Me.UsersTableAdapter.Update(Me.UsersDS.Users)
    End Sub


    Private Sub ButtonAddUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAddUser.Click
            UsersBindingSource.AddNew()
    End Sub

    I dont understand why this crashes on update when insert is working just fine. 
    Monday, November 03, 2008 7:03 PM
  • what the heck is going on here?  Insert is the only thing that works.  I just tried a delete, and it gives the same error:

    Private Sub ButtonDeleteUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDeleteUser.Click
    UsersBindingSource.RemoveCurrent()
    Me.Validate()
    UsersBindingSource.EndEdit()
    UsersTableAdapter.Update(UsersDS.Users)
    End Sub


    Prepare: CommandText property has not been initialized

    Its on UsersTableAdapter.Update(UsersDS.Users)



    Monday, November 03, 2008 10:59 PM
  • Ok... I added a completely new form to my app called UsersDebug. 

    I went into the DataSources pane, expanded the "UsersDS" dataset, then selected a "Details" list from the dropdown.  I then placed the "Details" list onto the new form.

    Here is the code:

    Public Class UsersDebug
        Private Sub UsersBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UsersBindingNavigatorSaveItem.Click
            Me.Validate()
            Me.UsersBindingSource.EndEdit()
            Me.UsersTableAdapter.Update(Me.UsersDS.Users)

        End Sub

        Private Sub UsersDebug_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'UsersDS.Users' table. You can move, or remove it, as needed.
            Me.UsersTableAdapter.Fill(Me.UsersDS.Users)
        End Sub
    End Class


    If I try and delete a user using the BindingNavigator "delete" button, then click the BindingNavigator "save" button, I get the same exact error:

    Prepare: CommandText property has not been initialized





    Should I just completely delete this Dataset and start over?  I dont know what else to try.
    Monday, November 03, 2008 11:12 PM
  • I did it... i added a new Dataset for the "Users" table.  Created another new form for debugging like the above post, and I get the exact same error on a delete or update... adding a new record works just fine..





    %(#$_%#$@^&$#)%(#@^%)#!_+%#()!$_^+%($&+%$#_%^#@$+_^*_+%)!@#%(&+$%_#@!^($#_+&*(
    Monday, November 03, 2008 11:24 PM
  • When working with DataAdapters, there is usually an insert, update and delete command associated it so that when you are done with changes, that command will execute with new values.

     

    All these commands are parameterized and automatically generated. But in certain cases, like where you did not have any primary key in the projected set, it is impossible to have a right update/delete command, cause it's not unique.

     

    Check the update/delete/insert commands of your DataAdapter, see if they are set properly. In your case they seem to be null thus the error.

     

    Make sure you have unique column in the set for these commands to work right.

    In addition to this, if you really want to avoid this fuss, use ResultSet, it is much faster and better with its own row tracking. Moreover it gives you option of TableDirect Commands.

    • Marked as answer by DigitalFusion Monday, February 16, 2009 6:50 PM
    Friday, November 07, 2008 8:56 PM
    Answerer
  • Hello

    I've the same exact problem. Did you manage to fix it? I need help solving this...

    Best regards

    Luis
    Tuesday, November 24, 2009 11:17 AM
  • Buenos Días, tuve el mismo problema que me aparecía el mensaje  Prepare: CommandText property has not been initialized durante la ejecución del procedimiento

    private void PasarDatosAPrestatarios()
            {
                formPrestatarios fp = new formPrestatarios();

                int i;
                //MODIFICACION de Paciente
                if (VarPubl.PrestatarioExiste && VarPubl.pacienteModificar)
                {
                 //  0j0 Nota Version las instrucciones de buscar Prestatario
                    //0j0 O usar el binding source abajo
                      //Utilizado para encontrar la fila en el DataGridView donde se ubica el Número del Contrato
                    // OJO OJO OJO Pendiente con la nota
                    //'NOTA.... esta trabajando bien si la forma esta abierta; si no,  da problemas
                    VarPubl.PorTodos = true; // Utilizado para que llene el DataGrid de Prestatarios
                    fp.Show();
                   // fp.prestatariosTableAdapter.Fill(this.bDFunasaludDataSet.Prestatarios);
                    i = fp.prestatariosBindingSource.Find("CedulaID", (uint)VarPubl.CedulaNum);  //BindingSource.Find("ContratoID", VarPubl.ContratoNum);
                    // Se coloca el indicador del Navegador en la fila correspondiente al número de contrato a ser modificado
                    prestatariosBindingSource.Position = i;
                    if (i != -1)
                    {

                        string direccion = txtDireccion.Text + " Paciente: " + txtPaciente.Text;
                       // fp.prestatariosDataGridView.Rows[i].Cells[0].Value = (uint)VarPubl.CedulaNum;
                        fp.prestatariosDataGridView.Rows[i].Cells[2].Value = direccion;
                        fp.Validate();
                        fp.prestatariosBindingSource.EndEdit();
                        fp.tableAdapterManager.UpdateAll(fp.bDFunasaludDataSet);
                      //  fp.prestatariosTableAdapter.Update(fp.bDFunasaludDataSet.Prestatarios);
                        fp.Close();

                    }
                }
                else
                {
                    //Si No existe prestatario pasar los datos del nuevo prestatario
                    BDFunasaludDataSet.PrestatariosRow nuevoPrestatarioRow;
                    nuevoPrestatarioRow = bDFunasaludDataSet.Prestatarios.NewPrestatariosRow();
                    i = fp.prestatariosTableAdapter.GetData().Count;
                    fp.prestatariosBindingNavigator.PositionItem.Text = Convert.ToString(i);
                    nuevoPrestatarioRow.CedulaID = Convert.ToInt32(VarPubl.CedulaNum);
                    nuevoPrestatarioRow.Nombres = txtNombres.Text;
                    nuevoPrestatarioRow.Direccion = txtDireccion.Text;
                    nuevoPrestatarioRow.TelefonoCasa = txtTelefonoCasa.Text;
                    nuevoPrestatarioRow.TelefonoMovil1 = txtTelefonoMovil1.Text;
                    nuevoPrestatarioRow.TelefonoMovil2 = txtTelefonoMovil2.Text;

                    bDFunasaludDataSet.Prestatarios.Rows.Add(nuevoPrestatarioRow);
                    i = fp.prestatariosTableAdapter.GetData().Count;
                    fp.prestatariosBindingNavigator.PositionItem.Text = Convert.ToString(i);
                }
            }

    // Como lo resolví:

      /* Si se da el error "Prepare: CommandText property has not been initialized"
       * es porque no se crearón los comandos UpdateCommand y/o DeleteCommand y/o InsertCommand y/o SelectCommand
       * Se debe editar el dataset.xsd de su base de datos
       * Escoger en la tabla respectiva (ejem. Prestatarios)
       * el TableAdapter (PrestatariosTableAdapter) presionar F4 o disparar botón
       * derecho del raton (mouse) y escoger Propiedades para crear el(los) Command(s)
       * Ejemplo: en UpdateCommand aparece (None), disparar sobre (New)
       * el programa coloca (UpdateCommand),
       * expanda y haga click en  [...] de CommandText
       * le aparecerá una ventana titulada Add Table
       * seleccione la tabla respectiva (ejemplo: Prestatarios) y presione el botón "Add"
       * le aparecerá una ventana Query Builder
       * Haga click sobre cada campo de la tabla (ejemplo []CedulaID)
       *                         _________________________________________________                 
       * y en la grilla;    |Column   |Table         |Set   |NewValue  |Filter   |Or..   |Or..  |
                                └--------------------------------------------------------------------┘        
       * le aparecerá (ej): CedulaID   Prestatarios    [v]    @p1      =@p2

       * en new Value y en Filter coloqué a @p1  y =@p2  (respectivamente)                             
       *  y se le creará la consulta:
       *                          UPDATE       Prestatarios
       *                          SET          CedulaID = @p1
       *                          WHERE       (CedulaID = @p2)
       *                          haga click en el comando Ok
       */

                      Y SE RESOLVIÓ EL PROBLEMA

    //NOTA IMPORTANTE: Si tiene que trabajar con varios campos de la tabla. Revise cuidadosamente que
                       No vaya a cometer el error de colocar algún @pX repetido; en mi caso
                       repetí el filtro "=@P6"  con el NewValue @P6 de mi campo "NúmeroTelefónico"
                       lo que me dió el error  "Infracción de simultaneidad: UpdateCommand afectó a 0 de los 1 registros esperados."

    AL CAMBIAR =@P6 a =@P7 se resolvió este problema de Infracción.

    Saturday, November 12, 2016 3:19 PM