jimbobmcgee
asked on
BindingNavigator problems: column cannot be null
I have an Oracle database, which I access through a typed dataset.
I have some bound fields and a BindingNavigator on a form (created with the drag/drop functions in VS2005).
When I run the project and click the 'Add New' button on the navigator, it adds one to the internal list and allows editing.
If I click the 'Add New' button again, it throws an exception at the Application.Run(Form1) level, saying one of my columns cannot be null.
I have Googled the problem and come up with other examples: http://groups.google.com/group/microsoft.public.dotnet.framework.windowsforms.databinding/browse_thread/thread/83602e5caf7cc4a8/ff35910ef8f7a755?lnk=st&q=%2B%22BindingNavigator%22+%2B%22add+button%22&rnum=1#ff35910ef8f7a755
I have also tried to add ErrorProvider handling but find that the suggested event is not fired:
http://msdn2.microsoft.com/en-us/ms171930.aspx
I think it has to do with the EndEdit() method of the BindingSource, but I can find no resolution.
How can I trap this error efficiently?
J
I have some bound fields and a BindingNavigator on a form (created with the drag/drop functions in VS2005).
When I run the project and click the 'Add New' button on the navigator, it adds one to the internal list and allows editing.
If I click the 'Add New' button again, it throws an exception at the Application.Run(Form1) level, saying one of my columns cannot be null.
I have Googled the problem and come up with other examples: http://groups.google.com/group/microsoft.public.dotnet.framework.windowsforms.databinding/browse_thread/thread/83602e5caf7cc4a8/ff35910ef8f7a755?lnk=st&q=%2B%22BindingNavigator%22+%2B%22add+button%22&rnum=1#ff35910ef8f7a755
I have also tried to add ErrorProvider handling but find that the suggested event is not fired:
http://msdn2.microsoft.com/en-us/ms171930.aspx
I think it has to do with the EndEdit() method of the BindingSource, but I can find no resolution.
How can I trap this error efficiently?
J
ASKER
I understand that I can't enter a null, where my column is not nullable, but the exception is raised in such a way that it seems untrappable.
As an example, I can do the following in the Click event handler for the BindingNavigator's 'Save' button, like so:
try
{
myBindSource.EndEdit();
myTableAdapter.Update(myDa taTable);
MessageBox.Show("Update successful");
}
catch (Exception ex)
{
MessageBox.Show(string.For mat("Updat e failed - {0}", ex.Message));
}
but, it seems stupid to try:
try
{
Application.Run(new myForm);
}
catch (Exception ex)
{
MessageBox.Show(string.For mat("Gener al failure - {0}", ex.Message));
}
because, by this point, the form has already been destroyed.
Is this some kind of oversight by Microsoft in its develoment of the BindingNavigator object, or am I handling the whole thing wrong?
J.
As an example, I can do the following in the Click event handler for the BindingNavigator's 'Save' button, like so:
try
{
myBindSource.EndEdit();
myTableAdapter.Update(myDa
MessageBox.Show("Update successful");
}
catch (Exception ex)
{
MessageBox.Show(string.For
}
but, it seems stupid to try:
try
{
Application.Run(new myForm);
}
catch (Exception ex)
{
MessageBox.Show(string.For
}
because, by this point, the form has already been destroyed.
Is this some kind of oversight by Microsoft in its develoment of the BindingNavigator object, or am I handling the whole thing wrong?
J.
ASKER
So far, I am preventing the second click, by extending the BindingNavigator as follows:
public class ExtendedBindingNavigator : BindingNavigator
{
public ExtendedBindingNavigator()
{
this.ItemClicked += new ToolStripItemClickedEventH andler(Ext endedBindi ngNavigato r_ItemClic ked);
}
private void ExtendedBindingNavigator_I temClicked (object sender, ToolStripItemClickedEventA rgs e)
{
if (sender == this.AddNewItem && this.BindingSource != null)
this.BindingSource.CancelE dit();
}
}
Seems a bit of a hack, though...
J.
public class ExtendedBindingNavigator : BindingNavigator
{
public ExtendedBindingNavigator()
{
this.ItemClicked += new ToolStripItemClickedEventH
}
private void ExtendedBindingNavigator_I
{
if (sender == this.AddNewItem && this.BindingSource != null)
this.BindingSource.CancelE
}
}
Seems a bit of a hack, though...
J.
Hello Dear
This code is not giving clear understanding. Describe in detail.
This code is not giving clear understanding. Describe in detail.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, sumix, that confirms both what I thought and what I am doing.
As such, the I feel next question is fairly obvious: What is the best way to insert new records, when the Null constraint is fired by the dataset? My INSERT SQL statement provides default values for certain null entries (incremented sequences, etc) and, as such, I do not want the end user to be able to edit these fields. These fields are, therefore, databound to Labels.
I was under the impression that, when you call MyTableAdapter.Update(MyDa taTable) the TableAdapter object would decide for itself whether to use the INSERT statement or the UPDATE one, dependant on whether the row already existed, but this seems not to be the case. The UPDATE SQL is always called, which fails the null constraints (both at the dataset and at the database, when MyDataset.EnableConstraint s = false)
What is the most efficient way to perform my insertion when 'Save' is clicked?
J.
As such, the I feel next question is fairly obvious: What is the best way to insert new records, when the Null constraint is fired by the dataset? My INSERT SQL statement provides default values for certain null entries (incremented sequences, etc) and, as such, I do not want the end user to be able to edit these fields. These fields are, therefore, databound to Labels.
I was under the impression that, when you call MyTableAdapter.Update(MyDa
What is the most efficient way to perform my insertion when 'Save' is clicked?
J.
When you click an a AddNew button a new row is added in the datatable and its RowState is 'Added'. When you call Update method of the tableadapter, depending on the RowState property, the corresponding Insert/Update/Delete command is executed for every row - this is true.
You should set default values for columns in the DataTable where is the case (dataTable1.Columns["colum
So, in Save click you could only call Update method of the tableadapter.
there is any column which is not nullable at DB and ur entering null data in that field
Or may be u r not binding this feild with any control and u insert data it remain null