3

In my Winforms C# application, I have fields with Int data type and they are set to accept null values in SQL Server database (allow nulls).

In the forms I have some textboxes which are bound to those int data type fields. If I don't enter anything while creating a new record, it accepts. If I enter a number in the textbox, it also accepts it, and then if I delete it, it doesn’t accept it anymore and even doesn't allow me to move to the next field.

If I set its value as null or "" through code, it simply ignores and does not even update changes which I made in other non int text fields.

I am using following method to update.

this.Validate();
this.itemsbindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.sBSDBDataSet);

What can I do for the textbox to accept null values?

I have tried following.

IDTextBox.Text = "";
IDTextBox.Text = null;
ammar
  • 71
  • 9
  • What are you using as `DataSource` for the `itemsBindingSource`? – Ivan Stoev Sep 10 '16 at 10:40
  • The DataSource is 'sBSDBDataSet'. – ammar Sep 10 '16 at 10:44
  • 2
    But what is that `sBSDBDataSet`? Remember we don't have your code, look at the question, this is all what we see. – Ivan Stoev Sep 10 '16 at 10:48
  • Hi Ivan, sBSDBDataSe is the dataset name which I am using on the form. I have added DataSource in visual studio windowsform project using 'Add New Data Source' from sqlserver database. Then I simply dragged this added DataSet onto the form and renamed it as 'sBSDBDataSet'. Then I added 'itemsbindingSource', itemsTableAdapter and tableAdapterManager onto the form using toolbox controls. So in this way, my form is loaded with data using this.itemsTableAdapter.Fill(this.sBSDBDataSet.Items); method. Then I simply added update code which I shared above. – ammar Sep 10 '16 at 11:05
  • Ok. The problem is most likely caused by the data bindings. How do you create them - using the designer or via code? – Ivan Stoev Sep 10 '16 at 11:10
  • using the designer. But if the problem is with binding, why it updates other non-Int fields correctly.The issue is with all Int-fileds on the form. – ammar Sep 10 '16 at 11:15
  • Because `int` does not support `null`. If you were binding to some custom object, you would use `int?` (nullable int) property. But `DataTable` columns report `int` type. Can you open the .designer.cs file, find one of the problematic text box bindings (shoud be something like `textbox.DataBindings.Add(...`) and paste it here? – Ivan Stoev Sep 10 '16 at 11:22
  • this.IDTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.itemsbindingSource, "PictureID", true)); this.IDTextBox.Location = new System.Drawing.Point(291, 258); this.IDTextBox.Name = "IDTextBox"; this.IDTextBox.Size = new System.Drawing.Size(58, 21); this.IDTextBox.TabIndex = 88; – ammar Sep 10 '16 at 11:31
  • Replace the first line (`this.IDTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.itemsbindingSource, "PictureID", true));` with `this.IDTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.itemsbindingSource, "PictureID", true, System.Windows.Forms.DataSourceUpdateMode.OnValidation, ""));` – Ivan Stoev Sep 10 '16 at 11:37
  • From the search so far, I know that I can not enter null in Int data field. But then how to solve my issue. This is what I was trying to do initially. private void btnRmvPic_Click(object sender, EventArgs e) { if (picturePictureBox.Image != null) { IDTextBox.Text = null; } } based on that, I need null value to be updated in database. This ID is actually the Unique ID from Pictures table. And If I set it to Null, the picture will not be shown in the form, which is what I am trying to do. I can add NULL directly in sql table, but not able to do it through winform. – ammar Sep 10 '16 at 11:39
  • let me try it out – ammar Sep 10 '16 at 11:40
  • Dear Ivan, It worked perfectly. Thank you so much. But please tell me do I have to do this for all other Int fields on the form. I probably have 40-50 such fields. Is there is easy way to do this or the way how I bind data using designer. – ammar Sep 10 '16 at 11:46
  • With code it's quite easy. I didn't find any way to do that with designer. You can select the textbox, go to Properties -> Data Bindings and click Advanced. It will open a dialog where you will see a text box called "Null value". However I can't enter empty string there, I mean I can, but it will not generate the code needed. Also will be quite annoying to go through all text boxes. Let me think a bit what can be done... – Ivan Stoev Sep 10 '16 at 11:51

3 Answers3

3

I have tried following with the help of above solutions (specially Mr. Ivan) and this is how it worked out.

To clear the int field on the form:

IDTextBox.Text = String.Empty;

Then on Designer.cs file of the form, as suggested by Mr. Ivan, I searched for 'IDtextbox.DataBindings.Add' and replaced

this.IDTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.itemsbindingSource, "PictureID", true));

with

this.IDTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.itemsbindingSource, "PictureID", true, System.Windows.Forms.DataSourceUpdateMode.OnValidation, ""));

It took me a whole day to search and finally I posted my problem here, and it got solved in 1 hour.

ammar
  • 71
  • 9
2

This seems to be one of the WF data binding bugs. I can't say what exactly is causing it, but in order to make it work one should set Binding.NullValue property to "" (empty string, the default is null).

I couldn't find a way to do that in the designer, and also it would be quite annoying to locate all text boxes needed. So I would suggest you the following quick-and-dirty approach. Create a helper method like this:

public static class ControlExtensions
{
    public static void FixTextBoxBindings(this Control control)
    {
        if (control is TextBox)
        {
            foreach (Binding binding in control.DataBindings)
                if (binding.NullValue == null) binding.NullValue = "";
        }
        foreach (Control child in control.Controls)
            child.FixTextBoxBindings();
    }
}

and then simply include the following in your form Load event:

this.FixTextBoxBindings();
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
1

TextBox dont accept null value.

You can check if it null and set String.Empty;

If(dbValue == null)
{
   IDTextBox.Text = String.Empty;
}
else
{
  // here set value to your textbox
}
M. Wiśnicki
  • 6,094
  • 3
  • 23
  • 28
  • Hi mwisnicki. I am getting two errors on this one. 1. dbValue-> The name dbValue does not exist in the current context. 2. String.Empty() -> Non-invokable member 'String.Empty' can not be used like a method. Am I missing something? – ammar Sep 10 '16 at 10:37
  • "dbValue" is a variable which you try set to your textbox, you cant set null to textbox, so you have to check it if it is a null set empty string. – M. Wiśnicki Sep 10 '16 at 10:40
  • Hi mwisnicki. I have tried this but still does not work. var dbValue = (string)null; if(dbValue == null) { IDTextBox.Text = String.Empty; } else { // } – ammar Sep 10 '16 at 10:57
  • Show all your code. Because i dont know what you try do. You cant cast and set to string null. Show what you do – M. Wiśnicki Sep 10 '16 at 11:00
  • This is what I was trying to do initially. private void btnRmvPic_Click(object sender, EventArgs e) { if (picturePictureBox.Image != null) { IDTextBox.Text = null; } } based on that, I need null value to be updated in database. This ID is actually the Unique ID from Pictures table. And If I set it to Null, the picture will not be shown in the form, which is what I am trying to do. – ammar Sep 10 '16 at 11:12
  • So set String.Empty to IDTextBox.Text and when you will saving it to db check if it empty (null) or not(some int). – M. Wiśnicki Sep 10 '16 at 11:17
  • I tried IDTextBox.Text = String.Empty; but no success. e.g. there was 75 before. after running this cose and updating record, value is supposed to be NULL, but it is still 75. I can add NULL directly in sql table, but not able to do it through winform. – ammar Sep 10 '16 at 11:24