1

I have a problem that am trying to solve for hours. Its 2 in the morning and even after reading tones of articles I still cannot solve the problem. I have c# windows app that loads data from ms access database into a dataGridView. I can succesfully load it, add new records and save it. However when I try to edit/remove rows and then save it I obtain error: Dynamic SQL generation for the DeleteCommand is not supported against a SelectCommand that does not return any key column information.

I have no idea what to do. I'd really appreciate some help!

public class UserInterfaceForm: Form
{
    //db connection
    String connString;
    OleDbDataAdapter dAdapter;
    OleDbCommandBuilder cBuilder;
    DataTable dTable = new DataTable();
    BindingSource bSource;

    //buttons
    private Button SavePermitButton;
    private Button loadPermitButton;
    private Button exitButton;
    private Button deletePermitButton;

    //GridView
    private DataGridView allPermitsDataGridView;

    public UserInterfaceForm()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        this.SavePermitButton = new System.Windows.Forms.Button();
        this.loadPermitButton = new System.Windows.Forms.Button();
        this.allPermitsDataGridView = new System.Windows.Forms.DataGridView();
        this.exitButton = new System.Windows.Forms.Button();
        this.deletePermitButton = new System.Windows.Forms.Button();
        ((System.ComponentModel.ISupportInitialize)(this.allPermitsDataGridView)).BeginInit();
        this.SuspendLayout();
        // 
        // SavePermitButton
        // 
        this.SavePermitButton.Location = new System.Drawing.Point(115, 275);
        this.SavePermitButton.Name = "SavePermitButton";
        this.SavePermitButton.Size = new System.Drawing.Size(97, 25);
        this.SavePermitButton.TabIndex = 3;
        this.SavePermitButton.Text = "Save permit";
        this.SavePermitButton.UseVisualStyleBackColor = true;
        this.SavePermitButton.Click += new System.EventHandler(this.SavePermitButton_Click);
        // 
        // loadPermitButton
        // 
        this.loadPermitButton.Location = new System.Drawing.Point(12, 275);
        this.loadPermitButton.Name = "loadPermitButton";
        this.loadPermitButton.Size = new System.Drawing.Size(97, 25);
        this.loadPermitButton.TabIndex = 4;
        this.loadPermitButton.Text = "Load permit";
        this.loadPermitButton.UseVisualStyleBackColor = true;
        this.loadPermitButton.Click += new System.EventHandler(this.readPermitButton_Click);
        // 
        // allPermitsDataGridView
        // 
        this.allPermitsDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.allPermitsDataGridView.Location = new System.Drawing.Point(12, 12);
        this.allPermitsDataGridView.Name = "allPermitsDataGridView";
        this.allPermitsDataGridView.Size = new System.Drawing.Size(839, 239);
        this.allPermitsDataGridView.TabIndex = 8;
        // 
        // exitButton
        // 
        this.exitButton.Location = new System.Drawing.Point(752, 275);
        this.exitButton.Name = "exitButton";
        this.exitButton.Size = new System.Drawing.Size(99, 25);
        this.exitButton.TabIndex = 0;
        this.exitButton.Text = "Exit";
        this.exitButton.Click += new System.EventHandler(this.cancelButton_click);
        // 
        // deletePermitButton
        // 
        this.deletePermitButton.Location = new System.Drawing.Point(218, 275);
        this.deletePermitButton.Name = "deletePermitButton";
        this.deletePermitButton.Size = new System.Drawing.Size(97, 25);
        this.deletePermitButton.TabIndex = 9;
        this.deletePermitButton.Text = "Delete permit";
        this.deletePermitButton.UseVisualStyleBackColor = true;
        this.deletePermitButton.Click += new System.EventHandler(this.deletePermitButton_Click);
        // 
        // UserInterfaceForm
        // 
        this.ClientSize = new System.Drawing.Size(886, 312);
        this.Controls.Add(this.deletePermitButton);
        this.Controls.Add(this.allPermitsDataGridView);
        this.Controls.Add(this.loadPermitButton);
        this.Controls.Add(this.SavePermitButton);
        this.Controls.Add(this.exitButton);
        this.Name = "UserInterfaceForm";
        this.Text = "Parking Permit Application";
        ((System.ComponentModel.ISupportInitialize)(this.allPermitsDataGridView)).EndInit();
        this.ResumeLayout(false);
    }

    protected void cancelButton_click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void readPermitButton_Click(object sender, EventArgs e)
    {
        connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Arek\Desktop\Parking Permit Application - Assignment 3\Assignment_3_OOPARDGM.accdb; 
        Persist Security Info=False;";
        dAdapter = new OleDbDataAdapter("Select * From Permits", connString);
        cBuilder = new OleDbCommandBuilder(dAdapter);

        dAdapter.Fill(dTable);

        bSource = new BindingSource();            
        bSource.DataSource = dTable;            
        allPermitsDataGridView.DataSource = bSource;
    }

    private void SavePermitButton_Click(object sender, EventArgs e)
    {
        dAdapter.Update(dTable);
        MessageBox.Show("Database successfully saved!");
    }

    private void deletePermitButton_Click(object sender, EventArgs e)
    {
        foreach (DataGridViewRow item in this.allPermitsDataGridView.SelectedRows)
        {
            allPermitsDataGridView.Rows.RemoveAt(item.Index);
        } 

    }       
}//class

1 Answer 1

1

It looks like your Permits table doesn't have key field

Sign up to request clarification or add additional context in comments.

3 Comments

you mean like an autonumber permits_ID etc? yes it has one.
Not autonumbers, but primary keys. Is permits_ID primary key for Permits table?
Oh God! I have created a autonumber but forgot to mark it as a primary key. Thanks a million! Now I feel embarassed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.