BindingContext problems

D

Depesz

Hi
My english is not perfect...
I create a simple form by the Data Form Wizard. In last window of Data Form
Wizard I chose display style "Single record in individual controls". After
compilng the code my application works fine.
Next I add a DataGrid control to my form generated by the Data Form Wizard.
I bound the DataGrid with a created DataSet. I set DataGrid property
ReadOnly as True.
When I run my application and first select any DataGrid's row and next I
click "Add" button on my form I receive error that I try to input into my
dataset NULL value.
When I run my application and DataGrid is not active control on my form, if
I insert new value into DataSet, I see inserted values twice in the
DataGrid.
I think my problems are with BindingContext but I'm not sure.

Here is the code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace WebShopAdmin
{
/// <summary>
/// Summary description for kategorie.
/// </summary>

public class kategorie : System.Windows.Forms.Form
{
private System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
private System.Data.OleDb.OleDbCommand oleDbInsertCommand1;
private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1;
private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1;
private WebShopAdmin.kategorieDataSet objkategorieDataSet;
private System.Data.OleDb.OleDbConnection oleDbConnection1;
private System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1;
private System.Windows.Forms.Button btnUpdate;
private System.Windows.Forms.Button btnCancelAll;
private System.Windows.Forms.Label lblNazwaKategorii;
private System.Windows.Forms.TextBox editNazwaKategorii;
private System.Windows.Forms.Button btnNavFirst;
private System.Windows.Forms.Button btnNavPrev;
private System.Windows.Forms.Label lblNavLocation;
private System.Windows.Forms.Button btnNavNext;
private System.Windows.Forms.Button btnLast;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnDelete;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.DataGridTableStyle dataGridTableStyle1;
private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn1;
private System.Windows.Forms.Button button1;

/// <summary>
/// Required designer variable.
/// </summary>

private System.ComponentModel.Container components = null;
public kategorie()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();

this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();

this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand();

this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand();

this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand();

this.objkategorieDataSet = new WebShopAdmin.kategorieDataSet();

this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter();

this.btnUpdate = new System.Windows.Forms.Button();

this.btnCancelAll = new System.Windows.Forms.Button();

this.lblNazwaKategorii = new System.Windows.Forms.Label();

this.editNazwaKategorii = new System.Windows.Forms.TextBox();

this.btnNavFirst = new System.Windows.Forms.Button();

this.btnNavPrev = new System.Windows.Forms.Button();

this.lblNavLocation = new System.Windows.Forms.Label();

this.btnNavNext = new System.Windows.Forms.Button();

this.btnLast = new System.Windows.Forms.Button();

this.btnAdd = new System.Windows.Forms.Button();

this.btnDelete = new System.Windows.Forms.Button();

this.btnCancel = new System.Windows.Forms.Button();

this.dataGrid1 = new System.Windows.Forms.DataGrid();

this.dataGridTableStyle1 = new System.Windows.Forms.DataGridTableStyle();

this.dataGridTextBoxColumn1 = new
System.Windows.Forms.DataGridTextBoxColumn();

this.button1 = new System.Windows.Forms.Button();

((System.ComponentModel.ISupportInitialize)(this.objkategorieDataSet)).Begin
Init();

((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();

this.SuspendLayout();

//

// oleDbSelectCommand1

//

this.oleDbSelectCommand1.CommandText = "SELECT KategoriaId, NazwaKategorii
FROM Kategoria";

this.oleDbSelectCommand1.Connection = this.oleDbConnection1;

//

// oleDbConnection1

//

this.oleDbConnection1.ConnectionString = @"Provider=SQLOLEDB.1;Integrated
Security=SSPI;Persist Security Info=False;Initial Catalog=WebShop;Data
Source=MATRIX;Use Procedure for Prepare=1;Auto Translate=True;Packet
Size=4096;Workstation ID=MATRIX;Use Encryption for Data=False;Tag with
column collation when possible=False";

//

// oleDbInsertCommand1

//

this.oleDbInsertCommand1.CommandText = "INSERT INTO
Kategoria(NazwaKategorii) VALUES (?); SELECT KategoriaId, NazwaKatego" +

"rii FROM Kategoria WHERE (KategoriaId = @@IDENTITY)";

this.oleDbInsertCommand1.Connection = this.oleDbConnection1;

this.oleDbInsertCommand1.Parameters.Add(new
System.Data.OleDb.OleDbParameter("NazwaKategorii",
System.Data.OleDb.OleDbType.VarChar, 100, "NazwaKategorii"));

//

// oleDbUpdateCommand1

//

this.oleDbUpdateCommand1.CommandText = "UPDATE Kategoria SET NazwaKategorii
= ? WHERE (KategoriaId = ?) AND (NazwaKategor" +

"ii = ?); SELECT KategoriaId, NazwaKategorii FROM Kategoria WHERE
(KategoriaId = " +

"?)";

this.oleDbUpdateCommand1.Connection = this.oleDbConnection1;

this.oleDbUpdateCommand1.Parameters.Add(new
System.Data.OleDb.OleDbParameter("NazwaKategorii",
System.Data.OleDb.OleDbType.VarChar, 100, "NazwaKategorii"));

this.oleDbUpdateCommand1.Parameters.Add(new
System.Data.OleDb.OleDbParameter("Original_KategoriaId",
System.Data.OleDb.OleDbType.Integer, 4,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "KategoriaId", System.Data.DataRowVersion.Original,
null));

this.oleDbUpdateCommand1.Parameters.Add(new
System.Data.OleDb.OleDbParameter("Original_NazwaKategorii",
System.Data.OleDb.OleDbType.VarChar, 100,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "NazwaKategorii", System.Data.DataRowVersion.Original,
null));

this.oleDbUpdateCommand1.Parameters.Add(new
System.Data.OleDb.OleDbParameter("Select_KategoriaId",
System.Data.OleDb.OleDbType.Integer, 4, "KategoriaId"));

//

// oleDbDeleteCommand1

//

this.oleDbDeleteCommand1.CommandText = "DELETE FROM Kategoria WHERE
(KategoriaId = ?) AND (NazwaKategorii = ?)";

this.oleDbDeleteCommand1.Connection = this.oleDbConnection1;

this.oleDbDeleteCommand1.Parameters.Add(new
System.Data.OleDb.OleDbParameter("Original_KategoriaId",
System.Data.OleDb.OleDbType.Integer, 4,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "KategoriaId", System.Data.DataRowVersion.Original,
null));

this.oleDbDeleteCommand1.Parameters.Add(new
System.Data.OleDb.OleDbParameter("Original_NazwaKategorii",
System.Data.OleDb.OleDbType.VarChar, 100,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "NazwaKategorii", System.Data.DataRowVersion.Original,
null));

//

// objkategorieDataSet

//

this.objkategorieDataSet.DataSetName = "kategorieDataSet";

this.objkategorieDataSet.Locale = new
System.Globalization.CultureInfo("pl-PL");

this.objkategorieDataSet.Namespace =
"http://www.tempuri.org/kategorieDataSet.xsd";

//

// oleDbDataAdapter1

//

this.oleDbDataAdapter1.DeleteCommand = this.oleDbDeleteCommand1;

this.oleDbDataAdapter1.InsertCommand = this.oleDbInsertCommand1;

this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1;

this.oleDbDataAdapter1.TableMappings.AddRange(new
System.Data.Common.DataTableMapping[] {

new System.Data.Common.DataTableMapping("Table", "Kategoria", new
System.Data.Common.DataColumnMapping[] {

new System.Data.Common.DataColumnMapping("KategoriaId", "KategoriaId"),

new System.Data.Common.DataColumnMapping("NazwaKategorii",
"NazwaKategorii")})});

this.oleDbDataAdapter1.UpdateCommand = this.oleDbUpdateCommand1;

//

// btnUpdate

//

this.btnUpdate.Location = new System.Drawing.Point(101, 142);

this.btnUpdate.Name = "btnUpdate";

this.btnUpdate.TabIndex = 1;

this.btnUpdate.Text = "&Update";

this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);

//

// btnCancelAll

//

this.btnCancelAll.Location = new System.Drawing.Point(371, 142);

this.btnCancelAll.Name = "btnCancelAll";

this.btnCancelAll.TabIndex = 2;

this.btnCancelAll.Text = "Ca&ncel All";

this.btnCancelAll.Click += new System.EventHandler(this.btnCancelAll_Click);

//

// lblNazwaKategorii

//

this.lblNazwaKategorii.Location = new System.Drawing.Point(24, 24);

this.lblNazwaKategorii.Name = "lblNazwaKategorii";

this.lblNazwaKategorii.TabIndex = 5;

this.lblNazwaKategorii.Text = "Nazwa kategorii";

//

// editNazwaKategorii

//

this.editNazwaKategorii.DataBindings.Add(new
System.Windows.Forms.Binding("Text", this.objkategorieDataSet,
"Kategoria.NazwaKategorii"));

this.editNazwaKategorii.Location = new System.Drawing.Point(136, 24);

this.editNazwaKategorii.Name = "editNazwaKategorii";

this.editNazwaKategorii.Size = new System.Drawing.Size(216, 20);

this.editNazwaKategorii.TabIndex = 6;

this.editNazwaKategorii.Text = "";

//

// btnNavFirst

//

this.btnNavFirst.Location = new System.Drawing.Point(106, 77);

this.btnNavFirst.Name = "btnNavFirst";

this.btnNavFirst.Size = new System.Drawing.Size(40, 23);

this.btnNavFirst.TabIndex = 7;

this.btnNavFirst.Text = "<<";

this.btnNavFirst.Click += new System.EventHandler(this.btnNavFirst_Click);

//

// btnNavPrev

//

this.btnNavPrev.Location = new System.Drawing.Point(146, 77);

this.btnNavPrev.Name = "btnNavPrev";

this.btnNavPrev.Size = new System.Drawing.Size(35, 23);

this.btnNavPrev.TabIndex = 8;

this.btnNavPrev.Text = "<";

this.btnNavPrev.Click += new System.EventHandler(this.btnNavPrev_Click);

//

// lblNavLocation

//

this.lblNavLocation.BackColor = System.Drawing.Color.White;

this.lblNavLocation.Location = new System.Drawing.Point(181, 77);

this.lblNavLocation.Name = "lblNavLocation";

this.lblNavLocation.Size = new System.Drawing.Size(95, 23);

this.lblNavLocation.TabIndex = 9;

this.lblNavLocation.Text = "No Records";

this.lblNavLocation.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;

//

// btnNavNext

//

this.btnNavNext.Location = new System.Drawing.Point(276, 77);

this.btnNavNext.Name = "btnNavNext";

this.btnNavNext.Size = new System.Drawing.Size(35, 23);

this.btnNavNext.TabIndex = 10;

this.btnNavNext.Text = ">";

this.btnNavNext.Click += new System.EventHandler(this.btnNavNext_Click);

//

// btnLast

//

this.btnLast.Location = new System.Drawing.Point(311, 77);

this.btnLast.Name = "btnLast";

this.btnLast.Size = new System.Drawing.Size(40, 23);

this.btnLast.TabIndex = 11;

this.btnLast.Text = ">>";

this.btnLast.Click += new System.EventHandler(this.btnLast_Click);

//

// btnAdd

//

this.btnAdd.Location = new System.Drawing.Point(11, 142);

this.btnAdd.Name = "btnAdd";

this.btnAdd.TabIndex = 12;

this.btnAdd.Text = "&Add";

this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);

//

// btnDelete

//

this.btnDelete.Location = new System.Drawing.Point(191, 142);

this.btnDelete.Name = "btnDelete";

this.btnDelete.TabIndex = 13;

this.btnDelete.Text = "&Delete";

this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);

//

// btnCancel

//

this.btnCancel.Location = new System.Drawing.Point(281, 142);

this.btnCancel.Name = "btnCancel";

this.btnCancel.TabIndex = 14;

this.btnCancel.Text = "&Cancel";

this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);

//

// dataGrid1

//

this.dataGrid1.DataMember = "Kategoria";

this.dataGrid1.DataSource = this.objkategorieDataSet;

this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;

this.dataGrid1.Location = new System.Drawing.Point(12, 192);

this.dataGrid1.Name = "dataGrid1";

this.dataGrid1.Size = new System.Drawing.Size(432, 160);

this.dataGrid1.TabIndex = 15;

this.dataGrid1.TableStyles.AddRange(new
System.Windows.Forms.DataGridTableStyle[] {

this.dataGridTableStyle1});

//

// dataGridTableStyle1

//

this.dataGridTableStyle1.AllowSorting = false;

this.dataGridTableStyle1.AlternatingBackColor =
System.Drawing.Color.LightGray;

this.dataGridTableStyle1.DataGrid = this.dataGrid1;

this.dataGridTableStyle1.GridColumnStyles.AddRange(new
System.Windows.Forms.DataGridColumnStyle[] {

this.dataGridTextBoxColumn1});

this.dataGridTableStyle1.HeaderForeColor =
System.Drawing.SystemColors.ControlText;

this.dataGridTableStyle1.MappingName = "Kategoria";

this.dataGridTableStyle1.ReadOnly = true;

//

// dataGridTextBoxColumn1

//

this.dataGridTextBoxColumn1.Format = "";

this.dataGridTextBoxColumn1.FormatInfo = null;

this.dataGridTextBoxColumn1.HeaderText = "Kategoria";

this.dataGridTextBoxColumn1.MappingName = "NazwaKategorii";

this.dataGridTextBoxColumn1.ReadOnly = true;

this.dataGridTextBoxColumn1.Width = 375;

//

// button1

//

this.button1.Location = new System.Drawing.Point(368, 32);

this.button1.Name = "button1";

this.button1.TabIndex = 16;

this.button1.Text = "button1";

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// kategorie

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(456, 382);

this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.button1,

this.dataGrid1,

this.btnUpdate,

this.btnCancelAll,

this.lblNazwaKategorii,

this.editNazwaKategorii,

this.btnNavFirst,

this.btnNavPrev,

this.lblNavLocation,

this.btnNavNext,

this.btnLast,

this.btnAdd,

this.btnDelete,

this.btnCancel});

this.Name = "kategorie";

this.Text = "Kategorie";

((System.ComponentModel.ISupportInitialize)(this.objkategorieDataSet)).EndIn
it();

((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();

this.ResumeLayout(false);

}

#endregion

public void FillDataSet(WebShopAdmin.kategorieDataSet dataSet)

{

// Turn off constraint checking before the dataset is filled.

// This allows the adapters to fill the dataset without concern

// for dependencies between the tables.

dataSet.EnforceConstraints = false;

try

{

// Open the connection.

this.oleDbConnection1.Open();

// Attempt to fill the dataset through the OleDbDataAdapter1.

this.oleDbDataAdapter1.Fill(dataSet);

}

catch (System.Exception fillException)

{

// Add your error handling code here.

throw fillException;

}

finally

{

// Turn constraint checking back on.

dataSet.EnforceConstraints = true;

// Close the connection whether or not the exception was thrown.

this.oleDbConnection1.Close();

}

}

public void UpdateDataSource(WebShopAdmin.kategorieDataSet ChangedRows)

{

try

{

// The data source only needs to be updated if there are changes pending.

if ((ChangedRows != null))

{

// Open the connection.

this.oleDbConnection1.Open();

// Attempt to update the data source.

oleDbDataAdapter1.Update(ChangedRows);

}

}

catch (System.Exception updateException)

{

// Add your error handling code here.

throw updateException;

}

finally

{

// Close the connection whether or not the exception was thrown.

this.oleDbConnection1.Close();

}

}

public void LoadDataSet()

{

// Create a new dataset to hold the records returned from the call to
FillDataSet.

// A temporary dataset is used because filling the existing dataset would

// require the databindings to be rebound.

WebShopAdmin.kategorieDataSet objDataSetTemp;

objDataSetTemp = new WebShopAdmin.kategorieDataSet();

try

{

// Attempt to fill the temporary dataset.

this.FillDataSet(objDataSetTemp);

}

catch (System.Exception eFillDataSet)

{

// Add your error handling code here.

throw eFillDataSet;

}

try

{

// Empty the old records from the dataset.

objkategorieDataSet.Clear();

// Merge the records into the main dataset.

objkategorieDataSet.Merge(objDataSetTemp);

}

catch (System.Exception eLoadMerge)

{

// Add your error handling code here.

throw eLoadMerge;

}

}

public void UpdateDataSet()

{

// Create a new dataset to hold the changes that have been made to the main
dataset.

WebShopAdmin.kategorieDataSet objDataSetChanges = new
WebShopAdmin.kategorieDataSet();

// Stop any current edits.

this.BindingContext[objkategorieDataSet,"Kategoria"].EndCurrentEdit();

// Get the changes that have been made to the main dataset.

objDataSetChanges =
((WebShopAdmin.kategorieDataSet)(objkategorieDataSet.GetChanges()));

// Check to see if any changes have been made.

if ((objDataSetChanges != null))

{

try

{

// There are changes that need to be made, so attempt to update the
datasource by

// calling the update method and passing the dataset and any parameters.

this.UpdateDataSource(objDataSetChanges);

objkategorieDataSet.Merge(objDataSetChanges);

objkategorieDataSet.AcceptChanges();

}

catch (System.Exception eUpdate)

{

// Add your error handling code here.

throw eUpdate;

}

// Add your code to check the returned dataset for any errors that may have
been

// pushed into the row object's error.

}

}

private void btnCancelAll_Click(object sender, System.EventArgs e)

{

this.objkategorieDataSet.RejectChanges();

}

private void objkategorieDataSet_PositionChanged()

{

this.lblNavLocation.Text =
((((this.BindingContext[objkategorieDataSet,"Kategoria"].Position +
1)).ToString() + " of ")

+ this.BindingContext[objkategorieDataSet,"Kategoria"].Count.ToString());

}

private void btnNavNext_Click(object sender, System.EventArgs e)

{

this.BindingContext[objkategorieDataSet,"Kategoria"].Position =
(this.BindingContext[objkategorieDataSet,"Kategoria"].Position + 1);

this.objkategorieDataSet_PositionChanged();

}

private void btnNavPrev_Click(object sender, System.EventArgs e)

{

this.BindingContext[objkategorieDataSet,"Kategoria"].Position =
(this.BindingContext[objkategorieDataSet,"Kategoria"].Position - 1);

this.objkategorieDataSet_PositionChanged();

}

private void btnLast_Click(object sender, System.EventArgs e)

{

this.BindingContext[objkategorieDataSet,"Kategoria"].Position =
(this.objkategorieDataSet.Tables["Kategoria"].Rows.Count - 1);

this.objkategorieDataSet_PositionChanged();

}

private void btnNavFirst_Click(object sender, System.EventArgs e)

{

this.BindingContext[objkategorieDataSet,"Kategoria"].Position = 0;

this.objkategorieDataSet_PositionChanged();

}

private void btnLoad_Click(object sender, System.EventArgs e)

{

try

{

// Attempt to load the dataset.

this.LoadDataSet();

}

catch (System.Exception eLoad)

{

// Add your error handling code here.

// Display error message, if any.

System.Windows.Forms.MessageBox.Show(eLoad.Message);

}

this.objkategorieDataSet_PositionChanged();

}

private void btnUpdate_Click(object sender, System.EventArgs e)

{

try

{

// Attempt to update the datasource.

this.UpdateDataSet();

}

catch (System.Exception eUpdate)

{

// Add your error handling code here.

// Display error message, if any.

System.Windows.Forms.MessageBox.Show(eUpdate.Message);

}

this.objkategorieDataSet_PositionChanged();

}

private void btnAdd_Click(object sender, System.EventArgs e)

{

try

{

// Clear out the current edits

this.BindingContext[objkategorieDataSet,"Kategoria"].EndCurrentEdit();

this.BindingContext[objkategorieDataSet,"Kategoria"].AddNew();


}

catch (System.Exception eEndEdit)

{

System.Windows.Forms.MessageBox.Show(eEndEdit.Message);

}

this.objkategorieDataSet_PositionChanged();

}

private void btnDelete_Click(object sender, System.EventArgs e)

{

if ((this.BindingContext[objkategorieDataSet,"Kategoria"].Count > 0))

{

this.BindingContext[objkategorieDataSet,"Kategoria"].RemoveAt(this.BindingCo
ntext[objkategorieDataSet,"Kategoria"].Position);

this.objkategorieDataSet_PositionChanged();

}

}

private void btnCancel_Click(object sender, System.EventArgs e)

{

this.BindingContext[objkategorieDataSet,"Kategoria"].CancelCurrentEdit();

this.objkategorieDataSet_PositionChanged();

}

private void button1_Click(object sender, System.EventArgs e)

{

try

{

// Attempt to load the dataset.

this.LoadDataSet();

}

catch (System.Exception eLoad)

{

// Add your error handling code here.

// Display error message, if any.

System.Windows.Forms.MessageBox.Show(eLoad.Message);

}

this.objkategorieDataSet_PositionChanged();

}

}

}
 
D

Depesz

private void btnAdd_Click(object sender, System.EventArgs e)
{
try
{
// Clear out the current edits
this.BindingContext[objkategorieDataSet,"Kategoria"].EndCurrentEdit();
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I think here is my problem. When I click "Add" button and I change the
active control from DataGrid to Edit Box I receive an error that I try to
insert null value into my DataSet.But I dont know how to fix it.
this.BindingContext[objkategorieDataSet,"Kategoria"].AddNew();
}
catch (System.Exception eEndEdit)
{
System.Windows.Forms.MessageBox.Show(eEndEdit.Message);
}
this.objkategorieDataSet_PositionChanged();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top