none
Incorrect value RRS feed

  • Question

  • Hi,

    I have a SQL Server database which has "Companies" table. It has these columns: ID (Primary Key), Code (AllowNulls = false), Name (AllowNulls = false), Address. As you can see columns "Code" and "Name" can't be empty. I've created a dataset using wizard. Then I dragged and dropped it on a form (Details view) so the form now has bounded textboxes. When I run that application, press an "AddNewItem" button on BindingNavigator and leaving all textboxes empty and press "Save" button I get an error "Column XXXX does not allow nulls". What I want is when user tries to save leaving "Code" and "Names" empty the MessageBox would appear saying which textbox has incorrect value and then focus that textbox. So how I can identify which textbox/column has incorrect value?

    Thanks.

    Friday, June 29, 2012 8:44 PM

Answers

  • Hi EuroGanG,

    Database or Dataset will not inform DataGridView that the value is invalid. Commonly, we often write validation in the set section of the properties, if the value is valid, assign the value to the property, if the value is invalid, return an invalid message. This is, so when we assign a value to an object's property, it will immediatelyi help us to validate the value. You can also post the issue in Windows Form Forum, there may some validation mechanism in the data bind control, but I'm not sure. The experts in Windows Form Forum may told you the common way to do that.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by EuroGanG Wednesday, July 4, 2012 6:14 PM
    Wednesday, July 4, 2012 6:52 AM
    Moderator

All replies

  • Hi EuroGanG,

    Welcome to MSDN Forum.

    You can realize this function in Save button, please refer to the code below.

    private void btnSave_Click(object sender, EventArgs e)
            {
                
                    if (!String.IsNullOrEmpty(txtName.Text) && !String.IsNullOrEmpty(txtAge.Text) && !String.IsNullOrEmpty(txtSex.Text) && !String.IsNullOrEmpty(txtAddress.Text))
                    {
                        using (Model1Container context = new Model1Container())
                        {
                            PersonTest pt = new PersonTest();
                            pt.Name = txtName.Text;
                            pt.Sex = txtSex.Text;
                            pt.Age = Convert.ToInt32(txtAge.Text);
                            pt.Address = txtAddress.Text;
                            context.AddToPersonTests(pt);
                            context.SaveChanges();
                        }
                    }
                    if (String.IsNullOrEmpty(txtName.Text))
                    {
                        MessageBox.Show("Name couldn't be null!");
                        txtName.Focus();
                    }
                    if (String.IsNullOrEmpty(txtSex.Text))
                    {
                        MessageBox.Show("Sex couldn't be null!");
                        txtName.Focus();
                    }
                    if (String.IsNullOrEmpty(txtAge.Text))
                    {
                        MessageBox.Show("Age couldn't be null!");
                        txtName.Focus();
                    }
                    if (String.IsNullOrEmpty(txtAddress.Text))
                    {
                        MessageBox.Show("Address couldn't be null!");
                        txtName.Focus();
                    }            
            }

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, July 2, 2012 7:54 AM
    Moderator
  • Thanks Allen. I know this way you mentioned. But I don't think this is a good idea. What if my form would have many controls which needs to be checked. And I want to check them not only if they are empty, in database some columns may have other constraints (only numeric, fixed length and others...). I don't think it is good to do this on presentation layer. I think I need to get information from database/dataset if entered value on the form is correct for that specific column in database/dataset.
    Monday, July 2, 2012 1:26 PM
  • Hi EuroGanG,

    Database or Dataset will not inform DataGridView that the value is invalid. Commonly, we often write validation in the set section of the properties, if the value is valid, assign the value to the property, if the value is invalid, return an invalid message. This is, so when we assign a value to an object's property, it will immediatelyi help us to validate the value. You can also post the issue in Windows Form Forum, there may some validation mechanism in the data bind control, but I'm not sure. The experts in Windows Form Forum may told you the common way to do that.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by EuroGanG Wednesday, July 4, 2012 6:14 PM
    Wednesday, July 4, 2012 6:52 AM
    Moderator