简体   繁体   中英

CommandText Property has not been initialized

The following code attempts to delete a selected row from a datagridview and update the database.

But it's not updating the database...it's just issuing the error, "CommandText Property has not been initialized." ...any ideas? I assume it's because it's not being bound in the beginning, but at this point I'm clueless and my head is sore.

private void deleteRow()
{
    DialogResult dr = MessageBox.Show("Are you sure you want to delete this row?", "Confirmation", 
    MessageBoxButtons.YesNo, MessageBoxIcon.Question);

    if (dr == DialogResult.Yes)
    {
        while (dataGridView1.SelectedRows.Count > 0)
        dataGridView1.Rows.Remove(dataGridView1.SelectedRows[0]);                

        try
        {
            this.Validate();
            this.tradesBindingSource.EndEdit();
            this.tradesTableAdapter.Update(this.tradesDataSet.Trades);
        }
        catch (Exception ex)
        {
            MessageBox.Show("An error occurred during the update process: " + ex);
            // Add code to handle error here.
        }

        this.tradesTableAdapter.Fill(this.tradesDataSet.Trades); // refresh table
    }
}

I deleted my table adapters, datasets, etc... and recreated the entire table. Populated it with new data, and tried to delete a row. It worked... Don't know why, but it works now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM