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
看来您的“许可证”表没有关键字段
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.